Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2: Adding a collection based on a table-inheritance structure to a FormView
    primarykey
    data
    text
    <p>I am working on a Symfony2/Doctrine app which uses class-table-inheritance (http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#class-table-inheritance) to manage Complaints in a Consult. Each Consult can have many Complaints (OneToMany), and each different type of Complaint has a different structure and appearance. Complaints are a collection and are added dynamically with JS.</p> <p>At this point, I am able to persist the Complaints and link them to the Consults by recasting them as the appropriate types in the Controller before persistence. I have run into some issues with this and I'm planning on migrating this to a form event (http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html) or something of that nature to streamline the process.</p> <p>The problem that I am running into at this point, however, is that I am unable to display existing Complaints in a view using the FormView because the form builder demands that I set the type of the collection to be displayed. If each Consult had only one type of Complaint, that would be fine, but they can have multiple types, and setting the type in the form builder limits me to that one type.</p> <p>Is there some approach that I can take to stop the FormView from tyring to convert to string in the absence of a type or some way to dynamically detect and assign the type on a per-Complaint basis (using $complaint->getComplaintType(), perhaps)?</p> <pre><code>&lt;?php namespace Acme\ConsultBundle\Entity; class Consult { /** * @ORM\OneToMany(targetEntity="Acme\ConsultBundle\Entity\ComplaintBase", mappedBy="consult", cascade={"persist", "remove"}) */ protected $complaints; } ?&gt; &lt;?php namespace Acme\ConsultBundle\Entity; /** * Acme\ConsultBundle\Entity\ConsultBase * * @ORM\Entity * @ORM\Table(name="ConsultComplaintBase") * @ORM\HasLifecycleCallbacks * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="complaint_name", type="string") * @ORM\DiscriminatorMap({ * "ComplaintDefault" = "Acme\ConsultBundle\Entity\ComplaintDefault", * "ComplaintRosacea" = "Acme\ConsultBundle\Entity\ComplaintRosacea", * "ComplaintBotox" = "Acme\ConsultBundle\Entity\ComplaintBotox", * "ComplaintAcne" = "Acme\ConsultBundle\Entity\ComplaintAcne", * "ComplaintUrticaria" = "Acme\ConsultBundle\Entity\ComplaintUrticaria", * }) */ abstract class ComplaintBase { /** * @ORM\ManyToOne(targetEntity="Acme\ConsultBundle\Entity\Consult", inversedBy="complaints") * @ORM\JoinColumn(name="consult_id", referencedColumnName="id") */ protected $consult; /** * @ORM\Column(type="string", length="255") */ protected $complaintType; } ?&gt; &lt;?php namespace Acme\ConsultBundle\Form\Type; class ConsultType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder -&gt;add('complaints', 'collection', array( // 'type' =&gt; new ComplaintUrticariaType(), 'error_bubbling' =&gt; true, 'allow_add' =&gt; true, 'allow_delete' =&gt; true, 'by_reference' =&gt; false, )); } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload