
本文共 3429 字,大约阅读时间需要 11 分钟。
Active Record���AR������������������������������������������������������������
relations()
��������������������������������������������������������������������������������������������������������������������� SQL JOIN ��������� ������������
��� Active Record ���������������������������������������������
- BELONGS_TO���������������������������������������B ������ A���������������
Post belongs_to :User
������������������������������������ - HAS_MANY���������������������������������������A ��������� B���������������
User has_many :Posts
��������������������������������� - HAS_ONE������������ HAS_MANY������������������������������������������������
User has_one :Profile
��������������������������������������� - MANY_MANY������������������������������������ AR ������
MANY_MANY
������������������������������������������
������������������
��� AR ������������������ CActiveRecord
��� relations()
���������������������������������������������������������������������������������������������������������������������������
return array( 'reply' => array( self::BELONGS_TO, // ������������ 'BookPostReply', // ��������������� 'postid' // ��������� ), // ������������...);
������������������ SQL ��������������������������������������������� ON��������� on id = postid
��������� BookPostReply
��������������� id
������������ postid
���
��������������������� ON��������������������������������������� 'on' => 't.postid = reply.postid'
������������������
���������������������������������������������
- select���������������������������������
*
���������������������������'select' => ['name', 'age']
- condition���WHERE������������������������������������������������������������������������������������������������
relationName.age > 20
- order���ORDER BY���������������������������������������������������������������������������
relationName.age DESC
- with������������������������������������������������������������������
- joinType������������
LEFT OUTER JOIN
- alias������������������������������������������
null
- params������������������������������
- on��������������� ON������
- index���������������������������������������������������������
- scopes������������������������������
������������
��������������������������������� additionalOptions ���������������������������
- group���GROUP BY������������������������
- having���HAVING������������������������
- limit������������������������������
BELONGS_TO
��� - offset������������������������������
BELONGS_TO
��� - through���������������������������������
HAS_ONE
���HAS_MANY
������������ 1.1.7 ������������
������������
���������������������������������������������������
return array( 'reply' => array( self::BELONGS_TO, // ������������ 'BookPostReply', // ��������������� '', // ��������������������������������������������� 'on' => 't.postid = reply.postid' // ��������� ON������ ), 'imgs' => array( self::HAS_MANY, // ������������ 'LightProImgs', // ��������������� 'pid', // ��������� 'order' => 'imgs.paixu desc', // ��������� ORDER BY������ 'limit' => 3 // limitation��������������� ));
������������ with()
������������������������������������ $posts = Post::model()->with('author', 'categories')->findAll();
��������������������������������� $posts = Post::model()->with('author.profile', 'author.posts', 'categories')->findAll();
发表评论
最新留言
关于作者
