yii的relations方法的使用
发布日期:2021-05-10 09:42:09 浏览次数:17 分类:精选文章

本文共 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();

上一篇:YII中的CComponent基础类
下一篇:Yii定制分页,两种分页方式CLinkPager和CListPager

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月07日 14时39分00秒