laravel5.1框架model类查询的实现方法
laravel框架model类查询实现:
User::where(['uid'=8])->get();
User类继承自Model类:Illuminate\Database\Eloquent\Model
当User类静态调用where方法时,自动调用了Model里的魔术方法:
publicstaticfunction__callStatic($method,$parameters) { $instance=newstatic;//这里的$instance就是User类的实例对象 returncall_user_func_array([$instance,$method],$parameters); }
相当于调用了user对象的where方法,这时就又调用了魔术方法:
publicfunction__call($method,$parameters) { if(in_array($method,['increment','decrement'])){ returncall_user_func_array([$this,$method],$parameters); } $query=$this->newQuery();//返回Illuminate\Database\Eloquent\Builder对象 returncall_user_func_array([$query,$method],$parameters); }
相当于调用Illuminate\Database\Eloquent\Builder对象里的where方法和get方法,这两个方法里其实
其实是封装调用了Illuminate\Database\Query\Builder对象里的where方法和get方法->get方法里调用了runselect方法
runSelect方法:
/** *Runthequeryasa"select"statementagainsttheconnection. * *@returnarray */ protectedfunctionrunSelect() { return$this->connection->select($this->toSql(),$this->getBindings(),!$this->useWritePdo);//调用connection对象的select方法 }
再看connection对象是怎么传到Illuminate\Database\Eloquent\Builder类实例里的:
Model类的newQuery方法:
/** *Getanewquerybuilderforthemodel'stable. * *@return\Illuminate\Database\Eloquent\Builder */ publicfunctionnewQuery() { $builder=$this->newQueryWithoutScopes(); return$this->applyGlobalScopes($builder); }
Model类的newQueryWithoutScopes方法:
/** *Getanewquerybuilderthatdoesn'thaveanyglobalscopes. * *@return\Illuminate\Database\Eloquent\Builder|static */ publicfunctionnewQueryWithoutScopes() { $builder=$this->newEloquentBuilder( $this->newBaseQueryBuilder()//这个方法返回 ); //Oncewehavethequerybuilders,wewillsetthemodelinstancessothe //buildercaneasilyaccessanyinformationitmayneedfromthemodel //whileitisconstructingandexecutingvariousqueriesagainstit. return$builder->setModel($this)->with($this->with); }
Model类的newBaseQueryBuilder方法实现
/** *Getanewquerybuilderinstancefortheconnection. * *@return\Illuminate\Database\Query\Builder */ protectedfunctionnewBaseQueryBuilder() { $conn=$this->getConnection();\\连接数据库并返回connection对象 $grammar=$conn->getQueryGrammar(); returnnewQueryBuilder($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()
/** *Createasingledatabaseconnectioninstance. * *@paramarray$config *@return\Illuminate\Database\Connection */ protectedfunctioncreateSingleConnection(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类查询的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。