1

I am busy developping a Symfony 3 app.

I need to know if it is possible to have an action which is served by 2 routes :

  • one with 1 parameter
  • one with 2 parameters

In the first case, it would be for creating a new entity which is associated to the given entity. In the second case, it would be to edit an existing and given entity, which is associated to another given entity.

If that's not clear, here my example:

/**
  * @Route("/admin/instances/{group}/reunions/nouveau", requirements={"group": "\d+"})
  * @Route("/admin/instances/{group}/reunions/{reunion}/modifier", requirements={"reunion": "\d+"})
  * @Method({"GET", "POST"})
  *
  * @param Request $request
  * @param Group   $group
  * @param Reunion $reunion
  *
  * @return Response
  */
 public function newEditAction(Request $request, Group $group, Reunion $reunion = null)
 {
     if (!$reunion) {
         // we know we are busy with a creation
     }
 }

Although, with this, the $reunion is never null unless the table which contains the entities is empty.

Because both entities Group and Reunion are related to each other (see relation below), it seems Symfony automatically searches for the first Reunion entity that is associated with the given Group. Any idea why?

Reunion.php

/**
 * @var \AppBundle\Entity\Group
 *
 * @ORM\ManyToOne(targetEntity="Group", inversedBy="reunions")
 * @ORM\JoinColumn(name="reunion_group_id", referencedColumnName="group_id")
 */
private $group;

Thank you for your help.

mentinet
  • 744
  • 3
  • 9
  • 23
  • 2
    Why? This is going to create an overcomplexe action. You can easily have two actions (each with their own route), which after their specific tasks redirect to a third action/call another method, where all the common code lies – kero May 23 '17 at 14:09
  • @kero depends on what your code does.. in 99% of the times I create and update entities in one method and just handle how or if it should retrieve the entity in question. I rarely need to separate these actions in separate methods. – tftd May 23 '17 at 15:25
  • 1
    Yeah, it should not be too complexe. I just need to check whether my object is empty or not, it yes I instantiate a new one and pass it to the form. Then, when the form is submitted and valid, I check if it has an id before persisting. But I also agree with the fact that having two methods can be useful sometimes too. But to answer my question, whatever the method I use, do you know why Symfony reacts like this with the type-hinting? – mentinet May 23 '17 at 15:30

1 Answers1

0

You should have two different routes. One for creation that accepts POST only requests, the other for edit that accepts PUT or PATCH requests.

Artur Yukhatov
  • 190
  • 1
  • 9