Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony1 当multiple等于true时,如何在使用sfWidgetFormChoice时将选项数组_键存储为值_Symfony1_Symfony 1.4 - Fatal编程技术网

Symfony1 当multiple等于true时,如何在使用sfWidgetFormChoice时将选项数组_键存储为值

Symfony1 当multiple等于true时,如何在使用sfWidgetFormChoice时将选项数组_键存储为值,symfony1,symfony-1.4,Symfony1,Symfony 1.4,下面是我的表单中的小部件。类: $this->widgetSchema['schools'] = new sfWidgetFormChoice(array( 'choices' => Doctrine_Core::getTable('school')->getUsersSchools($userId), 'renderer_class' => 'sfWidgetFormSelectDoubleList', 'renderer_opt

下面是我的表单中的小部件。类:

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

如果我正确理解您的问题,您希望存储相关的学校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\u list
字段(在生成的基本from中正确定义),然后您可以修改该字段,使其由
sfWidgetFormSelectDoubleList
呈现:

$this->widgetSchema['schools_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
$this->widgetSchema['schools_list']->setOption('renderer_options', array(
  'label_unassociated' => 'Unassociated',
  'label_associated' => 'Associated'
));

如果我正确理解您的问题,您希望存储相关的学校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\u list
字段(在生成的基本from中正确定义),然后您可以修改该字段,使其由
sfWidgetFormSelectDoubleList
呈现:

$this->widgetSchema['schools_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
$this->widgetSchema['schools_list']->setOption('renderer_options', array(
  'label_unassociated' => 'Unassociated',
  'label_associated' => 'Associated'
));

什么类型的变量返回
getUsersSchools
?ID数组,集合?我相信这是一个集合。我已经更新了我的问题以向您显示查询。您使用的表单是一个生成表单,默认情况下有一个
学校
字段?是的,它是一个生成表单,有一个名为“学校”的默认字段。什么样的变量返回
getUsersSchools
?ID数组,集合?我相信这是一个集合。我已经更新了我的问题以向您显示查询。您使用的表单是一个生成表单,默认情况下有一个
schools
字段?是的,它是一个生成表单,有一个名为“schools”的默认字段。