Using STAT relation in CActiveDataProvider criteria

帅比萌擦擦* 提交于 2020-01-04 23:27:01

问题


This is my controller action

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

And these are my relations in Brano Model:

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

When I try to run it it says:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'propostoCount' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT t.id) FROM brano t WHERE (propostoCount=1 AND pubblicatoCount=1 AND pendingCount=1 AND rifiutatoCount=1 AND chiusiCount>0)


回答1:


I can see what you're trying to do here, compare the STAT relations value in the query right?

I ran into this same issue, but STAT relations aren't implemented that way. They're pulled from the database in a seperate query so are only available for use within PHP not in the SQL itself.

If you want to have a condition using the STAT relationship, you'll have to redefine it inside the query as a full sub-select (or something depending on the query type).



来源:https://stackoverflow.com/questions/7037649/using-stat-relation-in-cactivedataprovider-criteria

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!