簡體   English   中英

YII dataProvider關系CDbCriteria

[英]YII dataProvider relation CDbCriteria

我在控制器索引操作中使用CDbCriteria時遇到問題。 我有2節課:活動和與會者。 班級事件有很多與會者,我希望我的事件索引操作僅呈現當前登錄用戶是與會者的事件列表...

  $criteria = new CDbCriteria(array(                    
                            'order'=>'event_date desc',
                            'with'   => array('attendees'=>array('alias'=>'attend')),
                            'condition'=>'attend.uid = ' . Yii::app()->user->id,
                    ));
  $dataProviderAtt=new CActiveDataProvider('Event',
                    array(
                    'criteria'  => $criteria,
                            )
            );

  $this->render('index',array(
        'dataProvider'=>$dataProviderAtt,
    ));

不能在數據庫中顯示以下消息:CDbCommand無法執行SQL語句:SQLSTATE [42000]:語法錯誤或訪問沖突:1066不是唯一的表/別名:'user'

我在參加者班上的關系設置如下:

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( 'user' => array(self::BELONGS_TO, 'YumProfile', 'uid', 'alias'=>'user'), ); }

我試圖更改與會者模型中的defaultscope並定義多個別名:

   public function defaultScope()
{
  static $counter = 0;
   return array(
         'with' => array('user'=>array('alias'=>'user'.($counter++))),
         //'with' => 'user',
      'order' => 'score DESC',
     );
 }

但它似乎無法解決問題,因此我有更多錯誤。 我的標准設置正確嗎? 謝謝您的幫助

你在找

'on' => 'attend.uid = ' . Yii::app()->user->id,

與您與用戶的關系一樣

function relations()
{
    return array(
        'user' => array(
            self::BELONGS_TO,
            'attendees',
            '',
            'on' => 'attendees.uid = ' . Yii::app()->user->id,
        ),
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM