3

可能的重复:
Symfony 生成器形式、学说和 M:N 关系

我有一个基本的 M:N 设置,包含三个表:候选、位置和候选位置。这是我对鱼尾纹风格 ERD 的最佳尝试,只有文字

[candiate]-||------|<[candidate_position]>|------||-[position]

我想要完成的是:当candidate创建或编辑 a 时,表单将包含一个复选框数组,其中包含所有可用职位的分配给候选人。

在 Web 应用程序开发的正常世界中,这真的非常非常容易。但是我正在尝试使用 symfony 的管理生成器来提高我的能力。这是我到目前为止所得到的

应用程序/后端/模块/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 )
    );
  }
}

配置/教义/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]

这行得通!排序 =/

复选框呈现到创建和编辑屏幕,但数据不保存。显然(?)我需要对模型进行一些定制(lib/model/doctrine/candidate.class.php),这就是我失去焦点的地方。我不确定如何从内部获取候选人[职位]数据candidate::save()

  • PHP 5.2.x
  • symfony 1.4.3
4

2 回答 2

1

我假设您首先创建了数据库,然后执行了 build-schema 命令。这样做不会在 M:N 之间正确创建关系。因此,您必须将其添加到您的 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
  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
于 2011-03-08T14:01:26.957 回答
0
//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);
  }

即使使用默认的 generator.yml,它也应该立即工作

于 2011-03-08T15:30:11.577 回答