最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年10月08日 16:40:42   作者:dongruiha  
今天小編就為大家分享一篇laravel5.1框架model類(lèi)查詢(xún)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

laravel框架model類(lèi)查詢(xún)實(shí)現(xiàn):

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

User類(lèi)繼承自Model類(lèi):Illuminate\Database\Eloquent\Model

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

public static function __callStatic($method, $parameters)
{
  $instance = new static; //這里的$instance就是User類(lèi)的實(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類(lèi)實(shí)例里的:

Model類(lèi)的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類(lèi)的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類(lèi)的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ù)庫(kù)并返回connection對(duì)象

  $grammar = $conn->getQueryGrammar();

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

}

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

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

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

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

Model類(lèi)getConnection方法->DatabaseManager類(lèi)connection方法->

->ConnectionFactory類(lèi)的createSingleConnection()

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

}

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

相關(guān)文章

  • PHP獲取客戶(hù)端真實(shí)IP地址的5種情況分析和實(shí)現(xiàn)代碼

    PHP獲取客戶(hù)端真實(shí)IP地址的5種情況分析和實(shí)現(xiàn)代碼

    這篇文章主要介紹了PHP獲取客戶(hù)端真實(shí)IP地址的幾種情況分析和實(shí)現(xiàn)代碼,重點(diǎn)在幾種干擾獲得真實(shí)IP的幾種情況介紹,需要的朋友可以參考下
    2014-07-07
  • ajax調(diào)用返回php接口返回json數(shù)據(jù)的方法(必看篇)

    ajax調(diào)用返回php接口返回json數(shù)據(jù)的方法(必看篇)

    下面小編就為大家?guī)?lái)一篇ajax調(diào)用返回php接口返回json數(shù)據(jù)的方法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Yii框架防止sql注入,xss攻擊與csrf攻擊的方法

    Yii框架防止sql注入,xss攻擊與csrf攻擊的方法

    這篇文章主要介紹了Yii框架防止sql注入,xss攻擊與csrf攻擊的方法,結(jié)合實(shí)例形式分析了Yii框架針對(duì)sql注入,xss攻擊與csrf攻擊的防范方法與相關(guān)函數(shù)調(diào)用注意事項(xiàng),需要的朋友可以參考下
    2016-10-10
  • php接口實(shí)現(xiàn)拖拽排序功能

    php接口實(shí)現(xiàn)拖拽排序功能

    列表拖拽排序是一個(gè)很常見(jiàn)的功能,在后端接口處理中經(jīng)常會(huì)用到,今天小編給大家?guī)?lái)了php接口實(shí)現(xiàn)拖拽排序功能,感興趣的朋友一起看看吧
    2018-04-04
  • php插入mysql數(shù)據(jù)返回id的方法

    php插入mysql數(shù)據(jù)返回id的方法

    今天小編就為大家分享一篇php插入mysql數(shù)據(jù)返回id的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • laravel通用化的CURD的實(shí)現(xiàn)

    laravel通用化的CURD的實(shí)現(xiàn)

    這篇文章主要介紹了laravel通用化的CURD的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • laravel框架學(xué)習(xí)筆記之組件化開(kāi)發(fā)實(shí)現(xiàn)方法

    laravel框架學(xué)習(xí)筆記之組件化開(kāi)發(fā)實(shí)現(xiàn)方法

    這篇文章主要介紹了laravel框架學(xué)習(xí)筆記之組件化開(kāi)發(fā)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了laravel框架組件化開(kāi)發(fā)相關(guān)的實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • PHP7 echo和print語(yǔ)句實(shí)例用法

    PHP7 echo和print語(yǔ)句實(shí)例用法

    在本文中小編整理了在PHP7中echo和print語(yǔ)句實(shí)例用法以及相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的學(xué)習(xí)下。
    2019-02-02
  • thinkPHP中create方法與令牌驗(yàn)證實(shí)例淺析

    thinkPHP中create方法與令牌驗(yàn)證實(shí)例淺析

    這篇文章主要介紹了thinkPHP中create方法與令牌驗(yàn)證,以一個(gè)簡(jiǎn)單實(shí)例形式分析了thinkPHP中create方法與令牌驗(yàn)證增加表單安全性的相關(guān)技巧,代碼備有詳盡注釋說(shuō)明,需要的朋友可以參考下
    2015-12-12
  • PHP 使用位運(yùn)算實(shí)現(xiàn)四則運(yùn)算的代碼

    PHP 使用位運(yùn)算實(shí)現(xiàn)四則運(yùn)算的代碼

    這篇文章主要介紹了PHP 使用位運(yùn)算實(shí)現(xiàn)四則運(yùn)算的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01

最新評(píng)論

蒙山县| 威信县| 莱西市| 镶黄旗| 汉寿县| 达州市| 韩城市| 疏附县| 遂宁市| 黄大仙区| 璧山县| 谢通门县| 三江| 饶阳县| 保山市| 合阳县| 青阳县| 黔南| 马尔康县| 会同县| 康保县| 获嘉县| 成安县| 临朐县| 阜新| 贵州省| 吐鲁番市| 灌南县| 隆安县| 贵定县| 句容市| 苍山县| 合作市| 莱州市| 嘉荫县| 蒙阴县| 烟台市| 临沭县| 望江县| 盖州市| 敦化市|