我尝试用一个特殊的表方法创建一个customWidget,只显示用户的预选选项,这是表单:
$this->widgetSchema['Books_list'] = new MyWidgetFormThematicSelector(array(
'multiple' => true,
'model' => 'Books',
'table_method' => array('method' => 'getOnlySelected', 'parameters' => array($this->getObject()->getId())),
'expanded' => true,
));
方法getOnlySelected:
$q = Doctrine::getTable('BooksAuthors')
->createQuery('ba')
->select('ba.position,ba.name')
->leftJoin('ba.Books b')
->where('ba.BooksAuthors_id = ?', $id);
echo count($q); //return 4
return $q;
此方法返回4个元素,这是正常的,然后如果我尝试从小部件回显getChoices方法的值,我只得到1个回报!
class MyWidgetFormThematicSelector extends sfWidgetFormDoctrineChoiceWithParams {
public function configure($options = array(), $attributes = array())
{
parent::configure($options, $attributes);
}
public function getChoices() {
$choices = parent::getChoices();
echo count($choices); // return 1
return $choices;
}
public function render($name, $value = null, $attributes = array(), $errors = array()) {
return parent::render($name, $value, $attributes, $errors);
}
}
这是怎么回事?
我以相同的形式创建了一个类似的小部件,没有出现问题,它是完全相同的代码…
thx
我通过设置属性'key_method' => 'myUniqueId'来解决这个问题,在小部件被调用的形式中…因为我有两个主键在我的表和sfWidgetFormDoctrineChoiceWithParams小部件使用一个是相同的所有结果作为关键的数组选择,所以数组的大小总是一个…通过设置另一个主键作为getChoices方法的主键,我得到了正确的结果。