[設置]無法猜測如何獲得主義
- Symfony的3
CalendarEntity
可以是ChocolateEntity
BoxEntity
父父WrapperEntity
WrapperEntity
可以是ChocolateEntity
父母永遠是小孩的BoxEntity
ChocolateEntity
只能是任CalendarEntity
或WrapperEntity
[問題]
當我嘗試從任一CalendarEntity
或BoxEntity
導航到chocolate/show
或chocolate/edit
路線,我得到這個孩子消息:
無法猜測如何從請求信息
chocolate/index
和chocolate/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(),
));
}
}
可能重複[無法猜測如何從請求信息中獲得一個Doctrine實例](http://stackoverflow.com/questions/9726452/unable-to-guess-how-to-get-a-doctrine-實例從請求的信息) – Vamsi
已經檢查了這個答案,但他的情況和我的似乎differents,他的解釋是sl ... ...無法得到真正的問題提示。 – Preciel
你用過@ParamConverter嗎? – Vamsi