laravel框架model類查詢實現:laravel
User::where(['uid'=8])->get();數據庫
User類繼承自Model類:Illuminate\Database\Eloquent\Modelapp
當User類靜態調用where方法時,自動調用了Model裏的魔術方法:框架
public static function __callStatic($method, $parameters) { $instance = new static; //這裏的$instance就是User類的實例對象 return call_user_func_array([$instance, $method], $parameters); }
至關於調用了user對象的where方法,這時就又調用了魔術方法:ide
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方法->ui
->ConnectionFactory類的createSingleConnection()this
/** * 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); }