Home:ALL Converter>Adding multiple records to a relations table with symfony

Adding multiple records to a relations table with symfony

Ask Time:2012-09-22T02:10:05         Author:Carey Estes

Json Formatter

I have setup 3 tables in symfony:

A flipbook table, A Skills table, and a relations table, to connect each skill id with each flipbook id.

WHen I built the model, symfony constructed everything correctly, and by default gave a drop-down menu for the skills, which had all skills from the skills table as options. You can select an option and it creates the appropriate relationships.

This is working (sort of). When you submit the form, it does not add the skill_id to the record. It just adds the auto-increment id to the skills relations table and neither the flipbook_id or skill_id. Aaaaaand, if you click more than one checkbox, you get this nice message:

invalid parameter number number of bound variables does not match number of tokens

Whaaaat does that mean? This has got to be a schema issue eh?

How about some code? yes.please.

Schema:

Flipbook:
  tableName: flipbook
  inheritance:
    extends: SvaGeneric
    type: concrete
  columns:
    title: { type: string(255) }
    career_associations: { type: clob }
    skills: { type: string(255) }
    skills_associations: { type: clob }
    program: { type: string(255) }
    program_associations: { type: clob }
    draft_id: { type: integer(10) }

FlipbookSkills:
  tableName: flipbook_skills
  columns:
    title: { type: string(255) }
  relations:
    Flipbook:
      foreignAlias: flipbook_skills
      alias: skills
      local: title
      onDelete: SET NULL   

FlipbookSkillRelations:
  tableName: flipbook_skill_relations
  actAs:
    SoftDelete: ~
  options:
  columns:
    flipbook_id: { type: integer(10), notnull: false }
    skill_id: { type: integer(10), notnull: false }
  relations:
    Flipbook:
      foreignAlias: flipbook_skills_flipbook
      alias: flipbook
      local: flipbook_id
      onDelete: CASCADE
    FlipbookSkills:
      foreignAlias: flipbook_skills_skills
      alias: flipbookskills
      local: skill_id
      onDelete: CASCADE

FlipbookSkillsRelationsForm.class.php:

$this->widgetSchema['flipbook_id'] = new sfWidgetFormInputText(array(), array('class' => 'text size-500'));

$this->widgetSchema['skill_id'] = new sfWidgetFormSelectCheckbox(array('choices' => "**WHAT GOES HERE??**"), array('class' => 'text size-500'));


$useFields = array(
  'flipbook_id',
  'skill_id',
);

$this->useFields($useFields);

Let me know if I should provide further code or explanation.

Here is the FlipbookSkillsTable.class generated in the model:

<?php

/**
 * FlipbookSkillsTable
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class FlipbookSkillsTable extends Doctrine_Table
{
    /**
     * Returns an instance of this class.
     *
     * @return object FlipbookSkillsTable
     */
    public static function getInstance()
    {
        return Doctrine_Core::getTable('FlipbookSkills');
    }

    public function retrieveForFilter()
    {
      $res = $this->createQuery('s')
        ->select('s.id, s.name')
        ->orderBy('s.name ASC')
        ->execute(array(), Doctrine_Core::HYDRATE_NONE);

      // if you want an empty line
      $rets = array('' => '');
      foreach ($res as $ret)
      {
        $rets[$ret[0]] = $ret[1];
      }

      return $rets;
    }

    public function getAllFlipbookSkills() {
        $allSkills = Doctrine_Query::create()
            ->from('FlipbookSkills')
            ->orderBy('title ASC')
            ->execute();
    return $allSkills;
    }
}

UPDATE

I believe the issue is it is trying to bind the multiple values in the same record. It should make a new record for each skill chosen, like this:

id    flipbook_id    skill_id
1          2            1
2          2            2
3          2            3

SO I still don't have the schema nailed down correctly, otherwise Doctrine would know how to handle the data correct? Should I explicitly put one to many relationship?

Also, when submitted, it is only adding the flipbook_id value to the record. The skill_id record is 0

FLipbookRelationsForm:

class FlipbookSkillRelationsForm extends BaseFlipbookSkillRelationsForm
{
  public function configure()
  {

    $this->widgetSchema['skill_id'] = 
       new sfWidgetFormDoctrineChoice(array('model' => 
           $this->getRelatedModelName('flipbookskills'), 
            'expanded' => true, 'multiple' => true, 'add_empty' => false));

    $useFields = array(
            'flipbook_id',
            'skill_id',
        );

    $this->useFields($useFields);
  }
}

Author:Carey Estes,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12535904/adding-multiple-records-to-a-relations-table-with-symfony
glerendegui :

Multiple issues (maybe this post can help: Embedded forms in symfony 1.4 not saving propperly)\n\nYour n-m relations doesn't look well defined. Your entities should looks like:\n\nFlipbookSkill:\n tableName: flipbook_skill\n columns:\n title: { type: string(255) }\n relations:\n Flipbooks:\n class: Flipbook\n refClass: FlipbookSkillRelation\n local: skill_id\n foreign: flipbook_id\n/* Not foreignAlias and onDelete behaviour defined */\n\nFlipbook:\n tableName: flipbook\n inheritance:\n extends: SvaGeneric\n type: concrete\n columns:\n title: { type: string(255) }\n career_associations: { type: clob }\n skills: { type: string(255) } # why this????\n skills_associations: { type: clob } # why this????\n program: { type: string(255) }\n program_associations: { type: clob }\n draft_id: { type: integer(10) }\n relations:\n Skills:\n class: FlipbookSkill\n refClass: FlipbookSkillRelation\n local: flipbook_id\n foreign: skill_id\n\nFlipbookSkillRelation:\n tableName: flipbook_skill_relation\n actAs:\n SoftDelete: ~\n options:\n columns:\n flipbook_id: { type: integer(10), notnull: false }\n skill_id: { type: integer(10), notnull: false }\n relations:\n Flipbook:\n foreignAlias: FlipbookSkills\n local: flipbook_id\n foreign: id\n onDelete: CASCADE\n FlipbookSkill:\n foreignAlias: FlipbookSkills\n local: skill_id\n foreign: id\n onDelete: CASCADE\n\n\nThen, you methods/join allowed are:\n\n$flipbookObject->getSkills(); Because flipbook entity n-m relation name \"Skills\", wich returns a FlipbookSkill DoctrineCollection. Or in query ('f.innerJoin Skills s')\n\n$flipbookObject->getFlipbookSkills(); Because flipbookSkillRelation 1-n foreignAlias name, but probably you will never use this (because you don't really care about relations inself, instead you care about related skills).\n... etc\n\nIf you have a well defined schema.yml, then your forms should work properly.\n\nPlus:\nwhy are you using and text input widget for an id field (flipbook_id) ????\nAnd why are you using a sfWidgetFormSelectCheckbox if you are working with doctrine entities? You should use sfWidgetFormDoctrineChoice like the BaseEntity defines.\n\nI hope this can help you.",
2012-09-23T15:13:27
yy