Php Yii框架CDBCommand失败
我有两个表,用户和事务Php Yii框架CDBCommand失败,php,mysql,yii,Php,Mysql,Yii,我有两个表,用户和事务 $criteria->with = array('transactions'=>array('together'=>true)); $criteria->addCondition('transactions.user_id=users.id'); $criteria->order = 'SUM(transactions.amount) ASC'; 当我使用这些代码时,PHP向我显示错误 错误: 您可以尝试以下代码: 用户模型 User.ph
$criteria->with = array('transactions'=>array('together'=>true));
$criteria->addCondition('transactions.user_id=users.id');
$criteria->order = 'SUM(transactions.amount) ASC';
当我使用这些代码时,PHP向我显示错误
错误:
您可以尝试以下代码: 用户模型 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);
}
}
您可以尝试以下代码:
用户模型
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);
}
}
请使用错误消息更新您的问题,以便用户可以帮助您。这是我的错误。请使用错误消息更新您的问题,以便用户可以帮助您。这是我的错误。请告诉您,但它会显示给我<代码>活动记录“用户”正在尝试选择无效列“用户id”。注意,该列必须存在于表中,或者是一个带别名的表达式。
请发布您的表结构。用户:id、姓名、姓氏、电子邮件、密码和交易:id、用户id、金额、日期,但它显示给我<代码>活动记录“用户”正在尝试选择无效列“用户id”。注意,该列必须存在于表中,或者是带有别名的表达式。
请发布表结构。用户:id、姓名、姓氏、电子邮件、密码和交易:id、用户id、金额、日期
$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 ($model as $key => $value) {
echo $value->id."<br>";
echo $value->users->name."<br>";
echo $value->amount."<br>";
}