Search code examples
widgetsymfony-1.4propel

Symfony - relation n:m - sfWidgetFormPropelChoice


I go straight to the point;) I have such a scheme in the database:

    <table name="kongres">
    <column name="id" type="integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="is_actived" type="boolean" default="0"/>
    <column name="is_homepage" type="boolean" default="0"/>
    <column name="template" type="VARCHAR" size="50" required="true"/>
    <column name="produkt" type="integer" required="true"/>
</table>

<table name="menugroup">
    <column name="id" type="integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="tytul" type="varchar" size="255"/>
</table>

<table name="kongresmenugroup">
    <column name="id" type="integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="kongres_id" type="integer" required="true"/>
    <foreign-key foreignTable="kongres" onDelete="CASCADE">
        <reference local="kongres_id"
                   foreign="id"/>
    </foreign-key>
    <column name="menugroup_id" type="integer" required="true"/>
    <foreign-key foreignTable="menugroup" onDelete="CASCADE">
        <reference local="menugroup_id"
                   foreign="id"/>
    </foreign-key>
</table>

Once the correct command: symfony propel: build-model and symfony propel: build-forms I got a form (I've added and expanded and mulitple option, because I need to save more conferences than one in sub-menu) (backend):

     public function configure()
{

$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'kongres_id' => new sfWidgetFormPropelChoice(array('model' => 'Kongres', 'add_empty' => false, 'expanded' => true, 'multiple' => true)),
'menugroup_id' => new sfWidgetFormPropelChoice(array('model' => 'Menugroup', 'add_empty' => false)),
));

$this->setValidators(array(
'id' => new sfValidatorPropelChoice(array('model' => 'Kongresmenugroup', 'column' => 'id', 'required' => false)),
'kongres_id' => new sfValidatorPropelChoice(array('model' => 'Kongres', 'column' => 'id','multiple' => true)),
'menugroup_id' => new sfValidatorPropelChoice(array('model' => 'Menugroup', 'column' => 'id','multiple' => true)),
));

$this->widgetSchema->setNameFormat('kongresmenugroup[%s]');

$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);


}

And now my question, as if I click on SAVE it save's data to the database, but they are not correct, it always writes to the fields: menugroup_id -> 1 and kongres_id -> 1, for example, despite the fact that Congress has chosen ID = 21 and ID menu is equal to eg 3 Please tell me what I'm doing wrong because I am lost already ;/ Thanks!

btw. sorry for my english ;)


Solution

  • It is because you have 'multiple' => true on foreign keys in your form that the value is being save to 1; the foreign key relationship does not understand the array which is sent by this form control.
    Change multiple to false, or change the schema so that kongres_id and menugroup_id are not foreign keys.