yii框架数据库关联查询操作示例

2025-05-27 0 104

本文实例讲述了yii框架数据库关联查询操作。分享给大家供大家参考,具体如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<?php

namespace app\\controllers;

use yii\\web\\Controller;

use app\\models\\Customer;

class CustomerController extends Controller{

//根据顾客名字查询出所有的订单信息

public function actionIndex(){

$customer = Customer::find()->where(['name'=>'zhangsan'])->one();

$orders = $customer->hasMany('app\\models\\Order',['customer_id'=>'id'])->asArray()->all();

print_r($orders);

}

}

?>

上边的控制器方法查询,Customer模型没有具体方法。

上边的 app\\models\\Order 可以改进为Order::className(),并且上边要添加use app\\models\\Order;

方式二:(使用model方法)

customer模型代码:

?

1

2

3

4

5

6

7

8
<?php

namespace app\\models;

use yii\\db\\ActiveRecord;

class Customer extends ActiveRecord{

public function getOrders(){

return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray()->all();

}

}

控制器代码:

?

1

2

3

4

5

6

7

8

9

10

11
namespace app\\controllers;

use yii\\web\\Controller;

use app\\models\\Customer;

class CustomerController extends Controller{

//根据顾客名字查询出所有的订单信息

public function actionIndex(){

$customer = Customer::find()->where(['name'=>'zhangsan'])->one();

$orders = $customer->getOrders();

print_r($orders);

}

}

方法三:(调用模型的属性查询)

customer模型代码:

?

1

2

3

4

5

6

7
namespace app\\models;

use yii\\db\\ActiveRecord;

class Customer extends ActiveRecord{

public function getOrders(){

return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray();

}

}

控制器代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
namespace app\\controllers;

use yii\\web\\Controller;

use app\\models\\Customer;

class CustomerController extends Controller{

//根据顾客名字查询出所有的订单信息

public function actionIndex(){

$customer = Customer::find()->where(['name'=>'zhangsan'])->one();

$orders = $customer->orders;

//说明,当调用一个不存在的属性时,

//php会去调用一个__get()的方法,

//__get()的方法会自动调用一个get+属性的方法,即getOrders()

//并且会再查询时自动补上->all()或->one()方法,根据模型查询的hasMany或hasOne决定的

print_r($orders);

}

}

根据订单id获取对应的顾客信息:

模型代码:

?

1

2

3

4

5

6

7

8
namespace app\\models;

use yii\\db\\ActiveRecord;

class Order extends ActiveRecord{

//根据订单id获取顾客信息

public function getCustomer(){

return $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray();

}

}

控制器代码:

?

1

2

3

4

5

6

7

8

9

10

11
namespace app\\controllers;

use yii\\web\\Controller;

use app\\models\\Order;

class CustomerController extends Controller{

//根据订单查询用户信息

public function actionIndex(){

$orders = Order::find()->where(['id'=>2])->one();

$customer = $orders->customer;

print_r($customer);

}

}

以上代码中的$orders->customer会记录缓存,如果要删除缓存,可以使用unset($orders->customer)

关联查询的多次查询

?

1

2

3

4
$customers = Customer::find()->all();

foreach($customers as $customer){

$orders = $customer->orders;

}

这样如果有100条数据,就总共需要查询101次。

优化:

?

1

2

3

4
$customers = Customer::find()->with('orders')->all();

foreach($customers as $customer){

$orders = $customer->orders;

}

总共查询两次。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

原文链接:https://www.cnblogs.com/gyfluck/p/9104295.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 yii框架数据库关联查询操作示例 https://www.kuaiidc.com/70846.html

相关文章

猜你喜欢
发表评论
暂无评论