这是我在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;
}
答案 0 :(得分: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)
;
}
如果你有一个正确的架构(我假设学校应该是一个多对多的关联),那么当前的应该有一个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'
));