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

所属分类: 网络编程 / PHP编程 阅读数: 1117
收藏 0 赞 0 分享

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去除二维数组的重复项方法

这篇文章主要介绍了两种php去除二维数组的重复项方法,大家可以进行比较看哪一种更适合自己,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现分页功能的3种方法第1/3页

这篇文章主要介绍了php实现分页功能的3种方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

php对二维数组进行相关操作(排序、转换、去空白等)

这篇文章主要介绍了php对二维数组进行相关操作,包括php对二维数组排序、转换、去空白,以及去重复值等,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

php实现网站留言板功能

这篇文章主要介绍了php实现网站留言板功能,主要仿照了畅言留言板和网易跟帖样式进行制作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

PHP实现HTML页面静态化的方法

这篇文章主要介绍了PHP实现HTML页面静态化的方法,分享了静态处理的方法,静态处理后的优势,并提供了多种静态的方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

php对文件夹进行相关操作(遍历、计算大小)

这篇文章主要介绍了php对文件夹进行相关操作,包括遍历并打印指定目录下所有文件和计算文件大小去空白,以及去重复值等,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

非常全面的php日期时间运算汇总

这篇文章主要整理了关于php日期时间运算相关内容,涉及知识点较为全面,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

php根据用户语言跳转相应网页

这篇文章主要介绍了php根据用户语言跳转相应网页的方法,主要区分国内国外,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

双冒号 ::在PHP中的使用情况

前几天在百度知道里面看到有人问PHP中双冒号::的用法,当时给他的回答比较简洁因为手机打字不大方便!今天突然想起来,所以在这里总结一下我遇到的双冒号::在PHP中使用的情况
收藏 0 赞 0 分享

PHP explode()函数的几个应用和implode()函数有什么区别

这篇文章主要介绍了PHP explode()函数的几个应用和implode()函数有什么区别,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多