laravel5.1框架model类查询的实现方法


Posted in PHP onOctober 08, 2019

laravel框架model类查询实现:

User::where(['uid'=8])->get();

User类继承自Model类:Illuminate\Database\Eloquent\Model

当User类静态调用where方法时,自动调用了Model里的魔术方法:

public static function __callStatic($method, $parameters)
{
  $instance = new static; //这里的$instance就是User类的实例对象

  return call_user_func_array([$instance, $method], $parameters);
}

相当于调用了user对象的where方法,这时就又调用了魔术方法:

public function __call($method, $parameters)
{
  if (in_array($method, ['increment', 'decrement'])) {
    return call_user_func_array([$this, $method], $parameters);
  }

  $query = $this->newQuery(); //返回Illuminate\Database\Eloquent\Builder对象

  return call_user_func_array([$query, $method], $parameters);
}

相当于调用Illuminate\Database\Eloquent\Builder对象里的where方法和get方法,这两个方法里其实

其实是封装调用了Illuminate\Database\Query\Builder对象里的where方法和get方法->get方法里调用了runselect方法

runSelect方法:

/**
 * Run the query as a "select" statement against the connection.
 *
 * @return array
 */
protected function runSelect()
{
  return $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo); //调用connection 对象的select方法
}

再看connection对象是怎么传到Illuminate\Database\Eloquent\Builder类实例里的:

Model类的newQuery方法:

/**
 * Get a new query builder for the model's table.
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function newQuery()
{
  $builder = $this->newQueryWithoutScopes();

  return $this->applyGlobalScopes($builder);
}

Model类的newQueryWithoutScopes方法:

/**
 * Get a new query builder that doesn't have any global scopes.
 *
 * @return \Illuminate\Database\Eloquent\Builder|static
 */
public function newQueryWithoutScopes()
{
  $builder = $this->newEloquentBuilder(
    $this->newBaseQueryBuilder() //这个方法返回
  );

  // Once we have the query builders, we will set the model instances so the
  // builder can easily access any information it may need from the model
  // while it is constructing and executing various queries against it.
  return $builder->setModel($this)->with($this->with);
}

Model类的newBaseQueryBuilder方法实现

/**
 * Get a new query builder instance for the connection.
 *
 * @return \Illuminate\Database\Query\Builder
 */
protected function newBaseQueryBuilder()
{
  $conn = $this->getConnection(); \\连接数据库并返回connection对象

  $grammar = $conn->getQueryGrammar();

  return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); //Illuminate\Database\Query\Builder

}

Model类的$resolver属性(连接解析器)的设定是通过

Illuminate\Database\DatabaseServiceProvider 里的boot方法设置的

这样Model类的getConnection方法实际调用的DatabaseManager类的connection方法,返回connection类实例

如何创建的数据库连接:

Model类getConnection方法->DatabaseManager类connection方法->

->ConnectionFactory类的createSingleConnection()

/**
 * Create a single database connection instance.
 *
 * @param array $config
 * @return \Illuminate\Database\Connection
 */
protected function createSingleConnection(array $config)
{
  //创建连接器对象并连接数据库返回pdo对象
  $pdo = $this->createConnector($config)->connect($config);
  //传入PDO对象、并返回connection对象,connection对象负责查询数据库
  return $this->createConnection($config['driver'], $pdo, $config['database'], $config['prefix'], $config); 

}

以上这篇laravel5.1框架model类查询的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
从网上搜到的phpwind 0day的代码
Dec 07 PHP
php自动获取字符串编码函数mb_detect_encoding
May 31 PHP
用PHP+MySQL搭建聊天室功能实例代码
Aug 20 PHP
解决phpmyadmin中缺少mysqli扩展问题的方法
May 06 PHP
php中用加号与用array_merge合并数组的区别深入分析
Jun 03 PHP
微信支付开发教程(一)微信支付URL配置
May 28 PHP
php实现文件下载代码分享
Aug 19 PHP
MyEclipse常用配置图文教程
Sep 11 PHP
php使用COPY函数更新配置文件的方法
Jun 18 PHP
php实现数据库的增删改查
Feb 26 PHP
在 Laravel 中 “规范” 的开发短信验证码发送功能
Oct 26 PHP
PHP array_reverse() 函数原理及实例解析
Jul 14 PHP
在laravel框架中使用model层的方法
Oct 08 #PHP
Laravel-添加后台模板AdminLte的实现方法
Oct 08 #PHP
PHP7.3.10编译安装教程
Oct 08 #PHP
PHP使用redis位图bitMap 实现签到功能
Oct 08 #PHP
laravel-admin自动生成模块,及相关基础配置方法
Oct 08 #PHP
laravel-admin表单提交隐藏一些数据,回调时获取数据的方法
Oct 08 #PHP
关于Laravel-admin的基础用法总结和自定义model详解
Oct 08 #PHP
You might like
php模拟js函数unescape的函数代码
2012/10/20 PHP
PHP使用strtotime计算两个给定日期之间天数的方法
2015/03/18 PHP
PHP生成及获取JSON文件的方法
2016/08/23 PHP
Jquery CheckBox全选方法代码附js checkbox全选反选代码
2010/06/09 Javascript
location.href 在IE6中不跳转的解决方法与推荐使用代码
2010/07/08 Javascript
JS事件在IE与FF中的区别详细解析
2013/11/20 Javascript
JS实现将人民币金额转换为大写的示例代码
2014/02/13 Javascript
jQuery实现ichat在线客服插件
2014/12/29 Javascript
javascript中createElement的两种创建方式
2015/05/14 Javascript
JS代码防止SQL注入的方法(超简单)
2016/04/12 Javascript
js动态生成form 并用ajax方式提交的实现方法
2016/09/09 Javascript
微信小程序 textarea 详解及简单使用方法
2016/12/05 Javascript
BootStrap学习笔记之nav导航栏和面包屑导航
2017/01/03 Javascript
JavaScript 中 apply 、call 的详解
2017/03/21 Javascript
浅谈vue路径优化之resolve
2017/10/13 Javascript
ES6中字符串string常用的新增方法小结
2017/11/07 Javascript
Vue匿名插槽与作用域插槽的合并和覆盖行为
2019/04/22 Javascript
layui 图片上传+表单提交+ Spring MVC的实例
2019/09/21 Javascript
layui多图上传实现删除功能的例子
2019/09/23 Javascript
使用Element的InfiniteScroll 无限滚动组件报错的解决
2020/07/27 Javascript
Vue页面跳转传递参数及接收方式
2020/09/09 Javascript
python计算程序开始到程序结束的运行时间和程序运行的CPU时间
2013/11/28 Python
Python中super的用法实例
2015/05/28 Python
PyQt5 实现给窗口设置背景图片的方法
2019/06/13 Python
django 配置阿里云OSS存储media文件的例子
2019/08/20 Python
Django 拼接两个queryset 或是两个不可以相加的对象实例
2020/03/28 Python
Django如何使用redis作为缓存
2020/05/21 Python
详解pytorch中squeeze()和unsqueeze()函数介绍
2020/09/03 Python
css3 border-image使用说明
2010/06/23 HTML / CSS
美国医疗用品、医疗设备和家庭保健用品商店:Medical Supply Depot
2018/07/08 全球购物
员工安全承诺书
2014/05/22 职场文书
党的群众路线教育实践活动总结材料
2014/10/30 职场文书
2016先进集体事迹材料范文
2016/02/25 职场文书
Anaconda安装pytorch和paddle的方法步骤
2022/04/03 Python
vue实现书本翻页动画效果实例详解
2022/04/08 Vue.js
HTML中link标签属性的具体用法
2023/05/07 HTML / CSS