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.