Jam*_*ini 3 entity-relationship yii
我在我的"应用程序"模型中有这个代码,我正在尝试获取所有相关的"Campaign"对象
public function relations()
{
return array(
'campaigns' => array(self::HAS_MANY, 'Campaign', 'appKey'),
);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,广告系列表中的"appKey"字段不是应用程序表的主键,这就是Yii用来尝试查找广告系列的内容.
我的应用程序表的主键是'id',但我希望它使用'appKey'.如何在不将其作为主键的情况下更新我的关系方法?
谢谢.
您可以在Campaign模型中设置命名范围,如下所示:
public function byApplication($appKey)
{
$this->getDbCriteria()->mergeWith(array(
'condition'=>'appKey = :appkey',
'params'=>array('appKey'=>$appKey),
));
return $this;
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
$campaigns = Campaign::model()->byApplication($appKey);
Run Code Online (Sandbox Code Playgroud)
或者,正如Irobb所说,只需在Application模型中设置查询函数,而不是使用实际的Relation,如下所示:
public function getCampaigns() {
return Campaign::model()->findallbyAttributes(array('appKey'=>$this->appKey));
}
Run Code Online (Sandbox Code Playgroud)
并将其称为您的应用程序上的常规关系$model:
$campaigns = $model->campaigns; // remember with Yii you can call getters w/o the 'get'
Run Code Online (Sandbox Code Playgroud)