在公司模型中,我加入了模型用户和business_unit。
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('status',$this->status);
$criteria->condition = "`t`.status = '1'";
$criteria->order = 'created DESC';
$criteria->join = " inner join users on users.company_id = `t`.id";
$criteria->condition = "`t`.status = '1'";
$criteria->order = 'created DESC';
$criteria->join = " inner join business_unit on business_unit.company_id = `t`.id";
有效。但我只获得公司属性。如何获得business_unit
和users
属性以及company
属性
答案 0 :(得分:1)
您应该在用户和business_unit的“公司”模型中创建关系,例如:
1)首先要检查公司模型中是否有两个关系,用户和business_unit:
public function relations()
{
return array(
'users' => array(self::HAS_MANY, 'User', 'company_id'),
'businessUnits' => array(self::HAS_MANY, 'BusinessUnit', 'company_id'),
);
}
2)然后你可以用:
创建标准$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('status',$this->status);
$criteria->with = array('users', 'businessUnits');
$criteria->together = true;
3)拥有所有模型:
$models = Company::model()->findAll($criteria);
然后,对于每个模型,您都拥有用户和业务部门:
foreach($models as $m)
{
// Users
$users = $m->users;
$bu = $m->businessUnit;
}
4)拥有活跃的数据提供者:
$dataProvider = new CActiveDataProvider('Company', array('criteria' => $criteria);