2011-08-12 61 views
2

這是我的控制器操作使用STAT關係

public function actionIndex() 
    { 

     //Supervisor non possono vedere brani OPEN 
     //Gerard (manager) non puo' vedere OPEN/REJECTED/PROPOSED/CLOSED 
     //Editor non puo' vedere APERTO/PROPOSTO/REJECTED se non suo 


     $with = array(
      'propostoCount', 
      'pubblicatoCount', 
      'pendingCount', 
      'apertoCount', 
      'rifiutatoCount', 
      'chiusiCount', 
     ); 


     $condition = 'propostoCount=1 AND pubblicatoCount=1 AND pendingCount=1 AND rifiutatoCount=1 AND chiusiCount>0';   


     $dataProvider=new CActiveDataProvider('Brano', array(
      'criteria'=>array(    
       'with'=>$with, 
       'condition'=>$condition, 
       'order'=>'id DESC', 
      ), 

      'pagination'=>array(
       'pageSize'=>5, 
      ), 

     )); 

     $this->render('index',array(
      'dataProvider'=>$dataProvider, 
     )); 
    } 

而這些都是我在Brano型號關係:

public function relations() 
    { 
     // NOTE: you may need to adjust the relation name and the related 
     // class name for the relations automatically generated below. 
     return array(
      'proposto' => array(self::HAS_ONE, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_PROPOSED, 'order'=>'ultimo_aggiornamento DESC'), 
      'pubblicato' => array(self::HAS_ONE, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_PUBLISHED, 'order'=>'ultimo_aggiornamento DESC'), 
      'pending' => array(self::HAS_ONE, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_PENDING, 'order'=>'ultimo_aggiornamento DESC'), 
      'aperto' => array(self::HAS_ONE, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_OPEN, 'order'=>'ultimo_aggiornamento DESC'), 
      'rifiutato' => array(self::HAS_ONE, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_REJECTED, 'order'=>'ultimo_aggiornamento DESC'), 
      'chiusi' => array(self::HAS_MANY, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_CLOSED, 'order'=>'ultimo_aggiornamento DESC'), 

      'propostoCount'=>array(self::STAT, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_PROPOSED), 
      'pubblicatoCount'=>array(self::STAT, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_PUBLISHED), 
      'pendingCount'=>array(self::STAT, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_PENDING), 
      'apertoCount'=>array(self::STAT, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_OPEN), 
      'rifiutatoCount'=>array(self::STAT, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_REJECTED), 
      'chiusiCount'=>array(self::STAT, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_CLOSED), 
     ); 
    } 

當我嘗試運行它,它說:

CDbCommand無法執行SQL語句:SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'propostoCount'。執行的SQL語句是:SELECT COUNT(DISTINCT tid。)FROM branot WHERE(propostoCount = 1 AND pubblicatoCount = 1 AND pendingCount = 1 AND rifiutatoCount = 1 AND chiusiCount> 0)

+0

我不認爲你很理解關係是如何工作的,你可能想看看這裏的文檔(http://www.yiiframework.com/doc/guide/1.1/en/database.arr)。具體來說,請參閱關於使用與參數[命名範圍](http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes)關係的章節可能適用於你想要做的事情。你不會在「condition」屬性中使用關係名稱,如果它是相同的連接,我不相信它會使用多個「with」。 (使用「with」提前加入。) – ldg

回答

3

我可以看到你在做什麼試圖在這裏做,比較STAT關係值在查詢中的權利?

我遇到了同樣的問題,但STAT關係不是這樣實現的。它們在單獨的查詢中從數據庫中提取,因此只能在PHP中使用,而不能在SQL本身中使用。

如果要使用STAT關係有條件,則必須在查詢內將其重新定義爲完整子選擇(或取決於查詢類型的東西)。