Symfony Generator Forms, Doctrine, and M:N Relationships [duplicate]
Possible Duplicate:
Symfony Generator Forms, Doctrine, and M:N Relationships
I have a basic M:N setup with three tables: candidate, position, and candidate_position. Here's my best stab at a crow's-foot style ERD with just text
[candiate]-||------|<[candidate_position]>|------||-[position]
What I'm trying to accomplish is this: When a candidate
is being created or edited, the form will include a checkbox array of all available positions to assign to the candidate.
In the normal world of web app development, this is really, really easy. But I'm trying to increase my competency with symfony's admin generator. Here's what I've got so far
apps/backend/modules/condidate/config/generator.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: ~
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
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]
And this works! Sort of =/
The checkboxes render to the create and edit screens, but the data doesn't save. Clearly (?) I need to do some customizaton to the model (lib/model/doctrine/candidate.class.php), and that's where I'm losing focus. I'm not sure how to get at the candidate[positions] data from within candidate开发者_如何学Go::save()
- PHP 5.2.x
- symfony 1.4.3
I assume you did your schema creating first the db and then execute the build-schema command. Doing that way dosn't create the relationships properly between M:N. So you have to add to your schema.yml this and then rebuild your models, forms and builders.
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
//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);
}
And it should work right away even with default generator.yml
精彩评论