YII关联操作:通过Relational Active Record我们可以很方便的从数据获获取数据,例如我们有两个表一个 post 文章表 category 文字分类表。我们在模型中可以只操作post 通过它定义的relations() 获取此文字的category 当然在 post中我们需要定义category的id了。
YII 支持四种类型的关系:
-
BELONGS_TO
(属于): 如果表 A 和 B 之间的关系是一对多,则 表 B 属于 表 A (例如Post
属于User
); -
HAS_MANY
(有多个): 如果表 A 和 B 之间的关系是一对多,则 A 有多个 B (例如User
有多个Post
); -
HAS_ONE
(有一个): 这是HAS_MANY
的一个特例,A 最多有一个 B (例如User
最多有一个Profile
); -
MANY_MANY
: 这个对应于数据库中的 多对多 关系。 由于多数 DBMS 不直接支持 多对多 关系,因此需要有一个关联表将 多对多 关系分割为 一对多 关系。 在我们的示例数据结构中,tbl_post_category
就是用于此目的的。在 AR 术语中,我们可以解释MANY_MANY
为BELONGS_TO
和HAS_MANY
的组合。 例如,Post
属于多个(belongs to many)Category
,Category
有多个(has many)Post
.第四种比较特殊点。接着上面的例子:在 post.php 模型类中定义 relations方法 他返回的是一个数组 我们可以这样写:格式:1'VarName'
=>
array
(
'RelationType'
,
'ClassName'
,
'ForeignKey'
, ...additional options)
具体方法:
1234567public
function
relations()
{
return
array
(
'category'
=>
array
(self::BELONGS_TO,
'category'
,
'cat_id'
),
);
}
在查询时 我们可以这样做
1$posts
=Post::model()->with(
'category'
)->find();
在使用 在视图中获取category 可以这样获取
$posts->category->title;
2、定义many_many 是 在外键的那个位置使用第三个表来定义的 ;
关于具体使用方法查看 中文文档
注意:
1、关于关联操作 两个表中有重复的字段 例如create_time
当在使用 CDbCriteria 进行查询时会出现错误 所以当两个表有相同字段需要进行关联操作时需要进行 区分 本表使用 t区分 其他表使用 我们定义的 关系的名字 example:
$criteria=new CDbCriteria(array(‘select’=>’id,date,title,content,user_id,comment_count’,‘order’=>’t.create_time desc ‘, // 本表中使用 t// ‘order’=>’author.create_time desc ‘, 使用关系名));
//区分2123$posts
=Post::model()->with(
'comments'
)->findAll(
array
(
'order'
=>
'<span style="color: #ff0000;">t.create_time</span>, comments.create_time'
));
2、在定义关系时 也可以指定需要查询的数据以及其他一些额外的信息:'category'=>array(self::BELONGS_TO,'category','cat_id','select'=>'title')
额外的参数如下:
- ‘select': string|array, a list of columns to be selected. Defaults to ‘*’, meaning all columns. Column names should be disambiguated if they appear in an expression (e.g. COUNT(relationName.name) AS name_count).
- ‘condition': string, the WHERE clause. Defaults to empty. Note, column references need to be disambiguated with prefix ‘relationName.’ (e.g. relationName.age>20)
- ‘order': string, the ORDER BY clause. Defaults to empty. Note, column references need to be disambiguated with prefix ‘relationName.’ (e.g. relationName.age DESC)
- ‘with': string|array, a list of child related objects that should be loaded together with this object. Note, this is only honored by lazy loading, not eager loading.
- ‘joinType': type of join. Defaults to ‘LEFT OUTER JOIN’.
- ‘alias': the alias for the table associated with this relationship. It defaults to null, meaning the table alias is the same as the relationname.
- ‘params': the parameters to be bound to the generated SQL statement. This should be given as an array of name-value pairs.
- ‘on': the ON clause. The condition specified here will be appended to the joining condition using the AND operator.
- ‘index': the name of the column whose values should be used as keys of the array that stores related objects. This option is only available to HAS_MANY and MANY_MANY relations.
- ‘scopes': scopes to apply. In case of a single scope can be used like ‘scopes’=>’scopeName’, in case of multiple scopes can be used like ‘scopes’=>array(‘scopeName1′,’scopeName2′). This option has been available since version 1.1.9.
关联操作获取数据:
yii通过关联操作获取数据:
如果是一对一:A->VarName如果是一对多:A->VarName->name如果是多对多:$data= As->VarName //数组foreach($data as $val){$tags[] = $val -> content;}userStr = implode(‘, ‘, $tags);