在 CActiveDataProvider 条件中使用 STAT 关系 [英] Using STAT relation in CActiveDataProvider criteria

查看:25
本文介绍了在 CActiveDataProvider 条件中使用 STAT 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器动作

公共函数 actionIndex(){//主管非possono vedere brani OPEN//Gerard (manager) non puo' vedere OPEN/REJECTED/PROPOSED/CLOSED//Editor non puo' vedere APERTO/PROPOSTO/REJECTED se non suo$with = 数组('提案计数','pubblicatoCount','pendingCount','apertoCount','rifiutatoCount','chiusiCount',);$condition = 'proposoCount=1 AND pubblicatoCount=1 AND pendingCount=1 AND rifiutatoCount=1 AND chiusiCount>0';$dataProvider=new CActiveDataProvider('Brano', array('标准'=>数组('with'=>$with,'条件'=>$ 条件,'order'='id DESC',),'分页'=>数组('pageSize'=>5,),));$this->render('index',array('dataProvider'=>$dataProvider,));}

这些是我在 Brano 模型中的关系:

公共函数关系(){//注意:您可能需要调整关系名称和相关的//下面自动生成的关系的类名.返回数组('提案' =>数组(self::HAS_ONE,'BranoVersione','brano_id','condition'=>'stato='.BranoVersione::STATUS_PROPOSED,'order'=>'ultimo_aggiornamento DESC'),'pubblicato' =>数组(self::HAS_ONE,'BranoVersione','brano_id','condition'=>'stato='.BranoVersione::STATUS_PUBLISHED,'order'=>'ultimo_aggiornamento DESC'),'待定' =>数组(self::HAS_ONE,'BranoVersione','brano_id','condition'=>'stato='.BranoVersione::STATUS_PENDING,'order'=>'ultimo_aggiornamento DESC'),'aperto' =>数组(self::HAS_ONE,'BranoVersione','brano_id','condition'=>'stato='.BranoVersione::STATUS_OPEN,'order'=>'ultimo_aggiornamento DESC'),'rifiutato' =>数组(self::HAS_ONE,'BranoVersione','brano_id','condition'=>'stato='.BranoVersione::STATUS_REJECTED,'order'=>'ultimo_aggiornamento DESC'),'chiusi' =>数组(self::HAS_MANY, 'BranoVersione', 'brano_id', 'condition'=>'stato='.BranoVersione::STATUS_CLOSED, 'order'=>'ultimo_aggiornamento DESC'),'proposoCount'=>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 子句'中的未知列 'proposoCount'.执行的 SQL 语句是:SELECT COUNT(DISTINCT t.id) FROM brano t WHERE (propostoCount=1AND pubblicatoCount=1 AND pendingCount=1 AND rifiutatoCount=1 AND chiusiCount>0)

解决方案

我可以看到您在这里尝试做什么,比较查询中的 STAT 关系值对吗?

我遇到了同样的问题,但是 STAT 关系不是这样实现的.它们是在单独的查询中从数据库中提取的,因此只能在 PHP 中使用,而不能在 SQL 本身中使用.

如果您想要使用 STAT 关系的条件,则必须在查询中将其重新定义为完整的子选择(或取决于查询类型的其他内容).

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)

解决方案

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

这篇关于在 CActiveDataProvider 条件中使用 STAT 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆