具有自定义值的CActiveDataProvider

时间:2015-10-03 14:51:16

标签: php sql yii yii-cactiverecord

在我的Yii项目中,我需要按一些自定义值搜索和排序数据。我有'users'表,我需要每个User实例都有month_profit属性,该属性应该结合SQL数据+我自己的大量计算。目前我的用户模型中有:

public $month_profit;

public function search($pageSize = 10, $defaultOrder = '`t`.`reg_date` DESC')
{
    $criteria = new CDbCriteria;
    $criteria->with = array('money');
    $requests_table = Requests::model()->tableName();
    $requests_count_sql = "(SELECT COUNT(*) FROM $requests_table rt WHERE rt.partner_id = t.id) ";

    $referrals_table = Referrals::model()->tableName();
    $referrals_count_sql = "(SELECT COUNT(*) FROM $referrals_table reft WHERE reft.user_id = t.id) ";
    $referrals_payed_sql = "(SELECT COUNT(*) FROM $referrals_table reft WHERE reft.user_id = t.id AND reft.status = 'Оплачено') ";
    //$month profit_sql = ???;

    $criteria->select = array(
        '*',
        $requests_count_sql . "as requests_count",
        $referrals_count_sql . "as referrals_count",
        $referrals_payed_sql . "as referrals_payed_count",
        $month_profit_sql . "as month_profit",
    );

    $criteria->compare($requests_count_sql, $this->requests_count);
    $criteria->compare($referrals_count_sql, $this->referrals_count);
    $criteria->compare($referrals_payed_sql, $this->referrals_payed_count);
    $criteria->compare($month_profit_sql, $this->month_profit);

    $criteria->compare('t.id', $this->id);
    $criteria->compare('t.reg_date', $this->reg_date, true);
    $criteria->compare('username', $this->username, true);
    $criteria->compare('password', $this->password, true);
    $criteria->compare('site', $this->site, true);
    $criteria->compare('status', $this->status, true);

    return new CActiveDataProvider(get_class($this), array(
        'criteria' => $criteria,
        'pagination' => array( 'pageSize' => $pageSize ),
        'sort' => array(
            'defaultOrder' => $defaultOrder,
            'attributes' => array(
                'id' => array(
                    'asc' => '`t`.`id` ASC',
                    'desc' => '`t`.`id` DESC',
                ),
                'email' => array(
                    'asc' => '`t`.`email` ASC',
                    'desc' => '`t`.`email` DESC',
                ),
                'requests_count' => array(
                    'asc' => 'requests_count ASC',
                    'desc' => 'requests_count DESC',
                ),
                'referrals_count' => array(
                    'asc' => 'referrals_count ASC',
                    'desc' => 'referrals_count DESC',
                ),
                'referrals_payed_count' => array(
                    'asc' => 'referrals_payed_count ASC',
                    'desc' => 'referrals_payed_count DESC',
                ),
                'money' => array(
                    'asc' => 'money.profit',
                    'desc' => 'money.profit DESC',
                ),
                'fullProfit' => array(
                    'asc' => 'money.full_profit',
                    'desc' => 'money.full_profit DESC',
                ),
                '*',
            ),
        )
    ));
}

E.g。我的用户模型中有关系:

public function relations()
{
    return array(
        'clients' => array( self::HAS_MANY, 'Referrals', 'user_id'),

假设我的month_profit将是相同的:过去30天内注册的用户clients的数量* 150.我需要以某种方式将此数据传递给search()并创建CDbCriteria以对用户进行排序按month_profit。这是真的吗? :)我应该创建另一个函数来计算所有内容然后传递给search()吗?到目前为止,我所有的尝试都是失败的。

1 个答案:

答案 0 :(得分:0)

根据本指南http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview

,我认为您需要以下内容

首先在用户模型的关系中进行统计查询:

'month_profit' => array(self::STAT, 'User', 'id', 'select'=>'COUNT(*) * 150', 'condition'=>'DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= reg_date'),

然后标记&#34; month_profit&#34;属性在规则中的搜索方案中是安全的。

public function rules() {
    return array(

        ...

        array('username, ... , month_profit', 'safe', 'on' => 'search' ),   
    );
}

在User search()方法中添加所需的所有内容:

$user_table = User::model()->tableName();
$month_profit_sql = "(SELECT COUNT(*) FROM user_table WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= reg_date)";

$criteria->select = array(
    '*',
    $requests_count_sql . "as requests_count",
    $referrals_count_sql . "as referrals_count",
    $referrals_payed_sql . "as referrals_payed_count",
    $month_profit_sql . "as month_profit",
);

...
$criteria->compare($month_profit_sql, $this->month_profit);

return new CActiveDataProvider(get_class($this), array(
    'criteria' => $criteria,
    'pagination' => array( 'pageSize' => $pageSize ),
    'sort' => array(
        'defaultOrder' => $defaultOrder,
        'attributes' => array(
            'id' => array(
                'asc' => '`t`.`id` ASC',
                'desc' => '`t`.`id` DESC',
            ),

            ...

            'month_profit' => array(
                'asc' => 'month_profit ASC',
                'desc' => 'month_profit DESC',
            ),

            '*',
        ),
    )
));

最后,修改您的网格:

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'username',

        ... ,

        'month_profit',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

如果有效,请告诉我。

EDIT 如果那不是&#39;工作尝试没有统计查询。