无法猜测如何获得一个学说

时间:2016-12-16 03:39:13

标签: doctrine-orm doctrine symfony

[设置]

  • Symfony 3
  • protected $except = [ 'your route here' ]; 可以是CalendarEntity
  • 的父级
  • ChocolateEntity是父BoxEntity
  • WrapperEntity可以是WrapperEntity的父级,并且始终是ChocolateEntity
  • 的孩子
  • BoxEntity只能是ChocolateEntityCalendarEntity
  • 的孩子

Diagram

[问题]

当我尝试从WrapperEntitychocolate/show导航到chocolate/editCalendarEntity路线时,我收到此消息:

  

无法猜测如何从请求信息中获取Doctrine实例

BoxEntitychocolate/index路由工作正常 直到我在chocolate/new的控制器中添加代码,每条路线都正常工作。

检查dev.log文件给了我这个:

CalendarEntity

在我看来,Symfony对每个请求都需要参数,没有额外的参数 我无法解释如何解决这个问题。

[FILES]

ENTITIES:

的src /的appbundle /实体/ Calendar.php

// 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"} []

的src /的appbundle /实体/ Box.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 /实体/ Wrapper.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 /实体/ Chocolate.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 /资源/配置/ calendar.yml

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 /资源/配置/ box.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 /资源/配置/ wrapper.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 /资源/配置/ chocolate.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 /控制器/ CalendarController.php

# 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 /控制器/ BoxController.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 /控制器/ WrapperController.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 /控制器/ ChocolateController.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(),
        ));
    }
}

2 个答案:

答案 0 :(得分:1)

由于每个实体都是相关的,因此您无需将额外参数传递给showAction()editAction()

收回您的代码,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 %}

这样做,即使您只传递ChocolateEntity作为参数,也会提示Symfony获取父数据。其他树枝文件也是如此。

希望它会对你有所帮助。

这个主题很好吃,我自己去买一些巧克力。

答案 1 :(得分: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) {
...

我认为@Vamsi是正确的。