2013-04-29 22 views
0

等量我嘗試創建一個customWidget具有特殊tablemethod只顯示用戶的預選的選擇,這是形式:自定義部件不顯示的選擇

$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個元素這是正常的,然後如果我嘗試從窗口小部件我只得到1回波的getChoices方法的值!?

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); 

    } 

} 

這是怎麼回事?

創建在該probleme不會出現同樣的形式類似的部件,它個相當相同的代碼...

THX

+1

你有sfWidgetFormDoctrineChoiceWithParams的代碼嗎?沒有看到這個班級是如何工作的,很難說。傳遞的參數是否需要採用鍵/值格式,例如如果傳遞的參數是'parameters'=> array('id'=> $ this-> getObject() - > getId())) – antony 2013-04-29 23:50:52

+0

這裏是類,http://blog.squantin.fr/symfony-1 -4-passer-des-parametres-sur-table_method-dans-un-sfwidgetformdoctrinechoice-4351,我認爲你有正確的想法,但不是將密鑰傳遞給sfWidgetFormDoctrineChoiceWithParams,而是直接從具有key_method屬性的窗體傳遞它。 – krifur 2013-04-30 12:57:45

+0

謝謝,我來看看。它看起來好像這個類有能力返回1條記錄或者它認爲合適的一組記錄。所以我可能會逐步檢查它,看看在這種情況下它是如何做到的。 – antony 2013-04-30 13:01:17

回答

0

我通過設置屬性「key_method」解決這個問題=>'myUniqueId',窗口中的窗口小部件被調用... 原因我在我的表中有兩個主鍵,sfWidgetFormDoctrineChoiceWithParams小部件使用所有結果都是identic的那個作爲數組選擇的鍵,所以數組的大小始終爲1 ...通過將另一個主鍵設置爲getChoices方法的主鍵,我可以得到正確的結果。