ホームページ  >  記事  >  データベース  >  独自のデータベース パッケージを作成する方法 (4)

独自のデータベース パッケージを作成する方法 (4)

PHPz
PHPzオリジナル
2017-04-04 14:26:421730ブラウズ


テストデータベースのソース

本当は創刊号で渡すべきだったのですが、今さら触れても大丈夫です
リファレンスインストールmysqlサンプルデータベースsakila

シナリオ説明

テスト用に 1 つあります。データベース (sakila) にはテーブル (アクター) が含まれています。これで、それを Model クラスにバインドして、データを簡単に読み取ることができます


まず、新しいクラス名を作成します。は任意ですが、 と使用することをお勧めします。 テーブル名は同じです

Actor.php

<?php
/**
* 数据库中的Actor表
* 继承Model的属性和函数
*/
class Actor extends Model {
    // 由于我们的数据库表名和当前的类名是一样的,可以直接省略这一步
    // protected $table = &#39;Actor&#39;;

    // 设置Actor表的主键
    protected $identity = &#39;actor_id&#39;;

    // 或者设置unique key
    // 如果unique key只有一个
    // protected $unique = &#39;actor_id&#39;;
    // 如果多个unique key
    // protected $unique = [&#39;first_name&#39;, &#39;last_name&#39;];

}

非常に簡潔ではありませんか? すべての コメント を別にすることもできます。たった一行


例えば

  • 条件なし、テーブル内のすべてのデータを読みたいだけです

<?php
/**
 * 特别预告
 * 想必有人发现需要require的文件可能多了一些
 * 这可以用自动载入来解决
 * 敬请期待
 */
// 辅助函数我都写helper里了
require &#39;lib/helper.php&#39;;
require &#39;lib/Connector.php&#39;;
require &#39;lib/Builder.php&#39;;
require &#39;lib/Grammar.php&#39;;
require &#39;lib/Model.php&#39;;
require &#39;model/Actor.php&#39;;

$a = Actor::get();
dd($a);

結果を返します

array (size=197)
  0 => 
    object(Actor)[207]
      public 'actor_id' => string '1' (length=1)
      public 'first_name' => string 'PENELOPE' (length=8)
      public 'last_name' => string 'GUINESS' (length=7)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  1 => 
    object(Actor)[211]
      public 'actor_id' => string '2' (length=1)
      public 'first_name' => string 'NICK' (length=4)
      public 'last_name' => string 'WAHLBERG' (length=8)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  2 => 
    object(Actor)[215]
      public 'actor_id' => string '3' (length=1)
      public 'first_name' => string 'ED' (length=2)
      public 'last_name' => string 'CHASE' (length=5)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  3 => 
    object(Actor)[219]
      public 'actor_id' => string '4' (length=1)
      public 'first_name' => string 'JENNIFER' (length=8)
      public 'last_name' => string 'DAVIS' (length=5)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)

// 更多数据......

より読みやすく現在の要件に適した書き方

// 这回多一个小要求, 我只想看以下两个声明的字段
$a = Actor::all(['first_name', 'last_name']);

or

$a = Actor::select('first_name', 'last_name')->get();

結果を返す

array (size=197)
  0 => 
    object(Actor)[207]
      public 'first_name' => string 'ADAM' (length=4)
      public 'last_name' => string 'GRANT' (length=5)
  1 => 
    object(Actor)[211]
      public 'first_name' => string 'ADAM' (length=4)
      public 'last_name' => string 'HOPPER' (length=6)
  2 => 
    object(Actor)[215]
      public 'first_name' => string 'AL' (length=2)
      public 'last_name' => string 'GARLAND' (length=7)
  3 => 
    object(Actor)[219]
      public 'first_name' => string 'ALAN' (length=4)
      public 'last_name' => string 'DREYFUSS' (length=8)

......
  • 検索したいfirst_nameには文字Lのデータが含まれていますが、最初の5個のデータは必要ありません。必要に応じて、次の10個のデータを取得します。 first_name フィールドと last_name フィールドを表示するだけで十分です。 後でプレビューする
    ページング
    (paginate) のプロトタイプはこちらです

    $a = Actor::select('first_name', 'last_name')
      ->where('first_name', 'like', '%L%')
      ->skip(5) // 略过5条
      ->take(10) // 拿取10条
      ->get();
    結果を返します

    array (size=10)
    0 => 
      object(Actor)[20]
        public 'first_name' => string 'ANGELA' (length=6)
        public 'last_name' => string 'HUDSON' (length=6)
    1 => 
      object(Actor)[24]
        public 'first_name' => string 'ANGELA' (length=6)
        public 'last_name' => string 'WITHERSPOON' (length=11)
    2 => 
      object(Actor)[28]
        public 'first_name' => string 'ANGELINA' (length=8)
        public 'last_name' => string 'ASTAIRE' (length=7)
    3 => 
      object(Actor)[32]
        public 'first_name' => string 'BELA' (length=4)
        public 'last_name' => string 'WALKEN' (length=6)
    4 => 
      object(Actor)[36]
        public 'first_name' => string 'CHARLIZE' (length=8)
        public 'last_name' => string 'DENCH' (length=5)
    5 => 
      object(Actor)[40]
        public 'first_name' => string 'DARYL' (length=5)
        public 'last_name' => string 'CRAWFORD' (length=8)
    6 => 
      object(Actor)[44]
        public 'first_name' => string 'DARYL' (length=5)
        public 'last_name' => string 'WAHLBERG' (length=8)
    7 => 
      object(Actor)[48]
        public 'first_name' => string 'ELLEN' (length=5)
        public 'last_name' => string 'PRESLEY' (length=7)
    8 => 
      object(Actor)[52]
        public 'first_name' => string 'ELVIS' (length=5)
        public 'last_name' => string 'MARX' (length=4)
    9 => 
      object(Actor)[56]
        public 'first_name' => string 'EMILY' (length=5)
        public 'last_name' => string 'DEE' (length=3)

    他の例が必要ですか?コメントエリアに関数チェーンを記載します


デバッグ

ヒント時々役立つかもしれません。この時点で、SQLステートメントを理解していないとバグにつながります。ネイティブSQLを確認したい場合。ステートメントでは、Connector.php の read() 関数に

// 读取数据
    public function read($sql, $bindings) {
        var_dump($sql); // 就是这一句
        var_dump($bindings); // 还可以确认条件值是否正确对应
        // 将sql语句放入预处理函数
        $statement = $this->connection->prepare($sql);
        // 将附带参数带入pdo实例
        $this->bindValues($statement, $bindings);
        // 执行
        $statement->execute();
        // 返回所有合法数据, 以Object对象为数据类型
        return $statement->fetchAll(PDO::FETCH_OBJ);
    }

を追加できます。最後の例を例にとると、出力は次のようになります。

以上が独自のデータベース パッケージを作成する方法 (4)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。