laravel5.1框架model类查询的实现方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-28 10:31:36 浏览: 评论:0
今天小编就为大家分享一篇laravel5.1框架model类查询的实现方法,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧.
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);
- }
Tags: laravel5 1 model类查询
相关文章
- ·如何在laravel 5中使用DB事务?(2020-01-31)
- ·Laravel5中contracts详解(2021-05-15)
- ·Laravel 5 学习笔记(2021-05-15)
- ·Laravel 5.5官方推荐的Nginx配置学习教程(2021-08-11)
- ·laravel5 使用try catch的实例详解(2021-08-21)
- ·Laravel5.5中利用Passport实现Auth认证的方法(2021-08-22)
- ·Laravel 5.5基于内置的Auth模块实现前后台登陆详解(2021-08-25)
- ·Laravel5.2使用Captcha生成验证码实现登录(session巨坑)(2021-08-26)
- ·Laravel 5.4.36中session没有保存成功问题的解决(2021-09-03)
- ·在Laravel5.6中使用Swoole的协程数据库查询(2021-10-01)
- ·详细Laravel5.5执行表迁移命令出现表为空的解决方案(2021-10-10)
- ·Laravel5.5以下版本中如何自定义日志行为详解(2021-10-20)
- ·Laravel5框架添加自定义辅助函数的方法(2021-10-20)
- ·laravel5使用freetds连接sql server的方法(2021-11-02)
- ·Laravel5.4框架使用socialite实现github登录的方法(2021-11-13)
- ·Laravel5.7框架安装与使用学习笔记图文详解(2021-11-14)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)