Php Symfony生成器形式、原则和M:N关系
可能重复:Php Symfony生成器形式、原则和M:N关系,php,symfony1,doctrine,symfony-1.4,symfony-forms,Php,Symfony1,Doctrine,Symfony 1.4,Symfony Forms,可能重复: 我有一个基本的M:N设置,有三个表:候选者、职位和候选者职位。这是我在鱼尾纹风格ERD上的最佳尝试,只需文本 [candiate]-||------|<[candidate_position]>|------||-[position] lib/form/doctrine/candidateForm.class.php class candidateForm extends BasecandidateForm { public function configure(
我有一个基本的M:N设置,有三个表:候选者、职位和候选者职位。这是我在鱼尾纹风格ERD上的最佳尝试,只需文本
[candiate]-||------|<[candidate_position]>|------||-[position]
lib/form/doctrine/candidateForm.class.php
class candidateForm extends BasecandidateForm
{
public function configure()
{
unset( $this['created_at'] );
$this->widgetSchema['positions'] = new sfWidgetFormDoctrineChoice(
array( 'multiple' => true, 'model' => 'Position', 'renderer_class' => 'sfWidgetFormSelectCheckbox' )
);
$this->validatorSchema['positions'] = new sfValidatorDoctrineChoice(
array( 'multiple' => true, 'model' => 'Position', 'min' => 1 )
);
}
}
config/doctrine/schema.yml
generator:
class: sfDoctrineGenerator
param:
model_class: Candidate
theme: admin
non_verbose_templates: true
with_show: false
singular: ~
plural: ~
route_prefix: candidate
with_doctrine_route: true
actions_base_class: sfActions
config:
actions: ~
fields:
first_name: { label: First Name }
last_name: { label: Last Name }
created_at: { label: Created On }
positions: {}
list:
sort: [last_name, asc]
filter: ~
form:
display:
"User": [first_name, last_name]
"Applying For": [positions]
fields :
hide: [created_at]
edit: ~
new: ~
candidate:
columns:
id:
type: integer(4)
primary: true
unsigned: true
notnull: true
autoincrement: true
first_name:
type: string(45)
notnull: true
last_name:
type: string(45)
notnull: true
created_at:
type: integer(4)
unsigned: true
position:
columns:
id:
type: integer(4)
primary: true
unsigned: true
notnull: true
autoincrement: true
name:
type: string(45)
candidatePosition:
tableName: candidate_position
columns:
candidate_id:
type: integer(4)
primary: true
unsigned: true
notnull: true
position_id:
type: integer(4)
primary: true
unsigned: true
notnull: true
relations:
candidate:
class: candidate
local: candidate_id
foreign: id
foreignAlias: candidate_positions
position:
class: position
local: position_id
foreign: id
foreignAlias: candidate_positions
indexes:
fk_candidate_position_candidate1:
fields: [candidate_id]
fk_candidate_position_position1:
fields: [position_id]
这很有效!有点=/
复选框将呈现到“创建”和“编辑”屏幕,但数据不会保存。显然(?)我需要对模型进行一些定制(lib/model/doctrine/candidate.class.php),这就是我失去重点的地方。我不知道如何从candidate::save()
- PHP5.2.x
- symfony 1.4.3
candidate:
columns:
id:
type: integer(4)
primary: true
unsigned: true
notnull: true
autoincrement: true
first_name:
type: string(45)
notnull: true
last_name:
type: string(45)
notnull: true
created_at:
type: integer(4)
unsigned: true
relations:
Position:
refClass: candidatePosition
local: candidate_id
foreign: position_id
position:
columns:
id:
type: integer(4)
primary: true
unsigned: true
notnull: true
autoincrement: true
name:
type: string(45)
relations:
Candidate:
refClass: candidatePosition
local: position_id
foreign: candidate_id
即使使用默认生成器,它也应该可以立即工作。yml不确定这是否可以修复它,但请尝试将关系规范放在主表而不是引用表中,如-,当我这样做时,表单在admin generator应用程序中正常工作,在
php-symfony-doctrine:build-all
之后没有任何修改,谢谢您的回答。我的开发机器现在在电脑里。一旦它完全恢复健康,我将继续研究这个问题。回到这个问题上。这并不能解决问题。没有保存任何东西,而且它直接与doctrine自己的文档相反,关于这个主题,您在哪里看到这个模式,它直接与doctrine的文档相反?你试过了吗?哦,是的,我确实试过了。doctrine的文档说“doctrine只要求您在外键存在的一端指定关系。”在我的例子中,这就是候选位置表。你和Dziamid都让我把它们移到引用的表中。最后,不是外键存在的地方(即候选或位置)。因此,没有必要在两端定义关系,因为引用“关系的另一端将反映并建立在另一端”。检查示例中的关系是否在组模型中定义。感谢您的回答。我的开发机器现在在电脑里。一旦它完全恢复健康,我将继续研究这个问题。回到这个问题上。这并不能解决问题。没有保存任何内容,而且它与doctrine自己关于该主题的文档直接相反
//schema.yml
Candidate:
columns: ~
relations:
Position:
alias: Positions
refClass: CandidatePosition
local: candidate_id
foreign: position_id
Position:
columns: ~
relations:
Candidate:
alias: Candidates
refClass: CandidatePosition
local: position_id
foreign: candidate_id
CandidatePosition:
columns:
candidate_id:
type: integer(4)
primary: true
position_id:
type: integer(4)
primary: true
//CandidateForm class
public function configure()
{
$this->getWidget('positions_list')->setOption('expanded', true);
}