4:使用数据库的表连接(Yii权威指南)
来源:互联网 发布:2017云计算大会 ppt 编辑:程序博客网 时间:2024/05/29 16:19
我们使用如下所示的实体-关系(ER)图中的数据结构演示此节中的例子
tbl_user
和 tbl_post
),一对一( one-to-one 例如 tbl_user
和 tbl_profile
)和 多对多(many-to-many 例如 tbl_category
和tbl_post
)。在 AR 中,有四种关系:
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
.
'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
VarName
是关系的名字;RelationType
指定关系类型,可以是一下四个常量之一:self::BELONGS_TO
,self::HAS_ONE
, self::HAS_MANY
and self::MANY_MANY
;ClassName
是所关联的 AR 类的名字;ForeignKey
指定关系中使用的外键(一个或多个)。额外的选项可以在每个关系的最后指定(稍后详述)。User
和 Post
类的关系:class Post extends CActiveRecord{ ...... public function relations() { return array( 'author'=>array(self::BELONGS_TO, 'User', 'author_id'), 'categories'=>array(self::MANY_MANY, 'Category', 'tbl_post_category(post_id, category_id)'), ); }} class User extends CActiveRecord{ ...... public function relations() { return array( 'posts'=>array(self::HAS_MANY, 'Post', 'author_id'), 'profile'=>array(self::HAS_ONE, 'Profile', 'owner_id'), ); }}
$author
代表一个 User
AR 实例, 我们可以使用 $author->posts
访问其关联的Post
实例。// 获取 ID 为 10 的帖子 $post=Post::model()->findByPk(10);
// 获取帖子的作者(author): 此处将执行一个关联查询。 $author=$post->author;
N
个帖子的作者,使用这种懒惰式加载将会导致执行 N
个关联查询。 这种情况下,我们应该改为使用 渴求式加载(eager loading)方式。$posts=Post::model()->with('author')->findAll();
上述代码将返回一个 Post
实例的数组。与懒惰式加载方式不同,在我们访问每个 Post
实例中的 author
属性之前,它就已经被关联的 User
实例填充了。 渴求式加载通过 一个 关联查询返回所有帖子及其作者,而不是对每个帖子执行一次关联查询。
我们可以在 with() 方法中指定多个关系名字,渴求式加载将一次性全部取回他们。例如,如下代码会将帖子连同其作者和分类一并取回。
$posts=Post::model()->with('author','categories')->findAll();
$criteria=new CDbCriteria;$criteria->with=array( 'author.profile', 'author.posts', 'categories',);$posts=Post::model()->findAll($criteria);
或者
$posts=Post::model()->findAll(array( 'with'=>array( 'author.profile', 'author.posts', 'categories', ));
select
: 关联的 AR 类中要选择(select)的列的列表。 默认为 '*',即选择所有列。此选项中的列名应该是已经消除歧义的。condition
: 即WHERE
条件。默认为空。此选项中的列名应该是已经消除歧义的。params
: 要绑定到所生成的 SQL 语句的参数。应该以 名-值 对数组的形式赋值。此选项从 1.0.3 版起有效。on
: 即ON
语句。此处指定的条件将会通过AND
操作符附加到 join 条件中。此选项中的列名应该是已经消除歧义的。 此选项不会应用到MANY_MANY
关系中。此选项从 1.0.2 版起有效。order
: 即ORDER BY
语句。默认为空。 此选项中的列名应该是已经消除歧义的。with
: a list of child related objects that should be loaded together with this object. Be aware that using this option inappropriately may form an infinite relation loop.joinType
: type of join for this relationship. It defaults toLEFT OUTER JOIN
.alias
: the alias for the table associated with this relationship. This option has been available since version 1.0.1. It defaults to null, meaning the table alias is the same as the relation name.together
: whether the table associated with this relationship should be forced to join together with the primary table and other tables. This option is only meaningful forHAS_MANY
andMANY_MANY
relations. If this option is set false, the table associated with theHAS_MANY
orMANY_MANY
relation will be joined with the primary table in a separate SQL query, which may improve the overall query performance since less duplicated data is returned. If this option is set true, the associated table will always be joined with the primary table in a single SQL query, even if the primary table is paginated. If this option is not set, the associated table will be joined with the primary table in a single SQL query only when the primary table is not paginated. For more details, see the section "Relational Query Performance". This option has been available since version 1.0.3.join
: the extraJOIN
clause. It defaults to empty. This option has been available since version 1.1.3.group
: theGROUP BY
clause. It defaults to empty. Column names referenced in this option should be disambiguated.having
: theHAVING
clause. It defaults to empty. Column names referenced in this option should be disambiguated. Note: option has been available since version 1.0.1.index
: the name of the column whose values should be used as keys of the array that stores related objects. Without setting this option, an related object array would use zero-based integer index. This option can only be set forHAS_MANY
andMANY_MANY
relations. This option has been available since version 1.0.7.
In addition, the following options are available for certain relationships during lazy loading:
limit
: limit of the rows to be selected. This option does NOT apply toBELONGS_TO
relation.offset
: offset of the rows to be selected. This option does NOT apply toBELONGS_TO
relation.
class User extends CActiveRecord{ public function relations() { return array( 'posts'=>array(self::HAS_MANY, 'Post', 'author_id', 'order'=>'posts.create_time DESC', ), 'profile'=>array(self::HAS_ONE, 'Profile', 'owner_id'), ); }}
$posts=Post::model()->with('comments')->findAll(array( 'order'=>'t.create_time, comments.create_time'));
$user=User::model()->findByPk(1);$posts=$user->posts(array('condition'=>'status=1'));
public function relations(){ return array( 'comments' => array(self::HAS_MANY, 'Comment', 'post_id', 'together'=>false), );}
class Post extends CActiveRecord{ public function relations() { return array( 'commentCount'=>array(self::STAT, 'Comment', 'post_id'), 'categoryCount'=>array(self::STAT, 'Category', 'post_category(post_id, category_id)'), ); }}
commentCount
calculates the number of comments belonging to a post, and categoryCount
calculates the number of categories that a post belongs to. - 4:使用数据库的表连接(Yii权威指南)
- 4:使用数据库的DAO和AR(Yii权威指南)
- Yii 2.0 权威指南(3) 使用数据库
- Yii 2.0 权威指南 (8) 配合数据库使用
- 3:Yii使用表单(Yii权威指南)
- Yii 2.0 权威指南 (4) 使用 Gii 生成代码
- Yii 2.0 权威指南 (2) 使用forms
- 从yii权威指南中学到的
- Yii权威指南
- 5:缓存(Yii权威指南)
- yii 2.0中文 权威指南
- 1:创建Yii(Yii权威指南)
- 2:Yii基础知识(Yii权威指南)
- 6:扩展Yii(Yii权威指南)
- Yii 2.0 权威指南 (5) 应用结构
- Yii 2.0 权威指南 (6) 请求处理
- Yii 2.0 权威指南(7) 关键概念
- [JQ权威指南]NotesForLightBox插件的使用
- 用存储过程产生序列号,解决了匿名用户没有存储过程授权的问题
- 博客整理:人在运维囧途系列学习目录
- 通过百度定位sdk获取实时位置
- 口算异或xor
- 4:使用数据库的DAO和AR(Yii权威指南)
- 4:使用数据库的表连接(Yii权威指南)
- 5:缓存(Yii权威指南)
- ZTree + JQuery +Struts2 +SSH框架
- [C/C++]洗牌算法
- Java发邮件
- DX学习笔记之Surfaces
- getchar函数
- 二分图匹配算法总结
- 递归调用