Yii2 Working with Databases

发布于 2024-09-02 19:17:50 字数 6328 浏览 15 评论 0

Create

<?php
 // 单条数据插入
Yii::$app->db->createCommand()->insert('shop_user', [
    'userName' => 'HelloWorld',
    'userPass' => md5(123456),
    'userEmail' => 'hello@world.com',
    'createdAt' => time(),
])->execute();

$customer = new Customer();
$customer->name = 'Ann';
$customer->email = 'AnnEason@qq.com';
$customer->save();

// 批量数据插入
Yii::$app->db->createCommand()->batchInsert('shop_user',
    ['userName', 'userPass', 'userEmail', 'createdAt'],
    [
        ['Ann', md5(123456), 'Ann@qq.com', time()],
        ['Leon', md5(123456), 'Leon@qq.com', time()],
        ['bitch', md5(123456), 'bitch@qq.com', time()],
        ['Linda', md5(123456), 'Linda@qq.com', time()],
    ]
)->execute();

Update

<?php
// 数据更新方法(表名, 字段与值, 条件)
Yii::$app->db->createCommand()->update('shop_user', ['userName' => 'hello'], 'userId = 6')->execute();

$customer = Customer::findOne(123);
$customer->email = 'AnnEason@qq.com';
$customer->save();

$values = [
    'name' => 'Ann',
    'email' => 'Ann@qq.com'
];

$customer = new Customer();
$customer->attributes = $values;
$customer->save();

Customer::updateAll(['status' => Customer::STATUS_ACTIVE],['like','email','@example.com']);

Retrieve

<?php
// 存在数据返回二维数组,没有数据返回空数组
$orders = Yii::$app->db->createCommand("select * from z_order order by id desc limit 100 ;")->queryAll();
$rows = (new \yii\db\Query())->select(['id','email'])->from('user') ->all();
$models = Model::find()->where(['status' => Model::STATUS])->all();

// 存在数据返回一维数组,没有数据返回 false
$order = Yii::$app->db->createCommand("select * from z_order where id = 802478;")->queryOne();
$row = (new \yii\db\Query())->from('user')->where(['like','username','test'])->one();
$model = Model::find()->where(['id'=>1])->one();

// 存在数据返回一维数组,没有数据返回空数组
$orderNumbers = Yii::$app->db->createCommand("select orderno from z_order order by id desc limit 100")
->queryColumn();
$columns = (new \yii\db\Query())->select('id')->from('user')->column()

// 存在数据返回标量数值,没有数据返回 false
$orderCount = Yii::$app->db->createCommand("select count(orderno) from z_order")->queryScalar();
$count = (new \yii\db\Query())->from('user')->where(['like','username','test'])->count();
$modelCount = Model::find()->where(['status'=>1])->count();

Delete

<?php
// 数据删除方法(表名,条件)
Yii::$app->db->createCommand()->delete('shop_user', 'userName = "HelloWorld"')->execute();

$customer = Customer::findOne(123);
$customer->delete();

Customer::deleteAll(['status' => Customer::STATUS_INACTIVE]);

Where

<?php
// String Format
$query->where('status = 1');
$query->where('status = :status', [':status' => $status]);
$query->where('status = :status')->addParams([':status' => $status]);

// Hash Format ...WHERE (`status` = 10) AND (`type` IS NULL) AND (`id` IN (4, 8, 15))
$query->where([
    'status' => 10,
    'type' => null,
    'id' => [4, 8, 15],
]);

// Operator Format [operator, operand1, operand2, ...]
$query->where(['and', 'id=1', 'id=2']);
$query->where(['and', 'type=1', ['or', 'id=1', 'id=2']]);

$query->where(['between', 'id', 1, 10]);
$query->where(['not between', 'id', 1, 10]);

$query->where(['in', 'id', [1, 2, 3, 4, 5]]);
$query->where(['not in', 'id', [1, 2, 3, 4, 5]]);

$query->where(['like', 'name', 'hello']);
$query->where(['like', 'name', ['hello', 'world']]);

$query->where(['>', 'age', 10])
$query->andWhere(['like', 'title', 'hello-world']);
$query->orWhere(['like','name','LuisEdware']);

$query->filterWhere(['name' => $name, 'email' => $email]);
$query->andFilterWhere(['!=', 'id', 1]);
$query->orFilterWhere(['status' => 2]);

$query->andFilterCompare('name', 'John Doe');

orderBy

<?php
$query->orderBy([
    'id' => SORT_ASC,
    'name' => SORT_DESC,
]);

$query->orderBy('id ASC, name DESC');

$query->orderBy('id ASC')->addOrderBy('name DESC');

groupBy

<?php

$query->groupBy(['id','status']);

$query->groupBy('id,status');

$query->groupBy(['id','status'])->addGroupBy('age');

having

<?php

$query->having(['status'=>1]);

$query->having(['status'=>1])->andHaving(['>','age',30]);

limit and offset

<?php
$query->limit(10)->offset(20);

join

<?php
// ... LEFT JOIN `post` ON `post`.`user_id` = `user`.`id`
$query->join('LEFT JOIN','post','post.user_id = user.id');

union

<?php
$query1 = (new \yii\db\Query())
    ->select('id,category_id AS type, name')
    ->from('post')
    ->limit(10);

$query2 = (new \yii\db\Query())
    ->select('id,type,name')
    ->from('user')
    ->limit(10);

$query1->union($query2);

find

<?php
// SELECT * FROM customer WHERE id = 123;
$customer = Customer::findOne(123);

// SELECT * FROM customer WHERE id IN (1,2,3,4,5)
$customers = Customer::findOne([1,2,3,4,5]);

$customer = Customer::findOne(['id'=>123, 'status'=> Customer::STATUS]);
$customers = Customer::findAll(['id'=>123, 'status'=> Customer::STATUS_ACTIVE]);

Relations

<?php
class Order extends ActiveRecord
{
    public function getOrderItems()
    {
        return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
    }

    public function getItems()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems');
    }
}
$items = $order->items;


$customers = Customer::find()->with('orders.items')->all();
// no SQL executed
$items = $customers[0]->orders[0]->items;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

上一篇:

下一篇:

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

绿光

暂无简介

0 文章
0 评论
23 人气
更多

推荐作者

linfzu01

文章 0 评论 0

可遇━不可求

文章 0 评论 0

枕梦

文章 0 评论 0

qq_3LFa8Q

文章 0 评论 0

JP

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文