YII框架中的Relational Active Record 关联操作

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) CategoryCategory 有多个(has many) Post.第四种比较特殊点。接着上面的例子:在 post.php 模型类中定义 relations方法  他返回的是一个数组 我们可以这样写:格式:
    1
    'VarName' => array ( 'RelationType' , 'ClassName' , 'ForeignKey' , ...additional options)

    具体方法:

    1
    2
    3
    4
    5
    6
    7
    public 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 ‘,     使用关系名
    ));

    //区分2
    1
    2
    3
    $posts =Post::model()->with( 'comments' )->findAll( array (
         'order' =&gt; '<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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值