使用sfWidgetFormSelectCheckbox从数据库中提取选择数组

时间:2012-09-13 19:52:39

标签: php widget doctrine symfony-1.4

我在symfony中设置了3个表:

一个flipbook表,一个Skills表和一个关系表,用于将每个技能id与每个flipbook id相关联。

当我构建模型时,symfony正确构建了所有内容,默认情况下会为技能提供一个下拉菜单,其中包含技能表中的所有技能作为选项。您可以选择一个选项,它会创建适当的关系。

我需要将技能作为多个选择的复选框而不是单个选择下拉菜单,因此我使用的是sfWidgetFormSelectCheckbox小部件。

文档说使用“选项”来填充多个选项,如下所示:

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

我不知道如何从choices数组中的技能表中加载选项。我知道它们在某处,因为它们最初包含在默认小部件中。如何将它们拉出到单个复选框输入元素,其中值与skill_id匹配。

一些代码怎么样?

架构:

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);

如果我应该提供进一步的代码或解释,请告诉我。

更新

以下是模型中生成的FlipbookSkillsTable.class:

<?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;
    }
}

如果没有收到“Undefined Method”错误,我似乎无法调用此文件中的任何函数。我想这会是架构错误吗?

更新2

这是正常工作的:

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

1 个答案:

答案 0 :(得分:1)

我通常以这种方式这样做:

添加自定义方法以检索SkillTable.class.php内的技能数组,例如:

  /**
   * 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;
  }

然后在您的表单中,将其称为:

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