使用symfony将多个记录添加到关系表中 [英] Adding multiple records to a relations table with symfony

查看:199
本文介绍了使用symfony将多个记录添加到关系表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在symfony中设置了3个表:



一个幻灯片表,一个技能表和一个关系表,用于将每个技能id与每个flipbook id相连接。



当我建立模型时,symfony正确构建了一切,默认情况下会提供一个技能下拉菜单,该技能将技能表中的所有技能作为选项。您可以选择一个选项,并创建适当的关系。



这是工作(排序)。提交表单时,不会将skill_id添加到记录中。它只是将自动递增ID添加到技能关系表中,而不是flipbook_id或skill_id。 Aaaaaand,如果您点击多个复选框,您会得到这个好消息:

 无效参数号绑定变量的数量不匹配令牌数

Whaaaat是什么意思?这必须是一个模式问题eh?



一些代码怎么样? yes.please。



模式:

  Flipbook:
tableName:flipbook
继承:
extends:SvaGeneric
类型:具体
列:
标题:{type:string(255) }
career_associations:{type:clob}
技能:{type:string(255)}
skills_associations:{type:clob}
程序:{type:string(255)
program_associations:{type:clob}
draft_id:{type:integer(10)}

FlipbookSkills:
tableName:flipbook_skills
columns:
标题:{type:string(255)}
关系:
Flipbook:
foreignAlias:flipbook_skills
别名:技能
本地:标题
onDelete:SET NULL

FlipbookSkillRelations:
tableName:flipbook_skill_relations
actAs:
SoftDelete:〜
选项:
列:
flipbook_id:{type:integer(10),notnull:false}
skill_i d:{type:integer(10),notnull:false}
关系:
Flipbook:
foreignAlias:flipbook_skills_flipbook
别名:flipbook
本地:flipbook_id
onDelete:CASCADE
FlipbookSkills:
foreignAlias:flipbook_skills_skills
别名:flipbookskills
本地:skill_id
onDelete:CASCADE
/ pre>

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);

如果我应该提供进一步的代码或解释,请告诉我。



这是模型中生成的FlipbookSkillsTable.class:

 <?php 

/ **
* FlipbookSkillsTable
*
*此类已由Doctrine ORM框架自动生成
* /
class FlipbookSkillsTable extends Doctrine_Table
{
/ **
*返回此类的一个实例。
*
* @return对象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);

//如果你想要一个空行
$ rets = array(''=>'');
foreach($ res as $ ret)
{
$ regts [$ ret [0]] = $ ret [1];
}

return $ regts;
}

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

更新



我相信这个问题是在同一个记录中绑定多个值。它应该为所选择的每个技能创造一个新的记录,如下所示:

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

SO我还没有架构正确地确定,否则Doctrine会知道如何处理数据正确吗?我应该明确地提出一个很多的关系吗?



此外,当提交时,它只是将flipbook_id值添加到记录中。 skill_id记录为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);
}
}


解决方案

问题(也许这篇文章可以帮助:嵌入表单在symfony 1.4中不能保存得很好



您的nm关系定义不够明确。您的实体应该如下所示:

  FlipbookSkill:
tableName:flipbook_skill
列:
标题:{type:string(255)}
关系:
Flipbooks:
class:Flipbook
refClass:FlipbookSkillRelation
local:skill_id
foreign:flipbook_id
/ *不定义外国人和行为定义的行为* /

Flipbook:
tableName:flipbook
继承:
extends:SvaGeneric
type:concrete
列:
标题:{type:string(255)}
career_associations:{type:clob}
技能:{type:string(255)}# ??
skills_associations:{type:clob}#为什么这样???
程序:{type:string(255)}
program_associations:{type:clob}
draft_id:{type:integer(10)}
关系:
技能:
class:FlipbookSkill
refClass:FlipbookSkillRelation
local:flipbook_id
foreign:skill_id

FlipbookSkillRelation:
tableName:flipbook_skill_relation
actAs:
SoftDelete:〜
选项:
列:
flipbook_id:{type:integer(10),notnull:false}
skill_id:{type:integer 10),notnull:false}
关系:
Flipbook:
foreignAlias:FlipbookSkills
local:flipbook_id
foreign:id
onDelete:CASCADE
FlipbookSkill:
foreignAlias:FlipbookSkills
local:skill_id
foreign:id
onDelete:CASCADE

然后,你允许的方法/ join是:



$ flipbookObject-> getSkills();因为flipbook实体n-m关系名称技能,所以返回一个FlipbookSkill DoctrineCollection。或者在查询('f.innerJoin技能's)中



$ flipbookObject-> getFlipbookSkills();因为flipbookSkillRelation 1-n foreignAlias的名字,但可能你永远不会使用这个(因为你不关心关系本身,而是关心相关技能)。
...等



如果您有一个明确定义的schema.yml,那么您的表单应该正常工作。



Plus:
为什么要使用id文本输入小部件(flipbook_id)?
如果您正在使用教义实体,您为什么要使用sfWidgetFormSelectCheckbox?你应该使用像BaseEntity定义的sfWidgetFormDoctrineChoice。



我希望这可以帮助你。


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);
  }
}

解决方案

Multiple issues (maybe this post can help: Embedded forms in symfony 1.4 not saving propperly)

Your n-m relations doesn't look well defined. Your entities should looks like:

FlipbookSkill:
  tableName: flipbook_skill
  columns:
    title: { type: string(255) }
  relations:
    Flipbooks:
      class: Flipbook
      refClass: FlipbookSkillRelation
      local: skill_id
      foreign: flipbook_id
/* Not foreignAlias and onDelete behaviour defined */

Flipbook:
  tableName: flipbook
  inheritance:
    extends: SvaGeneric
    type: concrete
  columns:
    title: { type: string(255) }
    career_associations: { type: clob }
    skills: { type: string(255) } # why this????
    skills_associations: { type: clob }  # why this????
    program: { type: string(255) }
    program_associations: { type: clob }
    draft_id: { type: integer(10) }
  relations:
    Skills:
      class: FlipbookSkill
      refClass: FlipbookSkillRelation
      local: flipbook_id
      foreign: skill_id

FlipbookSkillRelation:
  tableName: flipbook_skill_relation
  actAs:
    SoftDelete: ~
  options:
  columns:
    flipbook_id: { type: integer(10), notnull: false }
    skill_id: { type: integer(10), notnull: false }
  relations:
    Flipbook:
      foreignAlias: FlipbookSkills
      local: flipbook_id
      foreign: id
      onDelete: CASCADE
    FlipbookSkill:
      foreignAlias: FlipbookSkills
      local: skill_id
      foreign: id
      onDelete: CASCADE

Then, you methods/join allowed are:

$flipbookObject->getSkills(); Because flipbook entity n-m relation name "Skills", wich returns a FlipbookSkill DoctrineCollection. Or in query ('f.innerJoin Skills s')

$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). ... etc

If you have a well defined schema.yml, then your forms should work properly.

Plus: why are you using and text input widget for an id field (flipbook_id) ???? And why are you using a sfWidgetFormSelectCheckbox if you are working with doctrine entities? You should use sfWidgetFormDoctrineChoice like the BaseEntity defines.

I hope this can help you.

这篇关于使用symfony将多个记录添加到关系表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆