0
votes

I have setup 3 tables in symfony:

A flipbook table, A Skills table, and a relations table, to connect each skill id with each flipbook id.

WHen I built the model, symfony constructed everything correctly, and by default gave a drop-down menu for the skills, which had all skills from the skills table as options. You can select an option and it creates the appropriate relationships.

I need the skills to be checkboxes for multiple selections rather than a single selection drop-down menu, so I am using the sfWidgetFormSelectCheckbox widget.

The documentation says to use "choices" to populate the multiple selections like so:

$this->widgetSchema['skill_id'] = new sfWidgetFormSelectCheckbox(
  array('choices' => "choice1", "choice2", choice3"), 
  array('class' => 'text size-500'));

I do not know how to load the choices from the skills table in the choices array. I know they are in there somewhere, because they are included initially in the default widget. How do I pull them out to individual checkbox input elements, where the values match the skill_id.

How about some code?

Schema:

Flipbook:
  tableName: flipbook
  inheritance:
    extends: SvaGeneric
    type: concrete
  columns:
    title: { type: string(255) }
    career_associations: { type: clob }
    skills: { type: string(255) }
    skills_associations: { type: clob }
    program: { type: string(255) }
    program_associations: { type: clob }
    draft_id: { type: integer(10) }

FlipbookSkills:
  tableName: flipbook_skills
  columns:
    title: { type: string(255) }
  relations:
    Flipbook:
      foreignAlias: flipbook_skills
      alias: skills
      local: title
      onDelete: SET NULL   

FlipbookSkillRelations:
  tableName: flipbook_skill_relations
  actAs:
    SoftDelete: ~
  options:
  columns:
    flipbook_id: { type: integer(10), notnull: false }
    skill_id: { type: integer(10), notnull: false }
  relations:
    Flipbook:
      foreignAlias: flipbook_skills_flipbook
      alias: flipbook
      local: flipbook_id
      onDelete: CASCADE
    FlipbookSkills:
      foreignAlias: flipbook_skills_skills
      alias: flipbookskills
      local: skill_id
      onDelete: CASCADE

FlipbookSkillsRelationsForm.class.php:

$this->widgetSchema['flipbook_id'] = new sfWidgetFormInputText(array(), array('class' => 'text size-500'));

$this->widgetSchema['skill_id'] = new sfWidgetFormSelectCheckbox(array('choices' => "**WHAT GOES HERE??**"), array('class' => 'text size-500'));


$useFields = array(
  'flipbook_id',
  'skill_id',
);

$this->useFields($useFields);

Let me know if I should provide further code or explanation.

UPDATE

Here is the FlipbookSkillsTable.class generated in the model:

<?php

/**
 * FlipbookSkillsTable
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class FlipbookSkillsTable extends Doctrine_Table
{
    /**
     * Returns an instance of this class.
     *
     * @return object FlipbookSkillsTable
     */
    public static function getInstance()
    {
        return Doctrine_Core::getTable('FlipbookSkills');
    }

    public function retrieveForFilter()
    {
      $res = $this->createQuery('s')
        ->select('s.id, s.name')
        ->orderBy('s.name ASC')
        ->execute(array(), Doctrine_Core::HYDRATE_NONE);

      // if you want an empty line
      $rets = array('' => '');
      foreach ($res as $ret)
      {
        $rets[$ret[0]] = $ret[1];
      }

      return $rets;
    }

    public function getAllFlipbookSkills() {
        $allSkills = Doctrine_Query::create()
            ->from('FlipbookSkills')
            ->orderBy('title ASC')
            ->execute();
    return $allSkills;
    }
}

I cannot seem to call any function from this file without getting the "Undefined Method" error. I guess this would be a schema error?

UPDATE 2

This is what is working correctly:

$this->widgetSchema['skill_id'] = new sfWidgetFormSelectCheckbox(array('choices' => Doctrine::getTable('FlipbookSkills')->getFlipbookskills()), array('class' => 'text size-500'));
1

1 Answers

1
votes

I usually do it in this way:

Add a custom method to retrieve an array of skill inside the SkillTable.class.php, something like:

  /**
   * Retrieve array for display the select list for filter
   *
   * @return array
   */
  public function getFlipbookskills()
  {
    $res = $this->createQuery('s')
      ->select('s.id, s.name')
      ->orderBy('s.name ASC')
      ->execute(array(), Doctrine_Core::HYDRATE_NONE);

    // if you want an empty line
    $rets = array('' => '');
    foreach ($res as $ret)
    {
      $rets[$ret[0]] = $ret[1];
    }

    return $rets;
  }

Then in your form, call it like :

$this->widgetSchema['skill_id'] = new sfWidgetFormSelectCheckbox(
  array('choices' => Doctrine::getTable('FlipbookSkills')->getFlipbookskills()), 
  array('class' => 'text size-500')
);