Symfony Generator, Doctrine, and M: N Forms

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

Here is a screenshot of ERD from MySQL Workbench

http://dl.dropbox.com/u/180411/stackoverflow/erd.png

Now, moving on from this, let's talk about forms. In the standard symfony generator world, you will have a separate CRUD interface for all three of these tables. However, I do not want to have a CRUD interface for candidate_position.

I want a field with several choices (select a list, an array of flags, whatever) to create CandidatePosition entries as part of the candidate’s actions for actions to create and edit the candidate’s interface.

Code entry

config / doctrine / schema.yml (Note: this is not all schema.yml, but only the tables discussed here)

---
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]

/////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:     ~

///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, , . candidateActions? Basecandidate::save()?

, , .

  • PHP 5.2.x
  • symfony 1.4.3
0
2

Symfony - 1.4.11, .

, doSave() Form :

protected function doSave($con = null)
{
    $existing = $this->object->Position->getPrimaryKeys();
    $values = $this->getValue('candidate_positions');

    $unlink = array_diff($existing, $values);
    $this->object->unlink('Position', array_values($unlink));

    $link = array_diff($values, $existing);
    $this->object->link('Position', array_values($link));

    parent::doSave($con);
}

, , :

public function updateDefaultsFromObject()
{
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['candidate_positions']))
    {
        $this->setDefault('candidate_positions', $this->object->Position->getPrimaryKeys());
    }
}

.

UPDATE

, , 1.4.11 , , , table 'candidPosition' :

  relations:
    Candidate:
      class: Candidate
      local: candidate_id
      foreign: id
      foreignAlias: Candidates
    Position:
      class: Position
      local: position_id
      foreign: id
      foreignAlias: Positions

, .

.

0

Source: https://habr.com/ru/post/1796363/


All Articles