Note that there are some explanatory texts on larger screens.

plurals
  1. POone to many with symfony's admin generator
    primarykey
    data
    text
    <p>I'm using Symfony's 1.4 admin generator, and I have a question about a one to many relationship. If A has many B and B one A, I will see a listbox when editing B, that's ok, but I have to add a list (with multiple choices) of B when I edit an A. I have a solution but I don't no if it is the right way : I have modified the A form this way : </p> <pre><code>class AForm extends BaseAForm { public function configure() { $this-&gt;widgetSchema['b_list'] = new sfWidgetFormDoctrineChoice(array('multiple' =&gt; true, 'model' =&gt; 'B')); $this-&gt;validatorSchema['b_list'] = new sfValidatorDoctrineChoice(array('multiple' =&gt; true, 'model' =&gt; 'B', 'required' =&gt; false)); } public function updateDefaultsFromObject() { parent::updateDefaultsFromObject(); if (isset($this-&gt;widgetSchema['b_list'])) { $this-&gt;setDefault('b_list', $this-&gt;object-&gt;B-&gt;getPrimaryKeys()); } } protected function doSave($con = null) { $this-&gt;saveBsList($con); parent::doSave($con); } public function saveBsList($con = null) { if (!$this-&gt;isValid()) { throw $this-&gt;getErrorSchema(); } if (!isset($this-&gt;widgetSchema['b_list'])) { // somebody has unset this widget return; } if (null === $con) { $con = $this-&gt;getConnection(); } $existing = $this-&gt;object-Bs-&gt;getPrimaryKeys(); $values = $this-&gt;getValue('b_list'); if (!is_array($values)) { $values = array(); } $unlink = array_diff($existing, $values); if (count($unlink)) { $this-&gt;object-&gt;unlink('Bs', array_values($unlink)); } $link = array_diff($values, $existing); if (count($link)) { $this-&gt;object-&gt;link('Bs', array_values($link)); } } } </code></pre> <p>I use the same code which is generated for a many-to-namy relation. Is there a better way ?</p> <p>Thanks</p> <p>Edit :</p> <p>The schema</p> <pre><code>Agency: columns: id: type: integer(4) primary: true autoincrement: true name: { type: string(255), notnull: true } relations: Consultants: refClass: Consultant Consultant: columns: if: type: integer(4) primary: true autoincrement: true name: { type: string(255), notnull: true } ref_agency: integer(4) relations: Agence: foreignAlias: Agencies local: ref_agency foreign: id class: Agency </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.
 

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