最新消息: 新版网站上线了!!!

Yii2 Active Record

这篇文章我们来看看在Yii2之中的Active Record,为啥要将Active Record单独出来说呢?因为个人认为这是Yii(不管是Yii1.1还是Yii2)最强大的一部分功能之一,何况又遇上在Yii2中其实对Active Record的改进还是比较多的,所以我们就通过这篇文章来瞅瞅Yii2的Active Record新特性。

1.支持更多的数据库

Yii2的Active Record首先值得称道的一点就是它已经可以支持更多的数据库,包括一些NoSQL类型的数据库如MongoDB,还有一些流行的存储后端如: elasticsearchredisSphinx search也得到很好的支持,现在真是随意你怎么玩数据了。因为我们在想换一个存储后端的时候可以轻松地在配置文件里切换过来,完全不用去修改Active Record的代码,酷毙!

2.在Yii2中使用Active Record查询数据

在Yii2中使用Active Record的时候,第一个最直观的感受可能就是model()调用已经跟Yii1.1变得不同了,可以说所有的查询函数都源自 find() 或 findBySql()这两个函数,所以像在Yii1.1中的Post::model()->findAll()等函数就没有了。这里对Active Record的查询模块介绍都是很简单的,当然也可能包含一些个人的主观因素在里面,不说废话,如果你能看看下面这些来自官方文档中Active Record介绍的代码片段你就会变得很清晰。

<?php
    // 检索出所有的customer然后根据id排序:
    $customers = Customer::find()
    ->where(['status' => Customer::STATUS_ACTIVE])
    ->orderBy('id')
    ->all();

    // 查找出id为1的customer(注意时一个):
    $customer = Customer::find()
        ->where(['id' => 1])
        ->one();

    // 返回 *active* customers的数量:
    $count = Customer::find()
        ->where(['status' => Customer::STATUS_ACTIVE])
        ->count();

    // 找到所有将id作为索引的customer:
    $customers = Customer::find()->indexBy('id')->all();
    // $customers array is indexed by customer IDs

    // 用查询语句查找所有的customer:
        $sql = 'SELECT * FROM customer';
        $customers = Customer::findBySql($sql)->all();

还有一个值得注意的地方是,在Yii1.1中使用的findByPk()find()在Yii2中有了新的替代者,

<?php
    // 查找一个id为1的customer:
    $customer = Customer::findOne(1);

    // 找到id为1的 *active* customer:
    $customer = Customer::findOne([
    'id' => 1,
    'status' => Customer::STATUS_ACTIVE,
    ]);

    // 查找出id为1,或2,或3的所有customer:
    $customers = Customer::findAll([1, 2, 3]);

    // 找到状态为 "deleted" 的customer:
    $customer = Customer::findAll([
    'status' => Customer::STATUS_DELETED,
    ]);

3.关联(Relations)

在Yii2中,不再存在relations()函数,而是使用getters的方式返回一个ActiveQuery对象

class Post extends yii/db/ActiveRecord{

   public function getComments()
   {
      return $this->hasMany('Comment', array('post_id' => 'id'));
   }
}

但是关联性还是可以使用类似$game->players,不过在Yii2你可以根据自己的情况来自定义你的查询环境(查询语句),比如:

$comments = $post->getComments()->
		
        
		-->
		
        

转载请注明:谷谷点程序 » Yii2 Active Record