主頁 > 知識(shí)庫 > laravel5.1框架model類查詢的實(shí)現(xiàn)方法

laravel5.1框架model類查詢的實(shí)現(xiàn)方法

熱門標(biāo)簽:沛縣400電話辦理 辦理重慶400電話 德陽中江如何申請(qǐng)400開頭電話 青白江地圖標(biāo)注 AI電話機(jī)器人OEM貼牌 銅川電話機(jī)器人價(jià)格 聊城電話外呼系統(tǒng)公司 智能電話機(jī)器人好公司門薩維 江蘇電商外呼系統(tǒng)運(yùn)營商

laravel框架model類查詢實(shí)現(xiàn):

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

User類繼承自Model類:Illuminate\Database\Eloquent\Model

當(dāng)User類靜態(tài)調(diào)用where方法時(shí),自動(dòng)調(diào)用了Model里的魔術(shù)方法:

public static function __callStatic($method, $parameters)
{
  $instance = new static; //這里的$instance就是User類的實(shí)例對(duì)象

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

相當(dāng)于調(diào)用了user對(duì)象的where方法,這時(shí)就又調(diào)用了魔術(shù)方法:

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對(duì)象

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

相當(dāng)于調(diào)用Illuminate\Database\Eloquent\Builder對(duì)象里的where方法和get方法,這兩個(gè)方法里其實(shí)

其實(shí)是封裝調(diào)用了Illuminate\Database\Query\Builder對(duì)象里的where方法和get方法->get方法里調(diào)用了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); //調(diào)用connection 對(duì)象的select方法
}

再看connection對(duì)象是怎么傳到Illuminate\Database\Eloquent\Builder類實(shí)例里的:

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() //這個(gè)方法返回
  );

  // 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方法實(shí)現(xiàn)

/**
 * Get a new query builder instance for the connection.
 *
 * @return \Illuminate\Database\Query\Builder
 */
protected function newBaseQueryBuilder()
{
  $conn = $this->getConnection(); \\連接數(shù)據(jù)庫并返回connection對(duì)象

  $grammar = $conn->getQueryGrammar();

  return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); //Illuminate\Database\Query\Builder

}

Model類的$resolver屬性(連接解析器)的設(shè)定是通過

Illuminate\Database\DatabaseServiceProvider 里的boot方法設(shè)置的

這樣Model類的getConnection方法實(shí)際調(diào)用的DatabaseManager類的connection方法,返回connection類實(shí)例

如何創(chuàng)建的數(shù)據(jù)庫連接:

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

->ConnectionFactory類的createSingleConnection()

/**
 * Create a single database connection instance.
 *
 * @param array $config
 * @return \Illuminate\Database\Connection
 */
protected function createSingleConnection(array $config)
{
  //創(chuàng)建連接器對(duì)象并連接數(shù)據(jù)庫返回pdo對(duì)象
  $pdo = $this->createConnector($config)->connect($config);
  //傳入PDO對(duì)象、并返回connection對(duì)象,connection對(duì)象負(fù)責(zé)查詢數(shù)據(jù)庫
  return $this->createConnection($config['driver'], $pdo, $config['database'], $config['prefix'], $config); 

}

以上這篇laravel5.1框架model類查詢的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 在Laravel5中正確設(shè)置文件權(quán)限的方法
  • Laravel5權(quán)限管理方法詳解
  • Laravel框架實(shí)現(xiàn)的rbac權(quán)限管理操作示例
  • laravel實(shí)現(xiàn)簡單用戶權(quán)限的示例代碼
  • 解決laravel中日志權(quán)限莫名變成了root的問題
  • laravel利用中間件做防非法登錄和權(quán)限控制示例
  • Laravel5.1數(shù)據(jù)庫連接、創(chuàng)建數(shù)據(jù)庫、創(chuàng)建model及創(chuàng)建控制器的方法
  • laravel5.1框架基礎(chǔ)之Blade模板繼承簡單使用方法分析
  • Laravel5.1框架注冊(cè)中間件的三種場景詳解
  • laravel5.1框架基礎(chǔ)之路由詳解
  • Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析

標(biāo)簽:南寧 濟(jì)寧 山南 三亞 赤峰 迪慶 鷹潭 烏魯木齊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《laravel5.1框架model類查詢的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  laravel5.1,框架,model,類,查詢,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《laravel5.1框架model類查詢的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于laravel5.1框架model類查詢的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章