0

我在 symfony 中设置了 3 个表:

一个翻书表、一个技能表和一个关系表,用于将每个技能 ID 与每个翻书 ID 连接起来。

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

我需要技能是多个选择的复选框,而不是单个选择下拉菜单,所以我正在使用sfWidgetFormSelectCheckbox小部件。

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

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

我不知道如何从选项数组中的技能表中加载选项。我知道它们在某处,因为它们最初包含在默认小部件中。如何将它们拉出到单个复选框输入元素,其中值与技能 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;
    }
}

如果没有收到“未定义的方法”错误,我似乎无法从此文件中调用任何函数。我想这将是一个架构错误?

更新 2

这是正常工作的:

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

1 回答 1

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')
);
于 2012-09-14T09:43:52.973 回答