Symfony发生器形式,原则和M:N关系 [英] Symfony Generator Forms, Doctrine, and M:N Relationships

查看:96
本文介绍了Symfony发生器形式,原则和M:N关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的M:N设置,有三个表:候选人,职位和候选人位置。

I have a basic M:N setup with three tables: candidate, position, and candidate_position.

这是从MySQL Workbench的ERD屏幕截图

Here's a screenshot of the ERD from MySQL Workbench

现在,继续谈谈表格。在默认的symfony生成器世界中,您可以为这三个表单独使用一个CRUD界面。但是,我不想为 candidate_position 提供CRUD界面。

Now, moving on from that let's talk about forms. In the default world of symfony generator, you'd have a separate CRUD interface for all three of these tables. However, I don't want to have a CRUD interface for candidate_position.

我想要的是用于创建和编辑候选界面的操作,以包含一个多选择字段(选择列表,复选框数组,任何),将创建CandidatePosition记录作为候选行为的一部分。

What I want, is for the create and edit actions of the Candidate interface to contain a multi-choice field (select list, checkbox array, whatever) that would create the CandidatePosition records as part of the Candidate actions.

代码

config / doctrine / schema.yml (注意:这不是整个schema.yml,而只是这里讨论的表)

---
detect_relations: true
options:
  type: InnoDB

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:
    Positions:
      class: 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:
    Candidates:
      class: Candidate
      refClass: CandidatePosition
      local: position_id
      foreign: candidate_id


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
  indexes:
    fk_candidate_position_candidate1:
      fields: [candidate_id]
    fk_candidate_position_position1:
      fields: [position_id]

apps / backend / modules / candidate / 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 }
        candidate_positions:  {label: Positions}
      list:    
        sort:  [last_name, asc]
      filter:  ~
      form:    
        display:
          "User": [first_name, last_name]
          "Applying For": [candidate_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['candidate_positions'] = new sfWidgetFormDoctrineChoice(
      array( 'multiple' => true, 'model' => 'Position', 'renderer_class' => 'sfWidgetFormSelectCheckbox' )
    );

    $this->validatorSchema['candidate_positions'] = new sfValidatorDoctrineChoice(
      array( 'multiple' => true, 'model' => 'Position', 'min' => 1 )
    );
  }
}

除了实际保存数据。

我明确需要做一些来创建/编辑/删除CandidatePosition记录,但是我'不知道从哪里开始工作。在候选方案?覆盖 Basecandidate :: save()

I clearly need to do something to create/edit/delete the CandidatePosition records, but I'm not sure where to start working. In candidateActions? Override Basecandidate::save()?

让我知道有没有其他数据可能需要看。

Let me know if there's any other data you might need to see.


  • PHP 5.2.x

  • symfony 1.4.3

推荐答案

约10个月前我在连接表中与M:N关系和元数据有类似的麻烦。

About 10 month ago I had similar trouble with M:N relationships and meta-data in the join table.

我发现melikedev的博客条目非常有用!这与您的用例不完全相同,但可能会给您一些提示:

I found those blog entries of melikedev very useful! This is not exactly the same as your use case, but it might give you some hints:

http://melikedev.com/2009/12/09/symfony-w-doctrine-saving-多对多关系/

http://melikedev.com/2009/12/06/symfony-sfformextraplugin-select-double-list-maintain-order/

http://melikedev.com/2010/04/06/symfony-saving-metadata-during-form-save-sort-ids/

这篇关于Symfony发生器形式,原则和M:N关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆