Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Finally I came to a solution that I can't call elegant but at least it works.</p> <p>Branch.php:</p> <p> <pre><code>namespace Acme\Bundle\ConsysBundle\Entity; //use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * Branch * * @ORM\Entity(repositoryClass="Acme\Bundle\ConsysBundle\Entity\BranchRepository") * @ORM\HasLifecycleCallbacks * @ORM\Table(name="branch") */ class Branch { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * * @ORM\OneToMany(targetEntity="BranchMeta", mappedBy="branch", cascade={"persist", "remove"}) */ private $metadata; /** * Virtual OneToMany field */ private $telephones; /** * Virtual OneToMany field */ private $faxes; /** * Virtual OneToMany field */ private $emails; public function __construct() { $this-&gt;metadata = new ArrayCollection(); $this-&gt;telephones = new ArrayCollection(); $this-&gt;faxes = new ArrayCollection(); $this-&gt;emails = new ArrayCollection(); } /* ...setters and getters are skipped... */ /** * @ORM\PostLoad */ public function onPostLoad() { foreach (array('telephone' =&gt; 'Telephones', 'fax' =&gt; 'Faxes', 'email' =&gt; 'Emails') as $metakey =&gt; $metaname) { $metadata = $this-&gt;getMetadata()-&gt;filter( function($entry) use ($metakey) { return $entry-&gt;getName() == $metakey; } ); $meta = call_user_func(array($this, 'set'.$metaname), $metadata); } } } </code></pre> <p>controller code:</p> <pre><code>private function checkBranchAdded($branch) { foreach (array('telephone' =&gt; 'Telephones', 'fax' =&gt; 'Faxes', 'email' =&gt; 'Emails') as $metakey =&gt; $metaname) { $metadata = call_user_func(array($branch, 'get'.$metaname)); foreach ($metadata as $item) { if (!$item-&gt;getBranch()) { $item-&gt;setBranch($branch); $branch-&gt;getMetadata()-&gt;add($item); } } } } private function checkBranchDeleted($branch) { $newMeta = array(); foreach (array('telephone' =&gt; 'Telephones', 'fax' =&gt; 'Faxes', 'email' =&gt; 'Emails') as $metakey =&gt; $metaname) { $metadata = call_user_func(array($branch, 'get'.$metaname)); $newMeta = array_merge($newMeta, array_keys($metadata-&gt;toArray())); } $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); foreach($branch-&gt;getMetadata() as $key =&gt; $item) { if (!in_array($key, $newMeta)) { $em-&gt;remove($item); $branch-&gt;getMetadata()-&gt;remove($key); } } } /** * * @Template() */ public function editAction($activeMenu, $activeSubmenu, $activeSubsubmenu, $id, Request $request) { /* ... some code ... */ $repository = $this-&gt;getDoctrine()-&gt;getRepository('AcmeConsysBundle:Branch'); $branch = $repository-&gt;find($id); if (!$branch) { throw $this-&gt;createNotFoundException('No branch found for id '.$id); } $form = $this-&gt;createForm(new BranchType(), $branch); if ($request-&gt;isMethod('POST')) { $form-&gt;bind($request); $this-&gt;checkBranchDeleted($branch); $this-&gt;checkBranchAdded($branch); /* ... some code ... */ } /* ... some code ... */ } </code></pre> <p>Form builder code. BranchType.php:</p> <pre><code>class BranchType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('telephones', 'collection', array( 'type' =&gt; new BranchMetaType('telephone'), 'allow_add' =&gt; true, 'allow_delete' =&gt; true, 'prototype' =&gt; true, 'options' =&gt; array( ), )); $builder-&gt;add('faxes', 'collection', array( 'type' =&gt; new BranchMetaType('fax'), 'allow_add' =&gt; true, 'allow_delete' =&gt; true, 'prototype' =&gt; true, 'options' =&gt; array( ), )); $builder-&gt;add('emails', 'collection', array( 'type' =&gt; new BranchMetaType('email'), 'allow_add' =&gt; true, 'allow_delete' =&gt; true, 'prototype' =&gt; true, 'options' =&gt; array( ), )); /* ... other fields ... */ } /* ... other methods ... */ } </code></pre> <p>BranchMetaType.php:</p> <pre><code>class BranchMetaType extends AbstractType { protected $name; public function __construct($name) { $this-&gt;name = $name; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('name', 'hidden', array( 'data' =&gt; $this-&gt;name, )); $builder-&gt;add('value', 'text', array( )); } /* ... other methods ... */ } </code></pre>
    singulars
    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.
    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