Maison >développement back-end >tutoriel php >构建自个儿的PHP框架-实现Model类(3)

构建自个儿的PHP框架-实现Model类(3)

WBOY
WBOYoriginal
2016-06-13 12:28:43702parcourir

构建自己的PHP框架--实现Model类(3)

在之前的博客中,我们实现并完善了Model类的findOne方法,下面我们来实现其中的其他方法。

先来看findAll方法,这个方法和findOne很相似。

<code class="sourceCode php">    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> findOne<span class="ot">(</span><span class="kw">$condition</span> = <span class="kw">null</span><span class="ot">)</span>    {        <span class="kw">$sql</span> = <span class="st">&#39;select * from &#39;</span> . <span class="kw">static</span>::tableName<span class="ot">();</span>        <span class="kw">$params</span> = <span class="ot">[];</span>        <span class="co">// 判空</span>        <span class="kw">if</span> <span class="ot">(</span>!<span class="fu">empty</span><span class="ot">(</span><span class="kw">$condition</span><span class="ot">))</span> {            <span class="kw">$sql</span> .= <span class="st">&#39; where &#39;</span><span class="ot">;</span>            <span class="kw">$params</span> = <span class="fu">array_values</span><span class="ot">(</span><span class="kw">$condition</span><span class="ot">);</span>            <span class="kw">$keys</span> = <span class="ot">[];</span>            <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$condition</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$value</span><span class="ot">)</span> {                <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$keys</span><span class="ot">,</span> <span class="st">"</span><span class="kw">$key</span><span class="st"> = ?"</span><span class="ot">);</span>            }            <span class="kw">$sql</span> .= <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39; and &#39;</span><span class="ot">,</span> <span class="kw">$keys</span><span class="ot">);</span>        }        <span class="kw">$stmt</span> = <span class="kw">static</span>::getDb<span class="ot">()</span>->prepare<span class="ot">(</span><span class="kw">$sql</span><span class="ot">);</span>        <span class="kw">$rs</span> = <span class="kw">$stmt</span>->execute<span class="ot">(</span><span class="kw">$params</span><span class="ot">);</span>        <span class="kw">$models</span> = <span class="ot">[];</span>        <span class="kw">if</span> <span class="ot">(</span><span class="kw">$rs</span><span class="ot">)</span> {            <span class="co">// 直接获取出所有符合条件的</span>            <span class="kw">$rows</span> = <span class="kw">$stmt</span>->fetchAll<span class="ot">(</span><span class="kw">PDO</span>::<span class="kw">FETCH_ASSOC</span><span class="ot">);</span>            <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$rows</span> <span class="kw">as</span> <span class="kw">$row</span><span class="ot">)</span> {                <span class="kw">if</span> <span class="ot">(</span>!<span class="fu">empty</span><span class="ot">(</span><span class="kw">$row</span><span class="ot">))</span> {                    <span class="kw">$model</span> = <span class="kw">new</span> <span class="kw">static</span><span class="ot">();</span>                    <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$row</span> <span class="kw">as</span> <span class="kw">$rowKey</span> => <span class="kw">$rowValue</span><span class="ot">)</span> {                        <span class="kw">$model</span>-><span class="kw">$rowKey</span> = <span class="kw">$rowValue</span><span class="ot">;</span>                    }                    <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$models</span><span class="ot">,</span> <span class="kw">$model</span><span class="ot">);</span>                }            }        }        <span class="kw">return</span> <span class="kw">null</span><span class="ot">;</span>    }</code>

你会发现有findOne和findAll方法很相似,明显可以将公共的部分抽出来,然后我们就多了如下两个方法:

<code class="sourceCode php">    <span class="co">/**</span><span class="co">     * Build a sql where part</span><span class="co">     * </span><span class="kw">@param</span><span class="co"> </span><span class="kw">mixed</span><span class="co"> $condition a set of column values</span><span class="co">     * </span><span class="kw">@return</span><span class="co"> string</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> buildWhere<span class="ot">(</span><span class="kw">$condition</span><span class="ot">,</span> <span class="kw">$params</span> = <span class="kw">null</span><span class="ot">)</span>    {        <span class="kw">if</span> <span class="ot">(</span><span class="fu">is_null</span><span class="ot">(</span><span class="kw">$params</span><span class="ot">))</span> {            <span class="kw">$params</span> = <span class="ot">[];</span>        }        <span class="kw">$where</span> = <span class="st">&#39;&#39;</span><span class="ot">;</span>        <span class="kw">if</span> <span class="ot">(</span>!<span class="fu">empty</span><span class="ot">(</span><span class="kw">$condition</span><span class="ot">))</span> {            <span class="kw">$where</span> .= <span class="st">&#39; where &#39;</span><span class="ot">;</span>            <span class="kw">$keys</span> = <span class="ot">[];</span>            <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$condition</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$value</span><span class="ot">)</span> {                <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$keys</span><span class="ot">,</span> <span class="st">"</span><span class="kw">$key</span><span class="st"> = ?"</span><span class="ot">);</span>                <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$params</span><span class="ot">,</span> <span class="kw">$value</span><span class="ot">);</span>            }            <span class="kw">$where</span> .= <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39; and &#39;</span><span class="ot">,</span> <span class="kw">$keys</span><span class="ot">);</span>        }        <span class="kw">return</span> <span class="ot">[</span><span class="kw">$where</span><span class="ot">,</span> <span class="kw">$params</span><span class="ot">];</span>    }    <span class="co">/**</span><span class="co">     * Convert array to model</span><span class="co">     * </span><span class="kw">@param</span><span class="co">  </span><span class="kw">mixed</span><span class="co"> $row the row data from database</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> arr2Model<span class="ot">(</span><span class="kw">$row</span><span class="ot">)</span>    {        <span class="kw">$model</span> = <span class="kw">new</span> <span class="kw">static</span><span class="ot">();</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$row</span> <span class="kw">as</span> <span class="kw">$rowKey</span> => <span class="kw">$rowValue</span><span class="ot">)</span> {            <span class="kw">$model</span>-><span class="kw">$rowKey</span> = <span class="kw">$rowValue</span><span class="ot">;</span>        }        <span class="kw">return</span> <span class="kw">$model</span><span class="ot">;</span>    }</code>

分别是构建sql中where部分的方法和将查找到的Array转换成Model的方法。大家会奇怪第一个方法中为什么需要params参数和返回值,其实这个为了之后的updateAll方法的使用。其实这个地方跟适合使用引用传值。

这样我们的findOne和findAll就便成了如下内容:

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

剩下的updateAll/deleteAll/insert/update和delete方法就不一一详细说明了,直接给出代码。其基本思想都是一致的,都是按照规则拼接SQL语句。

<code class="sourceCode php">    <span class="co">/**</span><span class="co">     * Updates models using the provided attribute values and conditions.</span><span class="co">     * For example, to change the status to be 2 for all customers whose status is 1:</span><span class="co">     *</span><span class="co">     * ~~~</span><span class="co">     * Customer::updateAll([&#39;status&#39; => 1], [&#39;status&#39; => &#39;2&#39;]);</span><span class="co">     * ~~~</span><span class="co">     *</span><span class="co">     * </span><span class="kw">@param</span><span class="co"> </span><span class="kw">array</span><span class="co"> $attributes attribute values (name-value pairs) to be saved for the model.</span><span class="co">     * </span><span class="kw">@param</span><span class="co"> </span><span class="kw">array</span><span class="co"> $condition the condition that matches the models that should get updated.</span><span class="co">     * An empty condition will match all models.</span><span class="co">     * </span><span class="kw">@return</span><span class="co"> integer the number of rows updated</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> updateAll<span class="ot">(</span><span class="kw">$condition</span><span class="ot">,</span> <span class="kw">$attributes</span><span class="ot">)</span>    {        <span class="kw">$sql</span> = <span class="st">&#39;update &#39;</span> . <span class="kw">static</span>::tableName<span class="ot">();</span>        <span class="kw">$params</span> = <span class="ot">[];</span>        <span class="kw">if</span> <span class="ot">(</span>!<span class="fu">empty</span><span class="ot">(</span><span class="kw">$attributes</span><span class="ot">))</span> {            <span class="kw">$sql</span> .= <span class="st">&#39; set &#39;</span><span class="ot">;</span>            <span class="kw">$params</span> = <span class="fu">array_values</span><span class="ot">(</span><span class="kw">$attributes</span><span class="ot">);</span>            <span class="kw">$keys</span> = <span class="ot">[];</span>            <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$attributes</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$value</span><span class="ot">)</span> {                <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$keys</span><span class="ot">,</span> <span class="st">"</span><span class="kw">$key</span><span class="st"> = ?"</span><span class="ot">);</span>            }            <span class="kw">$sql</span> .= <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39; , &#39;</span><span class="ot">,</span> <span class="kw">$keys</span><span class="ot">);</span>        }        <span class="fu">list</span><span class="ot">(</span><span class="kw">$where</span><span class="ot">,</span> <span class="kw">$params</span><span class="ot">)</span> = <span class="kw">static</span>::buildWhere<span class="ot">(</span><span class="kw">$condition</span><span class="ot">,</span> <span class="kw">$params</span><span class="ot">);</span>        <span class="kw">$sql</span> .= <span class="kw">$where</span><span class="ot">;</span>        <span class="kw">$stmt</span> = <span class="kw">static</span>::getDb<span class="ot">()</span>->prepare<span class="ot">(</span><span class="kw">$sql</span><span class="ot">);</span>        <span class="kw">$execResult</span> = <span class="kw">$stmt</span>->execute<span class="ot">(</span><span class="kw">$params</span><span class="ot">);</span>        <span class="kw">if</span> <span class="ot">(</span><span class="kw">$execResult</span><span class="ot">)</span> {            <span class="co">// 获取更新的行数</span>            <span class="kw">$execResult</span> = <span class="kw">$stmt</span>->rowCount<span class="ot">();</span>        }        <span class="kw">return</span> <span class="kw">$execResult</span><span class="ot">;</span>    }    <span class="co">/**</span><span class="co">     * Deletes models using the provided conditions.</span><span class="co">     * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.</span><span class="co">     *</span><span class="co">     * For example, to delete all customers whose status is 3:</span><span class="co">     *</span><span class="co">     * ~~~</span><span class="co">     * Customer::deleteAll([status = 3]);</span><span class="co">     * ~~~</span><span class="co">     *</span><span class="co">     * </span><span class="kw">@param</span><span class="co"> </span><span class="kw">array</span><span class="co"> $condition the condition that matches the models that should get deleted.</span><span class="co">     * An empty condition will match all models.</span><span class="co">     * </span><span class="kw">@return</span><span class="co"> integer the number of rows deleted</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> deleteAll<span class="ot">(</span><span class="kw">$condition</span><span class="ot">)</span>    {        <span class="fu">list</span><span class="ot">(</span><span class="kw">$where</span><span class="ot">,</span> <span class="kw">$params</span><span class="ot">)</span> = <span class="kw">static</span>::buildWhere<span class="ot">(</span><span class="kw">$condition</span><span class="ot">);</span>        <span class="kw">$sql</span> = <span class="st">&#39;delete from &#39;</span> . <span class="kw">static</span>::tableName<span class="ot">()</span> . <span class="kw">$where</span><span class="ot">;</span>        <span class="kw">$stmt</span> = <span class="kw">static</span>::getDb<span class="ot">()</span>->prepare<span class="ot">(</span><span class="kw">$sql</span><span class="ot">);</span>        <span class="kw">$execResult</span> = <span class="kw">$stmt</span>->execute<span class="ot">(</span><span class="kw">$params</span><span class="ot">);</span>        <span class="kw">if</span> <span class="ot">(</span><span class="kw">$execResult</span><span class="ot">)</span> {            <span class="co">// 获取删除的行数</span>            <span class="kw">$execResult</span> = <span class="kw">$stmt</span>->rowCount<span class="ot">();</span>        }        <span class="kw">return</span> <span class="kw">$execResult</span><span class="ot">;</span>    }    <span class="co">/**</span><span class="co">     * Inserts the model into the database using the attribute values of this record.</span><span class="co">     *</span><span class="co">     * Usage example:</span><span class="co">     *</span><span class="co">     * ```php</span><span class="co">     * $customer = new Customer;</span><span class="co">     * $customer->name = $name;</span><span class="co">     * $customer->email = $email;</span><span class="co">     * $customer->insert();</span><span class="co">     * ```</span><span class="co">     *</span><span class="co">     * </span><span class="kw">@return</span><span class="co"> boolean whether the model is inserted successfully.</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">function</span> insert<span class="ot">()</span>    {        <span class="kw">$sql</span> = <span class="st">&#39;insert into &#39;</span> . <span class="kw">static</span>::tableName<span class="ot">();</span>        <span class="kw">$params</span> = <span class="ot">[];</span>        <span class="kw">$keys</span> = <span class="ot">[];</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$this</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$value</span><span class="ot">)</span> {            <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$keys</span><span class="ot">,</span> <span class="kw">$key</span><span class="ot">);</span>            <span class="fu">array_push</span><span class="ot">(</span><span class="kw">$params</span><span class="ot">,</span> <span class="kw">$value</span><span class="ot">);</span>        }        <span class="co">// 构建由?组成的数组,其个数与参数相等数相同</span>        <span class="kw">$holders</span> = <span class="fu">array_fill</span><span class="ot">(</span><span class="dv">0</span><span class="ot">,</span> <span class="fu">count</span><span class="ot">(</span><span class="kw">$keys</span><span class="ot">),</span> <span class="st">&#39;?&#39;</span><span class="ot">);</span>        <span class="kw">$sql</span> .= <span class="st">&#39; (&#39;</span> . <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39; , &#39;</span><span class="ot">,</span> <span class="kw">$keys</span><span class="ot">)</span> . <span class="st">&#39;) values ( &#39;</span> . <span class="fu">implode</span><span class="ot">(</span><span class="st">&#39; , &#39;</span><span class="ot">,</span> <span class="kw">$holders</span><span class="ot">)</span> . <span class="st">&#39;)&#39;</span><span class="ot">;</span>        <span class="kw">$stmt</span> = <span class="kw">static</span>::getDb<span class="ot">()</span>->prepare<span class="ot">(</span><span class="kw">$sql</span><span class="ot">);</span>        <span class="kw">$execResult</span> = <span class="kw">$stmt</span>->execute<span class="ot">(</span><span class="kw">$params</span><span class="ot">);</span>        <span class="co">// 将一些自增值赋回Model中</span>        <span class="kw">$primaryKeys</span> = <span class="kw">static</span>::primaryKey<span class="ot">();</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$primaryKeys</span> <span class="kw">as</span> <span class="kw">$name</span><span class="ot">)</span> {            <span class="co">// Get the primary key</span>            <span class="kw">$lastId</span> = <span class="kw">static</span>::getDb<span class="ot">()</span>->lastInsertId<span class="ot">(</span><span class="kw">$name</span><span class="ot">);</span>            <span class="kw">$this</span>-><span class="kw">$name</span> = <span class="dt">(int)</span> <span class="kw">$lastId</span><span class="ot">;</span>        }        <span class="kw">return</span> <span class="kw">$execResult</span><span class="ot">;</span>    }    <span class="co">/**</span><span class="co">     * Saves the changes to this model into the database.</span><span class="co">     *</span><span class="co">     * Usage example:</span><span class="co">     *</span><span class="co">     * ```php</span><span class="co">     * $customer = Customer::findOne([&#39;id&#39; => $id]);</span><span class="co">     * $customer->name = $name;</span><span class="co">     * $customer->email = $email;</span><span class="co">     * $customer->update();</span><span class="co">     * ```</span><span class="co">     *</span><span class="co">     * </span><span class="kw">@return</span><span class="co"> integer|boolean the number of rows affected.</span><span class="co">     * Note that it is possible that the number of rows affected is 0, even though the</span><span class="co">     * update execution is successful.</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">()</span>    {        <span class="kw">$primaryKeys</span> = <span class="kw">static</span>::primaryKey<span class="ot">();</span>        <span class="kw">$condition</span> = <span class="ot">[];</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$primaryKeys</span> <span class="kw">as</span> <span class="kw">$name</span><span class="ot">)</span> {            <span class="kw">$condition</span><span class="ot">[</span><span class="kw">$name</span><span class="ot">]</span> = <span class="fu">isset</span><span class="ot">(</span><span class="kw">$this</span>-><span class="kw">$name</span><span class="ot">)</span> <span class="ot">?</span> <span class="kw">$this</span>-><span class="kw">$name</span> <span class="ot">:</span> <span class="kw">null</span><span class="ot">;</span>        }        <span class="kw">$attributes</span> = <span class="ot">[];</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$this</span> <span class="kw">as</span> <span class="kw">$key</span> => <span class="kw">$value</span><span class="ot">)</span> {            <span class="kw">if</span> <span class="ot">(</span>!<span class="fu">in_array</span><span class="ot">(</span><span class="kw">$key</span><span class="ot">,</span> <span class="kw">$primaryKeys</span><span class="ot">,</span> <span class="kw">true</span><span class="ot">))</span> {                <span class="kw">$attributes</span><span class="ot">[</span><span class="kw">$key</span><span class="ot">]</span> = <span class="kw">$value</span><span class="ot">;</span>            }        }        <span class="kw">return</span> <span class="kw">static</span>::updateAll<span class="ot">(</span><span class="kw">$condition</span><span class="ot">,</span> <span class="kw">$attributes</span><span class="ot">)</span> !== <span class="kw">false</span><span class="ot">;</span>    }    <span class="co">/**</span><span class="co">     * Deletes the model from the database.</span><span class="co">     *</span><span class="co">     * </span><span class="kw">@return</span><span class="co"> integer|boolean the number of rows deleted.</span><span class="co">     * Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.</span><span class="co">     */</span>    <span class="kw">public</span> <span class="kw">function</span> delete<span class="ot">()</span>    {        <span class="kw">$primaryKeys</span> = <span class="kw">static</span>::primaryKey<span class="ot">();</span>        <span class="kw">$condition</span> = <span class="ot">[];</span>        <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$primaryKeys</span> <span class="kw">as</span> <span class="kw">$name</span><span class="ot">)</span> {            <span class="kw">$condition</span><span class="ot">[</span><span class="kw">$name</span><span class="ot">]</span> = <span class="fu">isset</span><span class="ot">(</span><span class="kw">$this</span>-><span class="kw">$name</span><span class="ot">)</span> <span class="ot">?</span> <span class="kw">$this</span>-><span class="kw">$name</span> <span class="ot">:</span> <span class="kw">null</span><span class="ot">;</span>        }        <span class="kw">return</span> <span class="kw">static</span>::deleteAll<span class="ot">(</span><span class="kw">$condition</span><span class="ot">)</span> !== <span class="kw">false</span><span class="ot">;</span>    }</code>

这样基本的Model就算是暂时完成了,虽然可能还有很多问题和局限,但暂时先这样了,我们之后有机会会一步一步的去完善。

好了,今天就先到这里。项目内容和博客内容也都会放到Github上,欢迎大家提建议。

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

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

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn