2016-07-05 4 views
1

두 개의 테이블, 사용자 및 트랜잭션이 있습니다.Yii Framework CDBCommand 실패

$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

+0

사용자가 당신을 도울 수 있도록 오류 메시지와 질문을 업데이트하시기 바랍니다. –

+0

그것의 나의 error.https : //1drv.ms/i/s! ArgubT4lADlRpk6hMEgdck1kz5QC – Hamid

답변

0

.

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 루프와 관계를 사용하여 속성 : -

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

당신을 탱크 그러나 저 쇼. '활성 레코드 "사용자"가 잘못된 열 "user_id"를 선택하려고합니다. 테이블에 열이 있거나 별칭이있는 표현식이어야합니다. ' – Hamid

+0

테이블 구조를 게시하십시오. –

+0

사용자 : 아이디, 이름, 성, 이메일, 비밀번호 및 거래 : id, user_id, 금액, 날짜 – Hamid