php - Yii 框架 CDB 命令失败

标签 php mysql yii

我有两个表,用户和交易。

$criteria->with = array('transactions'=>array('together'=>true));
$criteria->addCondition('transactions.user_id=users.id');
$criteria->order = 'SUM(transactions.amount) ASC';

当我使用这段代码时,PHP 会显示错误。

错误: enter image description here

最佳答案

你可以试试这段代码:

用户模型。

User.php

<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property string $id
 * @property string $name
 * @property string $lastname
 * @property string $email
 * @property string $password
 * @property string $created_by
 * @property string $created_date
 * @property string $updated_by
 * @property string $updated_date
 */
class Users extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, lastname, email, password, created_by, created_date', 'required'),
            array('name, lastname', 'length', 'max'=>255),
            array('email, password', 'length', 'max'=>50),
            array('created_by, updated_by', 'length', 'max'=>10),
            array('updated_date', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name, lastname, email, password, created_by, created_date, updated_by, updated_date', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    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(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'lastname' => 'Lastname',
            'email' => 'Email',
            'password' => 'Password',
            'created_by' => 'Created By',
            'created_date' => 'Created Date',
            'updated_by' => 'Updated By',
            'updated_date' => 'Updated Date',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('lastname',$this->lastname,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('created_by',$this->created_by,true);
        $criteria->compare('created_date',$this->created_date,true);
        $criteria->compare('updated_by',$this->updated_by,true);
        $criteria->compare('updated_date',$this->updated_date,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Users the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

Transactions.php

您可以添加更多与 select 语句相关的字段。

<?php

/**
 * This is the model class for table "transactions".
 *
 * The followings are the available columns in table 'transactions':
 * @property string $id
 * @property string $user_id
 * @property string $amount
 * @property string $date
 * @property string $created_by
 * @property string $created_date
 * @property string $updated_by
 * @property string $updated_date
 */
class Transactions extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'transactions';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, amount, date, created_by, created_date', 'required'),
            array('user_id, amount', 'length', 'max'=>255),
            array('created_by, updated_by', 'length', 'max'=>10),
            array('updated_date', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, user_id, amount, date, created_by, created_date, updated_by, updated_date', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    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(
            'users' => array(self::BELONGS_TO, 'Users', 'user_id', 'joinType' => 'INNER JOIN', 'select' => ('id, name, lastname, email, password')),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'user_id' => 'User',
            'amount' => 'Amount',
            'date' => 'Date',
            'created_by' => 'Created By',
            'created_date' => 'Created Date',
            'updated_by' => 'Updated By',
            'updated_date' => 'Updated Date',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id,true);
        $criteria->compare('user_id',$this->user_id,true);
        $criteria->compare('amount',$this->amount,true);
        $criteria->compare('date',$this->date,true);
        $criteria->compare('created_by',$this->created_by,true);
        $criteria->compare('created_date',$this->created_date,true);
        $criteria->compare('updated_by',$this->updated_by,true);
        $criteria->compare('updated_date',$this->updated_date,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Transactions the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

您可以使用此条件获得输出。

$criteria = new CDbCriteria;
$criteria->select = 'users.id,SUM(t.amount) as amount';
$criteria->with = 'users';
$criteria->together = true;
$criteria->group = 'users.id';
$criteria->order = 'amount ASC';
$model  = Transactions::model()->findAll($criteria);

echo "<pre>";
print_r($model);
exit;

在您可以使用与 foreach 循环的关系访问用户模型属性之后 users

例如:-

foreach ($model as $key => $value) {
    echo $value->id."<br>";
    echo $value->users->name."<br>";
    echo $value->amount."<br>";
}

关于php - Yii 框架 CDB 命令失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38204685/

相关文章:

php - 从 PHP SDK 获取 AWS IAM 凭证

php - 我们如何计算 aaray 的结束位置?

php - CodeIgniter 中一致的主题和内容

mysql - Perl DBI 执行不维护 MySQL 存储过程结果

php - 从模型实例 YII 获取模型名称

PHP mysql连接?

mysql - 为什么 SQL 查询通过 PHPMySQLAdmin 而不是通过 mysql_query?

php - INSERT 中的 bindValue 不起作用

jquery - 查看文件中的错误消息

php - yii, 如何更改 Cmenu 上的文本颜色