D
D
Danbka2019-10-20 22:32:12
symfony
Danbka, 2019-10-20 22:32:12

Do I need to use Symfony forms?

I am learning Symfony Forms. In theory, everything sounds nice and simple. However, in reality, problems arise with a form with already three fields, if one field depends on the other. For example, there are entities City - District - Metro:

class City
{
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\District", mappedBy="city")
     */
    private $districts;
}

class District
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\City", inversedBy="districts")
     * @ORM\JoinColumn(nullable=false)
     */
    private $city;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\MetroStation", mappedBy="district")
     */
    private $metroStations;
}

class MetroStation
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\District", inversedBy="metroStations")
     * @ORM\JoinColumn(nullable=false)
     */
    private $district;
}

Obviously, despite the fact that the Metro is only bound to the district, in the form of adding / changing the metro I want to be able to select the City first, then one of the Districts of this city.
An absolutely standard task, however, the tutorials I found on the topic "dependent fields" suggest using Form events in order to get the necessary data when editing the form. What for? If the necessary data can be obtained directly in the buildForm() method?
In addition, Symfony Forms in this situation does not eliminate the need to manually implement ajax requests to get a list of Districts when selecting a City.
In my opinion, the approach that is used in Laravel, when each operation with an entity (list, view form, edit form, new entry, add, change, and delete handlers) has its own method in the controller, is much more transparent and understandable.
Can you please explain what is the "trick" of Symfony Forms and their advantage? I think it looks like they are good for very simple shapes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
OnYourLips, 2019-10-21
@OnYourLips

It is not needed in symfony projects: frontend is usually separated into separate applications, and request objects are created even before controller actions.

S
Sergey Nizhny Novgorod, 2019-10-21
@Terras

It usually works like this:
1) The front is a separate js-framework that sends a Rest request
2) The controller at the DTO level, using ParamConverter and Validations, takes values ​​or gives a beat to the front.
3) The value goes to the service, where some logic goes and a response is returned through some kind of json=normalizer.
___
Ps get used to the fact that in large Symfony projects this is not used at all:
* @ORM\ManyToOne(targetEntity="App\Entity\City", inversedBy="districts")
* @ORM\JoinColumn(nullable=false )
Doctrine links do not always work adequately and quickly, so they usually make a composite key for 2-3 columns and dependencies are pulled from the repository by a separate request.

O
ozornick, 2019-12-16
@ozornick

The topic is not so old that I would also like to ask. A similar situation occurs. Before creating an object (that is, not editing a saved entity), I will not find how to process the form. There is a User object, it has nested dependencies.

$user = $this->getUser();
// Какое то значение
$client = $user->getClient()->getClientSomeData();
// Допустим теги, описания, которое являются ArrayCollection вообще тьма
$clientTags = $user->getClient()->getTags()->getTag()->getName();

Given the ManyToMany, OneToOne relationships, I don’t know how to implement the form. When you save, the connection is not generated.
Yes, just like the author, you first need to get the User, take the client_id of the connection key from him, then $user->getClient() itself and so on

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question