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类查询的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]