Home  >  Article  >  Backend Development  >  Build your own PHP framework--implement Model class 3

Build your own PHP framework--implement Model class 3

WBOY
WBOYOriginal
2016-07-29 09:01:00937browse

In the previous blog, we implemented and improved the findOne method of the Model class. Let’s implement the other methods below.

Let’s first look at the findAll method. This method is very similar to 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>

You will find that the findOne and findAll methods are very similar. It is obvious that the common part can be extracted, and then we have the following two methods:

<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>

are the method of constructing the where part of sql and Method to convert the found Array into a Model. You may wonder why the params parameter and return value are needed in the first method. In fact, this is for the later use of the updateAll method. In fact, this place is suitable for using reference pass-by-value.

In this way, our findOne and findAll will become the following:

<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>

The remaining updateAll/deleteAll/insert/update and delete methods will not be explained in detail one by one, and the codes will be given directly. The basic ideas are the same, and they all splice SQL statements according to rules.

<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>

This basic Model is temporarily completed. Although there may still be many problems and limitations, this is it for now. We will improve it step by step when we have the opportunity.

Okay, let’s stop here today. Project content and blog content will also be put on Github, and everyone is welcome to make suggestions.

code: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.7

blog project: https://github.com/CraryPrimitiveMan/create-your-own-php-framework

The above introduces the construction of your own PHP framework-implementing Model class 3, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn