ホームページ > バックエンド開発 > PHPチュートリアル > 独自の PHP フレームワークを構築する -- モデル クラス 3 を実装する

独自の PHP フレームワークを構築する -- モデル クラス 3 を実装する

WBOY
リリース: 2016-07-29 09:01:00
オリジナル
963 人が閲覧しました

前回のブログでは、Model クラスの findOne メソッドを実装および改良しました。以下の他のメソッドも実装してみましょう。

まず findAll メソッドを見てみましょう。このメソッドは findOne と非常によく似ています。

<code><span>public</span><span>static</span><span>function</span> findOne<span>(</span><span>$condition</span> = <span>null</span><span>)</span>
    {
        <span>$sql</span> = <span>'select * from '</span> . <span>static</span>::tableName<span>();</span><span>$params</span> = <span>[];</span><span>// 判空</span><span>if</span><span>(</span>!<span>empty</span><span>(</span><span>$condition</span><span>))</span> {
            <span>$sql</span> .= <span>' where '</span><span>;</span><span>$params</span> = <span>array_values</span><span>(</span><span>$condition</span><span>);</span><span>$keys</span> = <span>[];</span><span>foreach</span><span>(</span><span>$condition</span><span>as</span><span>$key</span> => <span>$value</span><span>)</span> {
                <span>array_push</span><span>(</span><span>$keys</span><span>,</span><span>"</span><span>$key</span><span> = ?"</span><span>);</span>
            }
            <span>$sql</span> .= <span>implode</span><span>(</span><span>' and '</span><span>,</span><span>$keys</span><span>);</span>
        }

        <span>$stmt</span> = <span>static</span>::getDb<span>()</span>->prepare<span>(</span><span>$sql</span><span>);</span><span>$rs</span> = <span>$stmt</span>->execute<span>(</span><span>$params</span><span>);</span><span>$models</span> = <span>[];</span><span>if</span><span>(</span><span>$rs</span><span>)</span> {
            <span>// 直接获取出所有符合条件的</span><span>$rows</span> = <span>$stmt</span>->fetchAll<span>(</span><span>PDO</span>::<span>FETCH_ASSOC</span><span>);</span><span>foreach</span><span>(</span><span>$rows</span><span>as</span><span>$row</span><span>)</span> {
                <span>if</span><span>(</span>!<span>empty</span><span>(</span><span>$row</span><span>))</span> {
                    <span>$model</span> = <span>new</span><span>static</span><span>();</span><span>foreach</span><span>(</span><span>$row</span><span>as</span><span>$rowKey</span> => <span>$rowValue</span><span>)</span> {
                        <span>$model</span>-><span>$rowKey</span> = <span>$rowValue</span><span>;</span>
                    }
                    <span>array_push</span><span>(</span><span>$models</span><span>,</span><span>$model</span><span>);</span>
                }
            }
        }

        <span>return</span><span>null</span><span>;</span>
    }</code>
ログイン後にコピー

findOne メソッドと findAll メソッドが非常に似ていることがわかります。共通部分を抽出できることは明らかであり、さらに次の 2 つのメソッドがあります。

<code><span>/**</span><span>     * Build a sql where part</span><span>     * </span><span>@param</span><span>mixed</span><span> $condition a set of column values</span><span>     * </span><span>@return</span><span> string</span><span>     */</span><span>public</span><span>static</span><span>function</span> buildWhere<span>(</span><span>$condition</span><span>,</span><span>$params</span> = <span>null</span><span>)</span>
    {
        <span>if</span><span>(</span><span>is_null</span><span>(</span><span>$params</span><span>))</span> {
            <span>$params</span> = <span>[];</span>
        }

        <span>$where</span> = <span>''</span><span>;</span><span>if</span><span>(</span>!<span>empty</span><span>(</span><span>$condition</span><span>))</span> {
            <span>$where</span> .= <span>' where '</span><span>;</span><span>$keys</span> = <span>[];</span><span>foreach</span><span>(</span><span>$condition</span><span>as</span><span>$key</span> => <span>$value</span><span>)</span> {
                <span>array_push</span><span>(</span><span>$keys</span><span>,</span><span>"</span><span>$key</span><span> = ?"</span><span>);</span><span>array_push</span><span>(</span><span>$params</span><span>,</span><span>$value</span><span>);</span>
            }
            <span>$where</span> .= <span>implode</span><span>(</span><span>' and '</span><span>,</span><span>$keys</span><span>);</span>
        }
        <span>return</span><span>[</span><span>$where</span><span>,</span><span>$params</span><span>];</span>
    }

    <span>/**</span><span>     * Convert array to model</span><span>     * </span><span>@param</span><span>mixed</span><span> $row the row data from database</span><span>     */</span><span>public</span><span>static</span><span>function</span> arr2Model<span>(</span><span>$row</span><span>)</span>
    {
        <span>$model</span> = <span>new</span><span>static</span><span>();</span><span>foreach</span><span>(</span><span>$row</span><span>as</span><span>$rowKey</span> => <span>$rowValue</span><span>)</span> {
            <span>$model</span>-><span>$rowKey</span> = <span>$rowValue</span><span>;</span>
        }
        <span>return</span><span>$model</span><span>;</span>
    }</code>
ログイン後にコピー

は、 SQL の一部と、見つかった配列をモデルに変換するメソッドです。なぜ最初のメソッドで params パラメータと戻り値が必要なのか疑問に思われるかもしれませんが、これは後で updateAll メソッドを使用するためです。実際、この場所は参照値渡しを使用するのに適しています。

このようにして、findOne と findAll は次のようになります:

<code><span>/**</span><span>     * Returns a single model instance by a primary key or an array of column values.</span><span>     *</span><span>     * ```php</span><span>     * // find the first customer whose age is 30 and whose status is 1</span><span>     * $customer = Customer::findOne(['age' => 30, 'status' => 1]);</span><span>     * ```</span><span>     *</span><span>     * </span><span>@param</span><span>mixed</span><span> $condition a set of column values</span><span>     * </span><span>@return</span><span> static|null Model instance matching the condition, or null if nothing matches.</span><span>     */</span><span>public</span><span>static</span><span>function</span> findOne<span>(</span><span>$condition</span> = <span>null</span><span>)</span>
    {
        <span>list</span><span>(</span><span>$where</span><span>,</span><span>$params</span><span>)</span> = <span>static</span>::buildWhere<span>(</span><span>$condition</span><span>);</span><span>$sql</span> = <span>'select * from '</span> . <span>static</span>::tableName<span>()</span> . <span>$where</span><span>;</span><span>$stmt</span> = <span>static</span>::getDb<span>()</span>->prepare<span>(</span><span>$sql</span><span>);</span><span>$rs</span> = <span>$stmt</span>->execute<span>(</span><span>$params</span><span>);</span><span>if</span><span>(</span><span>$rs</span><span>)</span> {
            <span>$row</span> = <span>$stmt</span>->fetch<span>(</span><span>PDO</span>::<span>FETCH_ASSOC</span><span>);</span><span>if</span><span>(</span>!<span>empty</span><span>(</span><span>$row</span><span>))</span> {
                <span>return</span><span>static</span>::arr2Model<span>(</span><span>$row</span><span>);</span>
            }
        }

        <span>return</span><span>null</span><span>;</span>
    }

    <span>/**</span><span>     * Returns a list of models that match the specified primary key value(s) or a set of column values.</span><span>     *</span><span>     *  ```php</span><span>     * // find customers whose age is 30 and whose status is 1</span><span>     * $customers = Customer::findAll(['age' => 30, 'status' => 1]);</span><span>     * ```</span><span>     *</span><span>     * </span><span>@param</span><span>mixed</span><span> $condition a set of column values</span><span>     * </span><span>@return</span><span> array an array of Model instance, or an empty array if nothing matches.</span><span>     */</span><span>public</span><span>static</span><span>function</span> findAll<span>(</span><span>$condition</span> = <span>null</span><span>)</span>
    {
        <span>list</span><span>(</span><span>$where</span><span>,</span><span>$params</span><span>)</span> = <span>static</span>::buildWhere<span>(</span><span>$condition</span><span>);</span><span>$sql</span> = <span>'select * from '</span> . <span>static</span>::tableName<span>()</span> . <span>$where</span><span>;</span><span>$stmt</span> = <span>static</span>::getDb<span>()</span>->prepare<span>(</span><span>$sql</span><span>);</span><span>$rs</span> = <span>$stmt</span>->execute<span>(</span><span>$params</span><span>);</span><span>$models</span> = <span>[];</span><span>if</span><span>(</span><span>$rs</span><span>)</span> {
            <span>$rows</span> = <span>$stmt</span>->fetchAll<span>(</span><span>PDO</span>::<span>FETCH_ASSOC</span><span>);</span><span>foreach</span><span>(</span><span>$rows</span><span>as</span><span>$row</span><span>)</span> {
                <span>if</span><span>(</span>!<span>empty</span><span>(</span><span>$row</span><span>))</span> {
                    <span>$model</span> = <span>static</span>::arr2Model<span>(</span><span>$row</span><span>);</span><span>array_push</span><span>(</span><span>$models</span><span>,</span><span>$model</span><span>);</span>
                }
            }
        }

        <span>return</span><span>$models</span><span>;</span>
    }</code>
ログイン後にコピー

残りの updateAll/deleteAll/insert/update および delete メソッドについては、1 つずつ詳しく説明せず、コードを直接示します。基本的な考え方は同じで、すべてルールに従って SQL ステートメントを結合します。

<code><span>/**</span><span>     * Updates models using the provided attribute values and conditions.</span><span>     * For example, to change the status to be 2 for all customers whose status is 1:</span><span>     *</span><span>     * ~~~</span><span>     * Customer::updateAll(['status' => 1], ['status' => '2']);</span><span>     * ~~~</span><span>     *</span><span>     * </span><span>@param</span><span>array</span><span> $attributes attribute values (name-value pairs) to be saved for the model.</span><span>     * </span><span>@param</span><span>array</span><span> $condition the condition that matches the models that should get updated.</span><span>     * An empty condition will match all models.</span><span>     * </span><span>@return</span><span> integer the number of rows updated</span><span>     */</span><span>public</span><span>static</span><span>function</span> updateAll<span>(</span><span>$condition</span><span>,</span><span>$attributes</span><span>)</span>
    {
        <span>$sql</span> = <span>'update '</span> . <span>static</span>::tableName<span>();</span><span>$params</span> = <span>[];</span><span>if</span><span>(</span>!<span>empty</span><span>(</span><span>$attributes</span><span>))</span> {
            <span>$sql</span> .= <span>' set '</span><span>;</span><span>$params</span> = <span>array_values</span><span>(</span><span>$attributes</span><span>);</span><span>$keys</span> = <span>[];</span><span>foreach</span><span>(</span><span>$attributes</span><span>as</span><span>$key</span> => <span>$value</span><span>)</span> {
                <span>array_push</span><span>(</span><span>$keys</span><span>,</span><span>"</span><span>$key</span><span> = ?"</span><span>);</span>
            }
            <span>$sql</span> .= <span>implode</span><span>(</span><span>' , '</span><span>,</span><span>$keys</span><span>);</span>
        }

        <span>list</span><span>(</span><span>$where</span><span>,</span><span>$params</span><span>)</span> = <span>static</span>::buildWhere<span>(</span><span>$condition</span><span>,</span><span>$params</span><span>);</span><span>$sql</span> .= <span>$where</span><span>;</span><span>$stmt</span> = <span>static</span>::getDb<span>()</span>->prepare<span>(</span><span>$sql</span><span>);</span><span>$execResult</span> = <span>$stmt</span>->execute<span>(</span><span>$params</span><span>);</span><span>if</span><span>(</span><span>$execResult</span><span>)</span> {
            <span>// 获取更新的行数</span><span>$execResult</span> = <span>$stmt</span>->rowCount<span>();</span>
        }
        <span>return</span><span>$execResult</span><span>;</span>
    }

    <span>/**</span><span>     * Deletes models using the provided conditions.</span><span>     * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.</span><span>     *</span><span>     * For example, to delete all customers whose status is 3:</span><span>     *</span><span>     * ~~~</span><span>     * Customer::deleteAll([status = 3]);</span><span>     * ~~~</span><span>     *</span><span>     * </span><span>@param</span><span>array</span><span> $condition the condition that matches the models that should get deleted.</span><span>     * An empty condition will match all models.</span><span>     * </span><span>@return</span><span> integer the number of rows deleted</span><span>     */</span><span>public</span><span>static</span><span>function</span> deleteAll<span>(</span><span>$condition</span><span>)</span>
    {
        <span>list</span><span>(</span><span>$where</span><span>,</span><span>$params</span><span>)</span> = <span>static</span>::buildWhere<span>(</span><span>$condition</span><span>);</span><span>$sql</span> = <span>'delete from '</span> . <span>static</span>::tableName<span>()</span> . <span>$where</span><span>;</span><span>$stmt</span> = <span>static</span>::getDb<span>()</span>->prepare<span>(</span><span>$sql</span><span>);</span><span>$execResult</span> = <span>$stmt</span>->execute<span>(</span><span>$params</span><span>);</span><span>if</span><span>(</span><span>$execResult</span><span>)</span> {
            <span>// 获取删除的行数</span><span>$execResult</span> = <span>$stmt</span>->rowCount<span>();</span>
        }
        <span>return</span><span>$execResult</span><span>;</span>
    }

    <span>/**</span><span>     * Inserts the model into the database using the attribute values of this record.</span><span>     *</span><span>     * Usage example:</span><span>     *</span><span>     * ```php</span><span>     * $customer = new Customer;</span><span>     * $customer->name = $name;</span><span>     * $customer->email = $email;</span><span>     * $customer->insert();</span><span>     * ```</span><span>     *</span><span>     * </span><span>@return</span><span> boolean whether the model is inserted successfully.</span><span>     */</span><span>public</span><span>function</span> insert<span>()</span>
    {
        <span>$sql</span> = <span>'insert into '</span> . <span>static</span>::tableName<span>();</span><span>$params</span> = <span>[];</span><span>$keys</span> = <span>[];</span><span>foreach</span><span>(</span><span>$this</span><span>as</span><span>$key</span> => <span>$value</span><span>)</span> {
            <span>array_push</span><span>(</span><span>$keys</span><span>,</span><span>$key</span><span>);</span><span>array_push</span><span>(</span><span>$params</span><span>,</span><span>$value</span><span>);</span>
        }
        <span>// 构建由?组成的数组,其个数与参数相等数相同</span><span>$holders</span> = <span>array_fill</span><span>(</span><span>0</span><span>,</span><span>count</span><span>(</span><span>$keys</span><span>),</span><span>'?'</span><span>);</span><span>$sql</span> .= <span>' ('</span> . <span>implode</span><span>(</span><span>' , '</span><span>,</span><span>$keys</span><span>)</span> . <span>') values ( '</span> . <span>implode</span><span>(</span><span>' , '</span><span>,</span><span>$holders</span><span>)</span> . <span>')'</span><span>;</span><span>$stmt</span> = <span>static</span>::getDb<span>()</span>->prepare<span>(</span><span>$sql</span><span>);</span><span>$execResult</span> = <span>$stmt</span>->execute<span>(</span><span>$params</span><span>);</span><span>// 将一些自增值赋回Model中</span><span>$primaryKeys</span> = <span>static</span>::primaryKey<span>();</span><span>foreach</span><span>(</span><span>$primaryKeys</span><span>as</span><span>$name</span><span>)</span> {
            <span>// Get the primary key</span><span>$lastId</span> = <span>static</span>::getDb<span>()</span>->lastInsertId<span>(</span><span>$name</span><span>);</span><span>$this</span>-><span>$name</span> = <span>(int)</span><span>$lastId</span><span>;</span>
        }
        <span>return</span><span>$execResult</span><span>;</span>
    }

    <span>/**</span><span>     * Saves the changes to this model into the database.</span><span>     *</span><span>     * Usage example:</span><span>     *</span><span>     * ```php</span><span>     * $customer = Customer::findOne(['id' => $id]);</span><span>     * $customer->name = $name;</span><span>     * $customer->email = $email;</span><span>     * $customer->update();</span><span>     * ```</span><span>     *</span><span>     * </span><span>@return</span><span> integer|boolean the number of rows affected.</span><span>     * Note that it is possible that the number of rows affected is 0, even though the</span><span>     * update execution is successful.</span><span>     */</span><span>public</span><span>function</span> update<span>()</span>
    {
        <span>$primaryKeys</span> = <span>static</span>::primaryKey<span>();</span><span>$condition</span> = <span>[];</span><span>foreach</span><span>(</span><span>$primaryKeys</span><span>as</span><span>$name</span><span>)</span> {
            <span>$condition</span><span>[</span><span>$name</span><span>]</span> = <span>isset</span><span>(</span><span>$this</span>-><span>$name</span><span>)</span><span>?</span><span>$this</span>-><span>$name</span><span>:</span><span>null</span><span>;</span>
        }

        <span>$attributes</span> = <span>[];</span><span>foreach</span><span>(</span><span>$this</span><span>as</span><span>$key</span> => <span>$value</span><span>)</span> {
            <span>if</span><span>(</span>!<span>in_array</span><span>(</span><span>$key</span><span>,</span><span>$primaryKeys</span><span>,</span><span>true</span><span>))</span> {
                <span>$attributes</span><span>[</span><span>$key</span><span>]</span> = <span>$value</span><span>;</span>
            }
        }

        <span>return</span><span>static</span>::updateAll<span>(</span><span>$condition</span><span>,</span><span>$attributes</span><span>)</span> !== <span>false</span><span>;</span>
    }

    <span>/**</span><span>     * Deletes the model from the database.</span><span>     *</span><span>     * </span><span>@return</span><span> integer|boolean the number of rows deleted.</span><span>     * Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.</span><span>     */</span><span>public</span><span>function</span> delete<span>()</span>
    {
        <span>$primaryKeys</span> = <span>static</span>::primaryKey<span>();</span><span>$condition</span> = <span>[];</span><span>foreach</span><span>(</span><span>$primaryKeys</span><span>as</span><span>$name</span><span>)</span> {
            <span>$condition</span><span>[</span><span>$name</span><span>]</span> = <span>isset</span><span>(</span><span>$this</span>-><span>$name</span><span>)</span><span>?</span><span>$this</span>-><span>$name</span><span>:</span><span>null</span><span>;</span>
        }

        <span>return</span><span>static</span>::deleteAll<span>(</span><span>$condition</span><span>)</span> !== <span>false</span><span>;</span>
    }</code>
ログイン後にコピー

この基本的なモデルはまだ多くの問題や制限があるかもしれませんが、機会があれば段階的に改善していきます。

さて、今日はここまでにしましょう。プロジェクトの内容やブログの内容も Github に載せる予定ですので、皆様からのご提案をお待ちしております。

コード: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.7

ブログプロジェクト: https://github.com/CraryPrimitiveMan/create-your-own-php-framework

上記は、独自の PHP フレームワークを実装する Model クラス 3 の構築を、内容の側面も含めて紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート