我在 symfony 中设置了 3 个表:
一个翻书表、一个技能表和一个关系表,用于将每个技能 ID 与每个翻书 ID 连接起来。
当我构建模型时,symfony 正确构建了所有内容,并且默认为技能提供了一个下拉菜单,其中包含技能表中的所有技能作为选项。您可以选择一个选项,它会创建适当的关系。
这是工作(有点)。当您提交表单时,它不会将 Skill_id 添加到记录中。它只是将自动增量 id 添加到技能关系表中,而不是 Flipbook_id 或 Skill_id。Aaaaaand,如果您单击多个复选框,您会收到以下好消息:
invalid parameter number number of bound variables does not match number of tokens
这是什么意思?这一定是架构问题吧?
一些代码怎么样?是的,请。
架构:
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);
让我知道我是否应该提供进一步的代码或解释。
下面是模型中生成的 FlipbookSkillsTable.class:
<?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;
}
}
更新
我认为问题在于它试图在同一记录中绑定多个值。它应该为每个选择的技能创建一个新记录,如下所示:
id flipbook_id skill_id
1 2 1
2 2 2
3 2 3
所以我仍然没有正确确定模式,否则 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);
}
}