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


Posted in PHP onOctober 14, 2019

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

<?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模型代码:

<?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();
  }
}

控制器代码:

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模型代码:

namespace app\models;
use yii\db\ActiveRecord;
class Customer extends ActiveRecord{
  public function getOrders(){
    return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray();
  }
}

控制器代码:

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获取对应的顾客信息:

模型代码:

namespace app\models;
use yii\db\ActiveRecord;
class Order extends ActiveRecord{
  //根据订单id获取顾客信息
  public function getCustomer(){
    return $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray();
  }
}

控制器代码:

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)

关联查询的多次查询

$customers = Customer::find()->all();
foreach($customers as $customer){
  $orders = $customer->orders;
}

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

优化:

$customers = Customer::find()->with('orders')->all();
foreach($customers as $customer){
  $orders = $customer->orders;
}

总共查询两次。

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

PHP 相关文章推荐
Discuz! Passport 通行证整合
Mar 27 PHP
网页游戏开发入门教程二(游戏模式+系统)
Nov 02 PHP
php 智能404跳转代码,适合换域名没改变目录的网站
Jun 04 PHP
php计算程序运行时间的简单例子分享
May 10 PHP
PHP中数组的分组排序实例
Jun 01 PHP
Yii使用ajax验证显示错误messagebox的解决方法
Dec 03 PHP
PHP常用设计模式之委托设计模式
Feb 13 PHP
phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法
Mar 30 PHP
php封装的表单验证类完整实例
Oct 19 PHP
PHP+Ajax实现上传文件进度条动态显示进度功能
Jun 04 PHP
PHP常见的几种攻击方式实例小结
Apr 29 PHP
PHP使用openssl扩展实现加解密方法示例
Feb 20 PHP
laravel实现上传图片并在页面显示的例子
Oct 14 #PHP
Thinkphp5.0 框架的请求方式与响应方式分析
Oct 14 #PHP
laravel 多图上传及图片的存储例子
Oct 14 #PHP
Laravel 5.4前后台分离,通过不同的二级域名访问方法
Oct 13 #PHP
Laravel开启跨域请求的方法
Oct 13 #PHP
浅谈Laravel中的三种中间件的作用
Oct 13 #PHP
laravel config文件配置全局变量的例子
Oct 13 #PHP
You might like
追求程序速度,而不是编程的速度
2008/04/23 PHP
PHP中遍历stdclass object的实现代码
2011/06/09 PHP
easyui的tabs update正确用法分享
2014/03/21 PHP
ThinkPHP中处理表单中的注意事项
2014/11/22 PHP
php图片添加文字水印实现代码
2016/03/15 PHP
Zend Framework缓存Cache用法简单实例
2016/03/19 PHP
javascript之ESC(第二类混淆)
2007/05/06 Javascript
jQuery 加上最后自己的验证
2009/11/04 Javascript
jQuery中获取checkbox选中项等操作及注意事项
2013/11/24 Javascript
jquery原理以及学习技巧介绍
2015/11/11 Javascript
JavaScript的ExtJS框架中表格的编写教程
2016/05/21 Javascript
微信小程序  生命周期详解
2016/10/27 Javascript
jQuery设置和获取select、checkbox、radio的选中值方法
2017/01/01 Javascript
js获取浏览器和屏幕的各种宽度高度
2017/02/22 Javascript
jQuery插件HighCharts实现的2D条状图效果示例【附demo源码下载】
2017/03/15 Javascript
微信小程序开发之选项卡(窗口底部TabBar)页面切换
2017/04/12 Javascript
js实现多张图片延迟加载效果
2017/07/17 Javascript
JavaScript 保护变量不被随意修改的实现代码
2017/09/27 Javascript
Bootstrap Table中的多选框删除功能
2018/07/15 Javascript
vue router 跳转后回到顶部的实例
2018/08/31 Javascript
微信小程序自定义底部导航带跳转功能
2018/11/27 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
零基础写python爬虫之urllib2使用指南
2014/11/05 Python
Python通过90行代码搭建一个音乐搜索工具
2015/07/29 Python
基于Python的关键字监控及告警
2017/07/06 Python
Python实现excel转sqlite的方法
2017/07/17 Python
Python创建二维数组实例(关于list的一个小坑)
2017/11/07 Python
对Python中class和instance以及self的用法详解
2019/06/26 Python
利用django创建一个简易的博客网站的示例
2020/09/29 Python
Python 随机按键模拟2小时
2020/12/30 Python
手工制作的音乐盒:Music Box Attic
2019/09/05 全球购物
土地转让协议书范本
2014/04/15 职场文书
大学生应聘求职信
2014/05/26 职场文书
2015共产党员公开承诺书
2015/01/22 职场文书
个人自荐书怎么写
2015/03/26 职场文书
python实现自动化群控的步骤
2021/04/11 Python