3

重複の可能性:
symfony ジェネレーターのフォーム、教義、および M:N 関係

候補、位置、および候補位置の 3 つのテーブルを使用した基本的な M:N セットアップがあります。これは、テキストだけでカラスの足スタイルの ERD に最高の刺し傷を付けたものです。

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

私が達成しようとしているのはこれです:candidateが作成または編集されているとき、フォームには候補者に割り当てることができるすべてのポジションのチェックボックス配列が含まれます。

通常の Web アプリ開発の世界では、これは実に簡単です。しかし、私は symfony の admin ジェネレーターで自分の能力を高めようとしています。ここに私がこれまでに持っているものがあります

アプリ/バックエンド/モジュール/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]

そして、これはうまくいきます!=/の並べ替え

チェックボックスは作成画面と編集画面に表示されますが、データは保存されません。明らかに (?) モデル (lib/model/doctrine/candidate.class.php) をカスタマイズする必要があり、そこに焦点を当てていません。内部から候補者 [positions] データを取得する方法がわかりませんcandidate::save()

  • PHP 5.2.x
  • symfony 1.4.3
4

2 に答える 2

1

最初にdbを作成してから、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 に答える