Symfony2 teaching - Adding children to a self-contained entity

I have an entity that relates to itself. The object has fields: parent and children .

class A { // ... /** * @var A * @ORM\ManyToOne(targetEntity="A", inversedBy="children") * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true, onDelete="CASCADE") */ protected $parent; /** * @var A[] * @ORM\OneToMany(targetEntity="A", mappedBy="parent", cascade={"all"}, orphanRemoval=true) */ protected $children; } 

I want to add children to this object by customizing child elements in the form. This type of object is as follows:

 class AType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder // ... ->add('children', 'collection', [ 'type' => new AType(), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'prototype' => true, ]) ; } } 

When I send data as follows:

 'a' => [ [ 'name' => 'main a', 'children' => [ [ 'name' => 'child a 1', ], [ 'name' => 'child a 2', ], ], ], ], 

(in the test I have no idea, because this application is based on a full REST Api message) I received this error:

PHP Fatal error: the maximum level of nesting of the function "100" has been reached, is interrupted!

So, is it even possible to add children to standalone objects?

This will work if I have 2 objects: object A with child fields associated with object B. But can it work with this relation?

Should I change the type in the AType class from new AType() to something else.

EDIT: Actually, I just want to get the data and verify it. I do not need an HTML form to display it. I can do it like this:

 // controller $jms = $this->get('jms_serializer'); $entity = $jms->deserialize($request->getContent(), 'AcmeBundle\Entity\A', 'json'); $this->em->persist($entity); $this->em->flush(); 

without using the form in the controller. But in this case, my data will not be verified.

+5
source share
2 answers

PHP Fatal error: the maximum level of nesting of the function "100" has been reached, is interrupted!

Because you have recursion. When you call createForm , it tries to resolve type .

You can find this piece of code in the FormFactory resolveType function.

I think you can create a second type of form that includes title and parent .

 class AType extends AbstractType{ //... public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('title') ->add('parent') ->add('children', 'collection', array( 'type' => new BType(), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'prototype' => true )); } } class BType extends AbstractType { //.. public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('title') ->add('parent'); } } 

I think the form builder can extract and display Content-Type:application/x-www-form-urlencoded . I implemented with an html form. I also tried sending application/json , but the result was unsuccessful. This is why you can use json schema validators here.

+3
source

I would suggest you look here: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md#tree-entity-example

The structure identifier bit is based on other fields stored in the database, as well as on the parent identifier.

This model is based on this: https://en.wikipedia.org/wiki/Nested_set_model

0
source

Source: https://habr.com/ru/post/1236066/


All Articles