目的
- DBファサードのstatementメソッドを使用する機会があり当該メソッドは内部でどのような処理をしているのか気になりまずはメソッドが記載されている場所を探してみた
環境
- ハードウェア環境
項目 | 情報 |
---|---|
OS | macOS Catalina(10.15.5) |
ハードウェア | MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports) |
プロセッサ | 2 GHz クアッドコアIntel Core i5 |
メモリ | 32 GB 3733 MHz LPDDR4 |
グラフィックス | Intel Iris Plus Graphics 1536 MB |
- ソフトウェア環境
項目 | 情報 | 備考 |
---|---|---|
PHP バージョン | 7.4.8 | Homebrewを用いてこちらの方法で導入→Mac HomebrewでPHPをインストールする |
Laravel バージョン | 6.X | commposerを用いてこちらの方法で導入→Mac Laravelの環境構築を行う |
MySQLバージョン | 8.0.19 for osx10.13 on x86_64 | Homwbrewを用いてこちらの方法で導入→Mac HomebrewでMySQLをインストールする |
情報
- Macに直接作成したLaravelの環境でこの記事の内容を確認した。
- 当該メソッドがどこに記載されているのかを筆者がどのように探したかからまとめる。
statementメソッドの場所
-
DBファサードを使用する際のuse宣言で指定するnamespace先にあるファイルを開いてみる。(DBファサードを使用するには
use Illuminate\Support\Facades\DB;
と宣言する。)laravelのアプリ名ディレクトリで下記コマンドを実行してファイルを開く。vi vendor/laravel/framework/src/Illuminate/Support/Facades/DB.php
-
下記のような内容が記載されていた。
アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Support/Facades/DB.php<?php namespace Illuminate\Support\Facades; /** * @method static \Illuminate\Database\ConnectionInterface connection(string $name = null) * @method static string getDefaultConnection() * @method static void setDefaultConnection(string $name) * @method static \Illuminate\Database\Query\Builder table(string $table) * @method static \Illuminate\Database\Query\Expression raw($value) * @method static mixed selectOne(string $query, array $bindings = []) * @method static array select(string $query, array $bindings = []) * @method static bool insert(string $query, array $bindings = []) * @method static int update(string $query, array $bindings = []) * @method static int delete(string $query, array $bindings = []) * @method static bool statement(string $query, array $bindings = []) * @method static int affectingStatement(string $query, array $bindings = []) * @method static bool unprepared(string $query) * @method static array prepareBindings(array $bindings) * @method static mixed transaction(\Closure $callback, int $attempts = 1) * @method static void beginTransaction() * @method static void commit() * @method static void rollBack() * @method static int transactionLevel() * @method static array pretend(\Closure $callback) * @method static void listen(\Closure $callback) * * @see \Illuminate\Database\DatabaseManager * @see \Illuminate\Database\Connection */ class DB extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'db'; } }
アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Support/Facades/DB.php
のDBクラスではstatementメソッドは宣言されていないようである。しかしながらdocコメント内部に@see
タグを用いてDatabaseManagerクラスとConnectionクラスの記載がされているのでこの2つを見に行ってみる。アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php
を見に行ってみる。たくさんのメソッドが記載されていたたためDatabaseManagerクラスの内容の記載は割愛するがstatementメソッドは記載されていなかった。-
アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Connection.php
を見に行ってみる。関係ありそうな部分を下記に抜粋して記載する。アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Connection.php/** * Execute an SQL statement and return the boolean result. * * @param string $query * @param array $bindings * @return bool */ public function statement($query, $bindings = []) { return $this->run($query, $bindings, function ($query, $bindings) { if ($this->pretending()) { return true; } $statement = $this->getPdo()->prepare($query); $this->bindValues($statement, $this->prepareBindings($bindings)); $this->recordsHaveBeenModified(); return $statement->execute(); }); }
これで当該メソッドの記載場所はわかったので次回内部でどのような処理が実行されているのか確認しようと思う。