2012-06-28 28 views
0

在我的一個表單中,我希望有一個下拉列表(sfWidgetFormChoice),其中通過在數據庫上執行查詢來動態生成選項。如何使用數據庫查詢的結果動態填充Symfony選擇小部件

爲了更精確一點,我將列出表中所有的版本。查詢看起來是這樣的:

select distinct version from mytable order by version desc 

我有什麼事這麼遠,但不起作用:

class myForm extends sfForm 

$query = "select distinct version from mytable order by version desc"; 

$versions = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query); 

public function configure() 

$this->setWidgets(array('version' => new sfWidgetFormChoice(array('choices' => self::$versions)))); 

編輯:

Thansk傢伙對你的答案!非常感激!

無論如何,您的解決方案基於具有表模型。我寧願直接擁有PDO,因爲它速度更快。

Symfony Documentation我找到了我在「使用原始SQL查詢」下查找的內容。

所以我伸出我的表單與此落得:

class myForm extends sfForm 
{ 
    public function getVersions() 
    { 
    $connection = Doctrine_Manager::connection(); 
    $query  = "select distinct version from mytable order by version desc"; 
    $statement = $connection->prepare($query); 
    $statement->execute(); 
    $resultset = $statement->fetchAll(PDO::FETCH_COLUMN, 0); 

    return $resultset; 
    } 

    public function configure() 
    { 
    $this->setWidgets(array('version' => new sfWidgetFormChoice(array('choices' => self::getVersions())))); 
    } 
} 

由於這是我的下拉列表的結果得到妥善裏裝的是我的表,耶!但我也得到警告:

Warning: Invalid argument supplied for foreach() in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 196

Warning: join() [function.join]: Invalid arguments passed in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 141

Warning: Invalid argument supplied for foreach() in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 196

Warning: Invalid argument supplied for foreach() in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/debug/sfDoctrineConnectionProfiler.class.php on line 117

任何想法我在做什麼錯在這裏???奇怪的下拉菜單看起來不錯。

回答

0

我終於找到了我被自己尋找解決方案。這直接訪問數據庫。我不再使用FETCH_COLUMN的原因是,這將創建一個數組,其中的鍵是從0開始的Id,並且該值是查詢本身返回的任何值。相反,我想擁有的關鍵和價值是什麼查詢提供。這就是爲什麼查詢現在提供了兩個相同的列,然後是FETCH_KEY_PAIR。

如果其他人對我如何解決這個問題感興趣,這裏是我的代碼,正確地填充下拉列表並導致沒有錯誤。

class myForm extends sfForm 
{ 
    public function getVersions() 
    { 
    $connection = Doctrine_Manager::getInstance()->getCurrentConnection()->getDBh(); 
    $query  = "select distinct version as key, version as value from mytable order by version desc"; 
    $statement = $connection->prepare($query); 
    $statement->execute(); 
    $resultset = $statement->fetchAll(PDO::FETCH_KEY_PAIR); 

    return $resultset; 
    } 

    public function configure() 
    { 
    $this->setWidgets(array('version' => new sfWidgetFormChoice(array('choices' => self::getVersions())))); 
    } 
} 
+0

這是一個非常糟糕的形式和小部件的使用,它根本不是MVC。帶有自定義查詢的'sfWidgetFormDoctrineChoice'絕對是您的選擇。 – DevAntoine

0

您可以使用sfWidgetFormDoctrineChoice

public function configure(){ 

$this->widgetSchema['version'] = new sfWidgetFormDoctrineChoice(array('model' => 'YourModel', 'query' => Doctrine::getTable('YurModel')->getYourDataQuery(), 'add_empty' => true)); 

} 

而在你modelTable.class.php

public function getYourDataQuery() 
    { 
      //Your query 

      $q = $this->createQuery('a');   

     return $q; 
    } 
1

使用table_method選項sfWidgetFormDoctrineChoice這樣的:

在您的形式班級做到這一點:

$this->setWidgets(array('version' => new sfWidgetDoctrineChoice(array('model' => 'Version', 'table_method' => 'getData'))); 

然後在versionTable.class.php文件中創建一個getData()(你可以調用這個東西)函數,返回對象的集合:

public function getData() { 
    $this->getInstance()->createQuery() 
      ->orderBy('version desc') 
      ->execute(); 
} 
+1

您需要從..'table_method'=>'getData()'中刪除「()」。它必須是''table_method'=>'getData' – Lashae

0

謝謝先生,我真的很感謝你的解決方案。 你已經救了我的一天!

以防萬一誰有類似的問題。 使用該解決方案時遇到了PDO找不到問題。 (FatalErrorException:錯誤:類「....... \控制器\ PDO」中未找到) 原因是: https://groups.google.com/forum/#!topic/symfony2/mR22XXKPQrk 「你是濫用PHP命名空間當你在一個命名空間代碼,使用PDO 。意味着一個相對於當前命名空間的類名稱,你有解決你的問題的方法:使用完全限定的類名稱,例如 \ PDO或添加一個use語句來導入當前命名空間中的類。

所以在PDO ::之前添加'\'後,該解決方案完美無缺。

謝謝!

相關問題