Yii 1.16 ORM查找全部()

iqxoj9l9  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(114)

我如何在Yii ORM findAll中使用CDbCriteria来查找所有Users模型

SELECT DISTINCT u.id, u.slug, IFNULL(MIN(m.id), 0) FROM tbl_users u 
            INNER JOIN tbl_status s ON (s.users_id = u.id AND s.accept > 0) 
            INNER JOIN tbl_message m ON (m.owner_id = s.receiver AND m.users_id = s.users_id) 
            WHERE u.free = 0 AND u.es_index_time <> 0 AND 
            u.paid = 1 AND u.withdrawed = 0 AND ( 
            NOT EXISTS (SELECT 1 FROM tbl_users_prepay up WHERE up.users_id = u.id) OR 
            EXISTS (SELECT 1 FROM tbl_users_prepay up WHERE up.user_redirect_id = u.id)
            )
            GROUP BY u.id
            LIMIT 6;
5jdjgkvh

5jdjgkvh1#

/**
 * Class User - for more details:
 * @see     https://www.yiiframework.com/doc/api/1.1/CDbCriteria
 * or
 * @see     http://www.bsourcecode.com/2013/06/cdbcriteria-in-yii/
 */
class User extends CActiveRecord
{
    // ... user model methods

    public function myCustomMethod()
    {
        $criteria = new CDbCriteria();
        $criteria->alias = 'u';
        $criteria->distinct = true;
        // INNER JOIN
        $criteria->join = 'INNER JOIN tbl_status s ON (s.users_id = u.id AND s.accept > 0)';
        $criteria->join = 'INNER JOIN tbl_message m ON (m.owner_id = s.receiver AND m.users_id = s.users_id)';
        // condition
        $criteria->condition = 'u.free = 0 AND u.es_index_time <> 0 AND
            u.paid = 1 AND u.withdrawed = 0 AND (
                NOT EXISTS (SELECT 1 FROM tbl_users_prepay up WHERE up.users_id = u.id) OR
                EXISTS (SELECT 1 FROM tbl_users_prepay up WHERE up.user_redirect_id = u.id)
            )
        ';
        $criteria->group = 'u.id';
        $criteria->limit = '6';

        return self::model()->findAll($criteria);
    }
}

相关问题