2012-04-16 35 views
1

我想在數據提供程序如何在yii的dataprovider中顯示記錄?

select user.username ,company_user.* from user left join company_user on company_user.user_id=user.id where company_user.company_id=".$id 

如何CActiveDataProvider

寫plz幫助事先我 謝謝使用此查詢...

我有3個表
company_user - > id,company_id,user_id,first_name,last_name
company - > id,name_of_company
用戶 - > ID,用戶名,密碼

我想從提前company_user +用戶名來自用戶

感謝所有記錄... :)

我想名單在CGridView中

在我的用戶模型中我已經寫下這種類型的關係

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(
      'company_user'   =>array(self::HAS_ONE,'CompanyUser','user_id','select' =>array('first_name','status'), 
              'with'=>array('company'=>array(self::BELONGS_TO, 'Company', 'company_id','joinType' => 'INNER JOIN')), 
            ), 
      'company_user_rel_only' =>array(self::HAS_ONE,'CompanyUser','user_id','select' =>array('first_name', 'last_name')), 

    } 
+0

如果使用Gii創建模型,則應該已建立關係。嘗試使用它們。 – adamors 2012-04-16 09:11:37

+0

plz查看我更新的問題.... @Örs – User1988 2012-04-16 09:13:41

+0

hello @Örs,現在請說說如何使用這種關係? – User1988 2012-04-16 09:21:40

回答

1

與誼,你應該使用模式的方法,這樣的事情應該工作:

$criteria = new CDbCriteria; 
$criteria->with = 'user'; 
$criteria->compare('company_id', $id); //if error due to similar column name change it to t.company_id 
$dataProvider = new CActiveDataProvider('CompanyUser', array(
        'criteria' => $criteria, 
       )); 

我asume CompanyUser之間的關係的名稱和用戶是用戶,如果沒有,用屬性改變到正確的名字。此外,如果您想訪問用戶屬性,則可以訪問這些用戶屬性對象,然後使用→用戶名→用戶名