2011-08-12 53 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关系有条件,则必须在查询内将其重新定义为完整子选择(或取决于查询类型的东西)。