1

这是我在 Form.Class 中的小部件:

$this->widgetSchema['schools'] = new sfWidgetFormChoice(array(
    'choices'        => Doctrine_Core::getTable('school')->getUsersSchools($userId),
    'renderer_class' => 'sfWidgetFormSelectDoubleList', 
    'renderer_options' => array(
        'label_unassociated' => 'Unassociated',
        'label_associated' => 'Associated'
    )));

以上工作正常,但存储的值与上面引用的选择列表无关。我需要将检索到的数组的 id 存储为值。相反,检索到的列表是按时间顺序排列的,id 被忽略。

这是 schoolTable 查询:

 public function getUsersSchools($id){
          $q =Doctrine_Query::create()
             ->select('id')
             ->from('school')
             ->where('user_id = ?', $id)
             ->execute();
         return $q;
     }
4

1 回答 1

1

如果我正确理解您的问题,您想存储相关的学校 ID。改用sfWidgetFormDoctrineChoice小部件,它会开箱即用,因为它使用主键作为 id。

$query = Doctrine_Core::getTable('school')->queryForSelect($userId);
$this->setWidget('schools', new sfWidgetFormDoctrineChoice(array(
  'model' => 'school',
  'query' => $query,
  'multiple' => true,
  'renderer_class' => 'sfWidgetFormSelectDoubleList', 
  'renderer_options' => array(
    'label_unassociated' => 'Unassociated',
    'label_associated' => 'Associated'
  ),
)));
$this->setValidator('schools', new sfValidatorDoctrineChoice(array(
  'model' => 'schoool',
  'query' => $query,
  'multiple' => true,
)));

// in SchoolTable class
public function queryForSelect($userId)
{
  return $this->createQuery('s')
    ->andWhere('s.user_id = ?', $userId)
  ;
}

如果您有适当的模式(我认为学校应该是多对多关联),那么当前 from 应该有一个schools_list字段(在生成的基础中正确定义),然后您可以修改该字段以由sfWidgetFormSelectDoubleList

$this->widgetSchema['schools_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
$this->widgetSchema['schools_list']->setOption('renderer_options', array(
  'label_unassociated' => 'Unassociated',
  'label_associated' => 'Associated'
));
于 2012-11-04T20:32:02.590 回答