Yii2 使用 Active Record访问和操作数据库中的数据

Active Record 提供了⼀个⾯向对象的接⼝, ⽤以访问和操作数据库中的数据。 Active Record 类与数据库表关联, Active Record 实例对应于该表的⼀⾏, Active Record 实例的属性表示该⾏中特定列的值。 你可以访问
Active Record 属性并调⽤ Active Record ⽅法来访问和操作存储在数据库表中的数据, ⽽不⽤编写原始 SQL 语句。

官⽹⽂档:
https://www.yiichina.com/doc/guide/2.0/db-active-record#active-record
创建模型

namespace app\models;
use yii\db\ActiveRecord;
class Customer extends ActiveRecord
{
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    /**
     * @return string Active Record 类关联的数据库表名称
     */
    public static function tableName()
    {
        return '{{customer}}';
    }
}

框架表名的驼峰式为模型名,?如 customer 对应的模型名为 Customer
$customer = new Customer();
$customer->name = 'Lchiee';
$customer->save();
 

$values = [
    'name' => 'James',
    'email' => '[email protected]',
];

$customer = new Customer();
$customer->attributes = $values;
$customer->save();
块赋值
删除
$customer = Customer::findOne(123);
$customer->delete();
更新

$customer = Customer::findOne(1);
$customer->name = 'Lchiee Jk';
$customer->save(); //返回 boolean

// 没有更新时返回 0
$customer->update(); //返回 int
批量更新
// UPDATE `customer` SET `status` = 1 WHERE `email` LIKE `%@example.com%`
Customer::updateAll(['status' => 1], ['like', 'email', '@example.com']);
// UPDATE `customer` SET `age` = `age` + 1
Customer::updateAllCounters(['age' => 1]);
模型查询
// 获取查询结果
$customer = Customer::findOne();
//根据主键查询 SELECT * FROM `customer` WHERE `id` = 123
$customer = Customer::findOne(123);
//条件查询 SELECT * FROM `customer` WHERE `id` = 123 AND `status` = 1

$customer = Customer::findOne([
    'id' => 123,
    'status' => 1,
]);

//查询多条 SELECT * FROM `customer` WHERE `id` IN (1, 2, 3)
$customer = Customer::findAll([1, 2, 3]);
// 查询全表
$customer = Customer::findAll();
// 统计查询 SELECT COUNT(*) FROM `customer` WHERE `status` = 1
$count = Customer::find()->where(['active' => 1])->count();
// 以 id 为索引返回结果集
$count = Customer::find()->index('id')->all();
// (type = 1) AND (status = 2)
['type' => 1, 'status' => 2]
// (id IN (1, 2, 3)) AND (status = 2)
['id' => [1, 2, 3], 'status' => 2]
// status IS NULL
['status' => null]
// id=1 AND id=2
['and', 'id=1', 'id=2']
// type=1 AND (id=1 OR id=2)
['and', 'type=1', ['or', 'id=1', 'id=2']]
// (type IN (7, 8, 9) OR (id IN (1, 2, 3)))
['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]
// NOT (attribute IS NULL)
['not', ['attribute' => null]]
// id >= 10
['>=', 'id', 10]

 


where && andWhere && orWhere
// id IN (1, 2, 3)
['in', 'id', [1, 2, 3]]
// (`card_type`, `source`) IN (('?陆身份证', '微信'), ('护照', '后台'))
['in', ['card_type', 'source'], [['card_type' => '?陆身份证', 'source' => '微信'],
['card_type' => '护照', 'source' => '后台']]]
// user_id IN (SELECT "id" FROM "users" WHERE "active"=1)
['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' =>
1])]


`IN` && `NOT IN`
// name LIKE ‘%tester%'
['like', 'name', 'tester']
// name LIKE '%test%' AND name LIKE '%sample%'
['like', 'name', ['test', 'sample']]
// name LIKE '%tester'
['like', 'name', '%tester', false]
你也可以  'or like' 或者 'or not like' 来实现 `OR` 跟 `LIKE` 和 `NOT LIKE` 的组合
`LIKE` && `NOT LIKE`
// EXISTS (SELECT "id" FROM "users" WHERE "active"=1)
[‘exists', (new Query())->select('id')->from('users')->where(['active' => 1])]
`EXISTS` && `NOT EXISTS`


// id BETWEEN 1 AND 10
$customer = Customer::find()->where([‘between', 'id', 1, 10])->all();
`BETWEEN` && `NOT BETWEEN`
// SELECT EXISTS(SELECT * FROM `customer` WHERE `id`='123')
$customer = Customer::find()->where(['id' => '123'])->exists();
return $customer; // bool
exists
Tips: 当个值为 null、空数组、空字符串或者?个只包含空?字符时,那么它将被判定为空值。
// SELECT * FROM `customer` WHERE `card_type`='?陆身份证'

$card_type = '?陆身份证';
$soure = '';
$customer = Customer::find()
    ->filterWhere([
        'card_type' => '身份证',
        'soure' => '',
    ])
    ->all();

filterWhere && andFilterWhere && orFilterWhere
假设 $card_type 和 $soure 来?于?户的输?
使?关联数据
关联关系
一对一
一对多
 多对多
 桥接一对多
 多态一对多关联
 多态多对多关联
声明关联关系

class Customer extends ActiveRecord
{
// ...
    public function getOrders()
    {
        return $this->hasMany(Orders::className(), ['customer_id' => 'id']);
    }
}
class Store extends ActiveRecord
{
// ...
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }
}

 
访问关联关系
// SELECT * FROM `customer` WHERE `id` = 123
$customer = Customer::findOne(123);
// SELECT * FROM `order` WHERE `customer_id` = 123
$orders = $customer->orders;
// SELECT * FROM `order` WHERE `customer_id` = 123 AND `subtotal` > 200 ORDER BY
`id`
$orders = $customer->getOrders()
->where(['>', 'subtotal', 200])
->orderBy('id')
->all();


动态关联查询
延迟加载
// SELECT * FROM `customer` WHERE `id` = 123
$customer = Customer::findOne(123);
// SELECT * FROM `order` WHERE `customer_id` = 123
$orders = $customer->orders;
// 没有 SQL 语句被执行
$orders2 = $customer->orders;
// SELECT * FROM `customer` LIMIT 100
$customers = Customer::find()->limit(100)->all();
foreach ($customers as $customer) {
// SELECT * FROM `order` WHERE `customer_id` = ...
$orders = $customer->orders;
}
But, 延迟加载….
// SELECT * FROM `customer` LIMIT 100;
// SELECT * FROM `orders` WHERE `customer_id` IN (...)
$customers = Customer::find()
->with('orders')
->limit(100)
->all();


foreach ($customers as $customer) {
// 没有任何的 SQL 执行
$orders = $customer->orders;
}
即时加载
// 即时加载 "orders" and "country"
$customers = Customer::find()->with('orders', 'country')->all();
// 等同于使?数组语法 如下
$customers = Customer::find()->with(['orders', 'country'])->all();
即时加载多个
// 即时加载“订单”和嵌套关系“orders.items”
$customers = Customer::find()->with('orders.items')->all();
嵌套即时加载
// 查找所有客户,并带上他们国家和活跃订单
// SELECT * FROM `customer`
// SELECT * FROM `country` WHERE `id` IN (...)
// SELECT * FROM `order` WHERE `customer_id` IN (...) AND `status` = 1

$customers = Customer::find()->with([
    'country',
    'orders' => function ($query) {
        $query->andWhere(['status' => 1]);
    },
])->all();

自定义关联查询
提示: 如果你在即时加载的关联中调用 select() 方法,你要确保在关联声明中引用 的列必须被
select。否则,相应的模型(Models)无 法被加载。
关联关系的 JOIN 查询
// SELECT `customer`.* FROM `customer`
// LEFT JOIN `order` ON `order`.`customer_id` = `customer`.`id`
// WHERE `order`.`status` = 1
// SELECT * FROM `order` WHERE `customer_id` IN (...)
$customers = Customer::find()
->select('customer.*')
->leftJoin('order', '`order`.`customer_id` = `customer`.`id`')
->where(['order.status' => 1])
->with('orders')
->all();
$customers = Customer::find()
->joinWith('orders')
->where(['order.status' => Order::STATUS_ACTIVE])
->all();


使用 joinWith
默认情况下, joinWith() 会使⽤ LEFT JOIN 去连接主表和关联表。 你可以通过 $joinType 参
数指定不同的连接类型(⽐如 RIGHT JOIN)。 如果你想要的连接类型是 INNER JOIN,你可
以直接⽤ innerJoinWith() ⽅法代替。
 


使用innerJoinWith
$evaluate = $this->onlineRepairEvaluation()->find()->asArray()
->select(‘online_repair_evaluation.*, customer.id as customer_id,
project.name as project_name')
->addSelect(["CONCAT(building.name, '-', room.number) as room_name"])
->where(['online_repair_evaluation.id' => $id])
->innerJoinWith([
'onlineRepair.customer',
'onlineRepair.roomSplited.room',
'onlineRepair.roomSplited.room.building',
'onlineRepair.project'
])
->with(['projectManagerEvaluate', 'tags.evaluateTag', 'questions'])
->one();
以上介绍了yii2里面的Active Record用法

 

你可能感兴趣的:(yii)