Symfony Generator Forms,Doctrine和M:N Relationships

时间:2011-03-04 18:24:04

标签: php symfony1 doctrine symfony-1.4 symfony-forms

  

可能重复:
  Symfony Generator Forms, Doctrine, and M:N Relationships

我有一个基本的M:N设置,有三个表:candidate,position和candidate_position。这是我最好用一个只有文字

的鸦片式ERD
[candiate]-||------|<[candidate_position]>|------||-[position]

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

在Web应用程序开发的正常世界中,这非常非常简单。但我正在努力提高我对symfony管理生成器的能力。这是我到目前为止所得到的

应用/后端/模块/ condidate /配置/ 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 /形式/教义/ 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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

,它也应该可以正常工作