这篇文章我们来看看在 Yii2 之中的 Active Record,为啥要将 Active Record 单独出来说呢?因为个人认为这是 Yii(不管是 Yii1.1 还是 Yii2)最强大的一部分功能之一,何况又遇上在 Yii2 中其实对 Active Record的改进还是比较多的,所以我们就通过这篇文章来瞅瞅 Yii2 的 Active Record 新特性。
1、支持更多的数据库
下面是所有目前被 Yii 的 AR 功能所支持的数据库列表:
MySQL 4.1 及以上:通过 [[yii\db\ActiveRecord]]
PostgreSQL 7.3 及以上:通过 [[yii\db\ActiveRecord]]
SQLite 2 和 3:通过 [[yii\db\ActiveRecord]]
Microsoft SQL Server 2010 及以上:通过 [[yii\db\ActiveRecord]]
Oracle: 通过 [[yii\db\ActiveRecord]]
CUBRID 9.1 及以上:通过 [[yii\db\ActiveRecord]]
Sphinx:通过 [[yii\sphinx\ActiveRecord]],需求 yii2-sphinx
扩展
ElasticSearch:通过 [[yii\elasticsearch\ActiveRecord]],需求 yii2-elasticsearch
扩展
Redis 2.6.12 及以上:通过 [[yii\redis\ActiveRecord]],需求 yii2-redis
扩展
MongoDB 1.3.0 及以上:通过 [[yii\mongodb\ActiveRecord]],需求 yii2-mongodb
扩展
2、Active Record 的使用
1)简单使用
//获取状态正常的并且以name排序的客户
$customer = Customer::find()->where(['status'=>1])->orderBy('name')->all();
//获取name=gavin 的用户
$customer = Customer::find()->where(['name'=>'gavin'])->one();
//获取所有状态正常的用户
$count = Customer::find()->where(['status'=>1])->count();
//获取特定字段的数据
$customer = Customer::find()->where(['status'=>1])->select(['name', 'nickname', 'address'])->all();
//获取ID=1,2,3的数据
$customer = Customer::findAll(['id'=>[1,2,3]]);
//获取特定条件的一条数据
$customer = Customer::findOne(['name'=>'gavin', 'stuats'=>1])
2)连表操作
模型:
//YII的连表操作和CAKEPHP的操作基本是一致的,都是通过hasOne 和 hasMany()关联获取的。
比如:我有一个News表,还有一个NewsImages表,一个news对应多条newsp_w_picpaths数据
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\base\Object;
/**
* news class
*
* @author Gavin
* @version 1.0
*/
class News extends \yii\db\ActiveRecord
{
/**
* 指定数据表名称
*
* @return string
*/
public static function tableName() {
return '`news`';
}
/**
* 对news表和news_amges表进行关联
* news_p_w_picpaths数据表中的news_id对应news表中的id
*
* @return Object
*/
public function getNewsImages() {
return $this->hasMany(NewsImages::className(), ['news_id'=>'id']);
}
}
<?php
namespace backend\models;
use Yii;
use yii\base\Object;
/**
* news p_w_picpaths class
*
* @author Gavin
* @version 1.0
*/
class NewsImages extends \yii\db\ActiveRecord
{
/**
* 指定数据表名称
*
* @return string
*/
public static function tableName() {
return '`news_p_w_picpaths`';
}
/**
* news p_w_picpath 数据与news数据相关联
*
* @return Object
*/
public function getNews() {
return $this->hasOne(News::className(), ['id'=>'news_id']);
}
}
控制器
<?php
namespace backend\controllers;
use Yii;
use yii\base\Controller;
use backend\models\News;
class NewsController extends Controller
{
/**
* 连表查询数据
*
* @return void
*/
public function actionLB() {
$news = News::findOne(3);
$newsImage = $news->newsImages;
echo "<pre>";
var_dump($news);
var_dump($newsImage);
die;
}
}
备注:如果查询的关联表需要获取字段,在同一个表中应该这么写:【获取的特定字段一定包含关联字段,否则无法获取数据】
$object = News::find()->orderBy('published_time DESC, created_time DESC');
$newsDatas = $object->select(['id', 'title', 'status', 'recommend', 'set_top', 'hot_focus', 'published_time', 'created_time'])
->offset($pages->offset)
->limit($pages->limit)
//获取关联表的字段
->with(['newsImages'=>function($object){$object->select('id', 'p_w_picpath_url');}])
->asArray()
->all();
WHERE多条件的使用
AND
$condition
= [
'status'
=>1,
'set_top'
=>1]
如果是 or 来怎么处理呢?
$condition = ['or', 'recommend=1', 'status=2'];
$condition = ['or', 'recommend=1', 'source="http://dreameng.blog.51cto.com"'];
IN
$condition
= [
'in'
,
'id'
, [1,2,3]];
LIKE
$condition = [
'like'
,
'source'
,
'dreameng'
];
AND | OR
$condition
= [
'and'
,
'recommend=1'
, [
'or'
,
'source="http://dreameng.blog.51cto.com"'
,
'status=1'
]];
转载于:https://blog.51cto.com/dreameng/1650016