2016-12-16 21 views
2

[設置]無法猜測如何獲得主義

  • Symfony的3
  • CalendarEntity可以是ChocolateEntity
  • BoxEntity父父WrapperEntity
  • WrapperEntity可以是ChocolateEntity父母永遠是小孩的BoxEntity
  • ChocolateEntity只能是任CalendarEntityWrapperEntity

Diagram

[問題]

當我嘗試從任一CalendarEntityBoxEntity導航到chocolate/showchocolate/edit路線,我得到這個孩子消息:

無法猜測如何從請求信息

chocolate/indexchocolate/new路線做工精細獲得學說實例。
直到我來添加代碼在控制器爲CalendarEntity,每個路線工作正常。

檢查dev.log文件給了我這個:

// Route: /calendar/{idCalendar}/chocolate/{idChocolate}/show 
request.INFO: Matched route "calendar_chocolate_show". {"route":"calendar_chocolate_show","route_parameters":{"_controller":"AppBundle\\Controller\\ChocolateController::showAction","idCalendar":"1","idChocolate":"3","_route":"calendar_chocolate_show"},"request_uri":"http://sphere.gdn/app_dev.php/calendar/1/chocolate/3/show","method":"GET"} [] 

// Route: /box/{idBox}/wrapper/{idWrapper}/chocolate/{idChocolate}/show 
request.INFO: Matched route "wrapper_chocolate_show". {"route":"wrapper_chocolate_show","route_parameters":{"_controller":"AppBundle\\Controller\\ChocolateController::showAction","idBox":"1","idWrapper":"1","idChocolate":"1","_route":"wrapper_chocolate_show"},"request_uri":"http://sphere.gdn/app_dev.php/box/1/wrapper/1/chocolate/1/show","method":"GET"} [] 

在我看來,Symfony的對每個請求都需要paramters,並沒有多餘的參數。
我不能指出如何解決這個問題。

[FILES]

實體:

SRC /的appbundle /實體/ Calendar.php

class Calendar { 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $idCalendar; 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $nameCalendar; 
    /** 
    * @var 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Chocolate", mappedBy="calendar") 
    */ 
    private $chocolate; 
} 

SRC /的appbundle /實體/箱.php

class Box { 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $idBox; 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="parent", type="integer", nullable=true) 
    */ 
    private $parent; 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $nameBox; 
    /** 
    * @var 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Wrapper", mappedBy="box") 
    */ 
    private $wrapper; 
} 

的src /的appbundle /實體/ Wrapper.php

class Wrapper { 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $idWrapper; 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $nameWrapper; 
    /** 
    * @var 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Box", inversedBy="wrapper") 
    * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) 
    */ 
    private $box; 
    /** 
    * @var 
    * 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Chocolate", mappedBy="wrapper") 
    */ 
    private $chocolate; 
} 

的src /的appbundle /實體/巧克力。PHP

class Chocolate { 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $idChocolate; 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $nameChocolate; 
    /** 
    * @var 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Calendar", inversedBy="chocolate") 
    * @ORM\JoinColumn(onDelete="CASCADE", nullable=true) 
    */ 
    private $calendar; 
    /** 
    * @var 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Wrapper", inversedBy="chocolate") 
    * @ORM\JoinColumn(onDelete="CASCADE", nullable=true) 
    */ 
    private $wrapper; 
} 


路線:

的src /的appbundle /資源/配置/ calendar.yml

# Calendar 
calendar_show: 
    path:  /calendar/{idCalendar}/show/ 
    defaults: { _controller: "AppBundle:Calendar:show" } 
    methods: GET 

calendar_edit: 
    path:  /calendar/{idCalendar}/edit/ 
    defaults: { _controller: "AppBundle:Calendar:edit" } 
    methods: [GET, POST] 

# Chocolate 
calendar_chocolate: 
    resource: "@AppBundle/Resources/config/chocolate.yml" 
    prefix: /

的src /的appbundle /資源/配置/ box.yml

#Box 
box_show: 
    path:  /box/{idBox}/show/ 
    defaults: { _controller: "AppBundle:Box:show" } 
    methods: GET 

box_edit: 
    path:  /box/{idBox}/edit/ 
    defaults: { _controller: "AppBundle:Box:edit" } 
    methods: [GET, POST] 

# Wrapper 
box_wrapper: 
    resource: "@AppBundle/Resources/config/wrapper.yml" 
    prefix: /

的src /的appbundle /資源/配置/ wrapper.yml

# Wrapper 
wrapper_show: 
    path:  /box/{idBox}/wrapper/{idWrapper}/show/ 
    defaults: { _controller: "AppBundle:Wrapper:show" } 
    methods: GET 

wrapper_edit: 
    path:  /box/{idBox}/wrapper/{idWrapper}/edit/ 
    defaults: { _controller: "AppBundle:Wrapper:edit" } 
    methods: [GET, POST] 

# Chocolate 
wrapper_chocolate: 
    resource: "@AppBundle/Resources/config/chocolate.yml" 
    prefix: /

的src /的appbundle /資源/配置/ chocolate.yml

# Calendar Chocolate 
calendar_chocolate_show: 
    path:  /calendar/{idCalendar}/chocolate/{idChocolate}/show 
    defaults: { _controller: "AppBundle:Chocolate:show" } 
    methods: GET 

calendar_chocolate_edit: 
    path:  /calendar/{idCalendar}/chocolate/{idChocolate}/edit 
    defaults: { _controller: "AppBundle:Chocolate:edit" } 
    methods: [GET, POST] 

# Box Chocolate 
wrapper_chocolate_show: 
    path:  /box/{idBox}/wrapper/{idWrapper}/chocolate/{idChocolate}/show 
    defaults: { _controller: "AppBundle:Chocolate:show" } 
    methods: GET 

wrapper_chocolate_edit: 
    path:  /box/{idBox}/wrapper/{idWrapper}/chocolate/{idChocolate}/edit 
    defaults: { _controller: "AppBundle:Chocolate:edit" } 
    methods: [GET, POST] 


控制器:

SRC /的appbundle /控制器/ CalendarController.php

class CalendarController extends Controller { 
    /** 
    * Finds and displays a Calendar entity. 
    * 
    * @param Calendar $calendar 
    * 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function showAction(Calendar $calendar) { 
     $deleteForm=$this->createDeleteForm($calendar); 

     return $this->render('AppBundle:calendar:show.html.twig', array(
      'calendar'=>$calendar, 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 

    /** 
    * Displays a form to edit an existing Calendar entity. 
    * 
    * @param Request $request 
    * @param Calendar $calendar 
    * 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response 
    */ 
    public function editAction(Request $request, Calendar $calendar) { 
     $deleteForm=$this->createDeleteForm($calendar); 
     $editForm=$this->createForm('AppBundle\Form\CalendarType', $calendar); 
     $editForm->handleRequest($request); 

     if($editForm->isSubmitted() && $editForm->isValid()) { 
      $em=$this->getDoctrine() 
        ->getManager(); 
      $em->persist($calendar); 
      $em->flush(); 

      return $this->redirectToRoute('calendar_edit', array('idCalendar'=>$calendar->getIdCalendar())); 
     } 

     return $this->render('AppBundle:calendar:edit.html.twig', array(
      'calendar'=>$calendar, 
      'edit_form'=>$editForm->createView(), 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 
} 

SRC /的appbundle /控制器/ BoxController.php

class BoxController extends Controller { 
    /** 
    * Finds and displays a Box entity. 
    * 
    * @param Box $box 
    * 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function showAction(Box $box) { 
     $deleteForm=$this->createDeleteForm($box); 

     return $this->render('AppBundle:box:show.html.twig', array(
      'box'=>$box, 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 

    /** 
    * Displays a form to edit an existing Box entity. 
    * 
    * @param Request $request 
    * @param Box $box 
    * 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response 
    */ 
    public function editAction(Request $request, Box $box) { 
     $deleteForm=$this->createDeleteForm($box); 
     $editForm=$this->createForm('AppBundle\Form\BoxType', $box); 
     $editForm->handleRequest($request); 

     if($editForm->isSubmitted() && $editForm->isValid()) { 
      $em=$this->getDoctrine() 
        ->getManager(); 
      $em->persist($box); 
      $em->flush(); 

      return $this->redirectToRoute('box_edit', array('idBox'=>$box->getIdBox())); 
     } 

     return $this->render('AppBundle:box:edit.html.twig', array(
      'box'=>$box, 
      'edit_form'=>$editForm->createView(), 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 
} 

的src /的appbundle /控制器/ WrapperController.php

class WrapperController extends Controller { 
    /** 
    * Finds and displays a Wrapper entity. 
    * 
    * @param Box $box 
    * @param Wrapper $wrapper 
    * 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function showAction(Box $box, Wrapper $wrapper) { 
     $deleteForm=$this->createDeleteForm($box, $wrapper); 

     return $this->render('AppBundle:wrapper:show.html.twig', array(
      'box'=>$box, 
      'wrapper'=>$wrapper, 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 

    /** 
    * Displays a form to edit an existing Wrapper entity. 
    * 
    * @param Request $request 
    * @param Box $box 
    * @param Wrapper $wrapper 
    * 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response 
    */ 
    public function editAction(Request $request, Box $box, Wrapper $wrapper) { 
     $deleteForm=$this->createDeleteForm($box, $wrapper); 
     $editForm=$this->createForm('AppBundle\Form\WrapperType', $wrapper); 
     $editForm->handleRequest($request); 

     if($editForm->isSubmitted() && $editForm->isValid()) { 
      $em=$this->getDoctrine() 
        ->getManager(); 
      $em->persist($wrapper); 
      $em->flush(); 
      return $this->redirectToRoute('wrapper_edit', array(
       'idBox'=>$box->getIdBox(), 
       'idWrapper'=>$wrapper->getIdWrapper() 
      )); 
     } 

     return $this->render('AppBundle:wrapper:edit.html.twig', array(
      'box'=>$box, 
      'wrapper'=>$wrapper, 
      'edit_form'=>$editForm->createView(), 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 
} 

的src /的appbundle /控制器/ ChocolateController.php

class ChocolateController extends Controller { 
    /** 
    * Finds and displays a Chocolate entity. 
    * 
    * @param Calendar $calendar 
    * @param Box $box 
    * @param Wrapper $wrapper 
    * @param Chocolate $chocolate 
    * 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function showAction(Calendar $calendar=null, Box $box=null, Wrapper $wrapper=null, Chocolate $chocolate) { 
     $deleteForm=$this->createDeleteForm($calendar, $box, $wrapper, $chocolate); 

     return $this->render('AppBundle:chocolate:show.html.twig', array(
      'calendar'=>$calendar, 
      'box'=>$box, 
      'wrapper'=>$wrapper, 
      'chocolate'=>$chocolate, 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 

    /** 
    * Displays a form to edit an existing Chocolate entity. 
    * 
    * @param Request $request 
    * @param Calendar $calendar 
    * @param Box $box 
    * @param Wrapper $wrapper 
    * @param Chocolate $chocolate 
    * 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response 
    */ 
    public function editAction(Request $request, Calendar $calendar=null, Box $box=null, Wrapper $wrapper=null, Chocolate $chocolate) { 
     $deleteForm=$this->createDeleteForm($calendar, $box, $wrapper, $chocolate); 
     $editForm=$this->createForm('AppBundle\Form\ChocolateType', $chocolate); 
     $editForm->handleRequest($request); 

     if($editForm->isSubmitted() && $editForm->isValid()) { 
      $em=$this->getDoctrine() 
        ->getManager(); 
      $em->persist($chocolate); 
      $em->flush(); 
      if($box) { 
       return $this->redirectToRoute('wrapper_box_chocolate_edit', array(
        'idBox'=>$box->getIdBox(), 
        'idWrapper'=>$wrapper->getIdWrapper(), 
        'idChocolate'=>$chocolate->getIdChocolate() 
       )); 
      } else { 
       return $this->redirectToRoute('calendar_chocolate_edit', array(
        'idCalendar'=>$calendar->getIdCalendar(), 
        'idWrapper'=>$wrapper->getIdWrapper(), 
        'idChocolate'=>$chocolate->getIdChocolate() 
       )); 
      } 
     } 

     return $this->render('AppBundle:chocolate:edit.html.twig', array(
      'calendar'=>$calendar, 
      'box'=>$box, 
      'wrapper'=>$wrapper, 
      'chocolate'=>$chocolate, 
      'edit_form'=>$editForm->createView(), 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 
} 
+0

可能重複[無法猜測如何從請求信息中獲得一個Doctrine實例](http://stackoverflow.com/questions/9726452/unable-to-guess-how-to-get-a-doctrine-實例從請求的信息) – Vamsi

+0

已經檢查了這個答案,但他的情況和我的似乎differents,他的解釋是sl ... ...無法得到真正的問題提示。 – Preciel

+0

你用過@ParamConverter嗎? – Vamsi

回答

1

由於POB實體hawliau ICHI唐娜angen在editAction()額外的參數傳遞給您的showAction()

收回你的代碼,showAction()會變成這樣:

public function showAction(Chocolate $chocolate) { 
    $deleteForm=$this->createDeleteForm($video); 

    return $this->render('AppBundle:chocolate:show.html.twig', array(
     'chocolate'=>$chocolate, 
     'delete_form'=>$deleteForm->createView(), 
    )); 
} 

和你editAction()會變成這樣:

public function editAction(Request $request, Chocolate $chocolate) { 
    $deleteForm=$this->createDeleteForm($chocolate); 
    $editForm=$this->createForm('AppBundle\Form\ChocolateType', $chocolate); 
    $editForm->handleRequest($request); 

    if($editForm->isSubmitted() && $editForm->isValid()) { 
     $em=$this->getDoctrine() 
       ->getManager(); 
     $em->persist($chocolate); 
     $em->flush(); 
     if($chocolate->getWrapper() !== null) { 
      return $this->redirectToRoute('wrapper_chocolate_edit', 

     array(
      'idBox'=>$chocolate->getWrapper()->getBox()->getIdBox(), 
      'idWrapper'=>$chocolate->getWrapper()->getIdWrapper(), 
      'idChocolate'=>$chocolate->getIdChocolate() 
     )); 
    } else { 
     return $this->redirectToRoute('calendar_chocolate_edit', 
      array(
       'idCalendar'=>$chocolate->getCalendar()->getIdCalendar(), 
       'idChocolate'=>$chocolate->getIdChocolate() 
       )); 
      } 
     } 

     return $this->render('AppBundle:Dashboard/chocolate:edit.html.twig', array(
      'chocolate'=>$chocolate, 
      'edit_form'=>$editForm->createView(), 
      'delete_form'=>$deleteForm->createView(), 
     )); 
    } 

每一個信息都在ChocolateEntity,你只需要得到家長的每個時間,然後是所需的參數。

像你一樣的東西,只有showAction()newAction()需要有參數,這是因爲,在這兩種情況下,你不返回一個特定ChocolateEntity

public function indexAction(Calendar $calendar=null, Wrapper $wrapper=null) 
public function newAction(Request $request, Calendar $calendar=null, Wrapper $wrapper=null) 

而像editAction()你可以得到父母沒有通過他們。

另外請注意,你的樹枝會改變一點。 由於爲例,當你顯示一個實體(showAction()),鏈接「回到列表」將改變基礎的路線,它就會放棄這樣的事情:

{% if chocolate.wrapper is not null %} 
    <a href="{{ path('wrapper_chocolate_list', { 'idBox': chocolate.wrapper.box.idBox, 'idWrapper': chocolate.wrapper.idWrapper }) }}">Back to the list</a> 
{% else %} 
    <a href="{{ path('calendar_chocolate_list', { 'idCalendar': chocolate.calendar.idCalendar }) }}">Back to the list</a> 
{% endif %} 

這樣做,會提示Symfony的到即使您僅通過ChocolateEntity作爲參數,也可以獲取父母的數據。其他的樹枝文件也是一樣的。

希望它能幫助你。

這個話題很好吃,我自己會得到一些巧克力。

+0

確實解決了我的問題!將它應用到我的項目上,而我在它的時候,感謝詳細回覆! – Preciel

0

所以Preciel,在ChocolateController嘗試:

/** 
* Finds and displays a Chocolate entity. 
* 
* @param Calendar $calendar 
* @param Box $box 
* @param Wrapper $wrapper 
* @param Chocolate $chocolate 
* 
* @return \Symfony\Component\HttpFoundation\Response 
* 
* @ParamConverter("calendar", class="AppBundle:Calendar") 
* @ParamConverter("chocolate", class="AppBundle:Chocolate") 
*/ 
public function showAction(Calendar $calendar=null, Box $box=null, Wrapper $wrapper=null, Chocolate $chocolate) { 
... 

要feddwl @Vamsi下是正確的。

+0

我試過這麼簡單的語法是的,甚至把它擴展到另外兩個實體,但沒有奏效。 :( – Preciel

+0

什麼沒有工作?你可以顯示你的日誌這條路線:'http:// sphere.gdn/app_dev.php/calendar/1/chocolate/3/show' –

+0

Hi there Preciel我並沒有計劃在這方面花費太多時間,大多數用戶都非常忙,這就是爲什麼我們喜歡優秀(措辭不錯)的問題。您可以使用[pastebin](http://pastebin.com/)發佈信息日誌? –