我是靠谱客的博主 追寻音响,最近开发中收集的这篇文章主要介绍laravel5.1框架model类查询的实现方法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

laravel框架model类查询实现:

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

User类继承自Model类:IlluminateDatabaseEloquentModel

当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(); //返回IlluminateDatabaseEloquentBuilder对象

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

相当于调用IlluminateDatabaseEloquentBuilder对象里的where方法和get方法,这两个方法里其实

其实是封装调用了IlluminateDatabaseQueryBuilder对象里的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对象是怎么传到IlluminateDatabaseEloquentBuilder类实例里的:

Model类的newQuery方法:

/**
 * Get a new query builder for the model's table.
 *
 * @return IlluminateDatabaseEloquentBuilder
 */
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 IlluminateDatabaseEloquentBuilder|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 IlluminateDatabaseQueryBuilder
 */
protected function newBaseQueryBuilder()
{
  $conn = $this->getConnection(); \连接数据库并返回connection对象

  $grammar = $conn->getQueryGrammar();

  return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); //IlluminateDatabaseQueryBuilder

}

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

IlluminateDatabaseDatabaseServiceProvider 里的boot方法设置的

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

如何创建的数据库连接:

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

->ConnectionFactory类的createSingleConnection()

/**
 * Create a single database connection instance.
 *
 * @param array $config
 * @return IlluminateDatabaseConnection
 */
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类查询的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是追寻音响为你收集整理的laravel5.1框架model类查询的实现方法的全部内容,希望文章能够帮你解决laravel5.1框架model类查询的实现方法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部