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

构建自己的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

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
Trezor冷钱包:Model One与Model T的特性和使用指南Trezor冷钱包:Model One与Model T的特性和使用指南Jan 19, 2024 pm 04:12 PM

在许多中心化交易所出现问题后,越来越多的币圈投资者开始将资产转移到冷钱包中,以减少中心化交易所带来的风险。本文将介绍全球最早的冷钱包供应商Trezor,自2014年推出首款冷钱包至今,在全球多个国家销售。Trezor的产品包括2014年推出的ModelOne和2018年推出的进阶版本ModelT。下面将继续介绍这两款产品与其他冷钱包的区别。什么是Trezor冷钱包?2014年,Trezor推出了第一款冷钱包ModelOne。除了常见的BTC、ETH、USDT等币种外,该钱包还支持超过1000种其

c语言static的作用和用法是什么c语言static的作用和用法是什么Jan 31, 2024 pm 01:59 PM

c语言static的作用和用法:1、变量作用域;2、生命周期;3、函数内部;4、修饰全局变量;5、修饰函数;6、其他用途;详细介绍:1、变量作用域,当一个变量前有static关键字,那么这个变量的作用域被限制在声明它的文件内,也就是说,这个变量是“文件级作用域”,这对于防止变量的“重复定义”问题很有用;2、生命周期,静态变量在程序开始执行时初始化一次,并在程序结束时销毁等等。

Java中的static、this、super、final怎么使用Java中的static、this、super、final怎么使用Apr 18, 2023 pm 03:40 PM

一、static  请先看下面这段程序:publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello,world!");//(2)}}看过这段程序,对于大多数学过Java的从来说,都不陌生。即使没有学过Java,而学过其它的高级语言,例如C,那你也应该能看懂这段代码的意思。它只是简单的输出“Hello,world”,一点别的用处都没有,然而,它却展示了static关键字的主

Django框架中的Model详解Django框架中的Model详解Jun 17, 2023 am 08:48 AM

Django是一个开源的Pythonweb框架,它采用了MVT(Model-View-Template)的架构模式,将应用程序分为模型(Model)、视图(View)和模板(Template)三个部分。其中,Model是Django框架中的一个基础组件,用于对数据进行定义和管理。本文将对Django框架中的Model进行详解。什么是Model在Django

C语言中static关键字的实际应用场景及使用技巧C语言中static关键字的实际应用场景及使用技巧Feb 21, 2024 pm 07:21 PM

C语言中static关键字的实际应用场景及使用技巧一、概述static是C语言中的一个关键字,用于修饰变量和函数。它的作用是改变其在程序运行过程中的生命周期和可见性,使得变量和函数具有静态的特性。本文将介绍static关键字的实际应用场景及使用技巧,并通过具体的代码示例进行说明。二、静态变量延长变量的生命周期使用static关键字修饰局部变量可以将其生命周期

static的作用static的作用Jan 24, 2024 pm 04:08 PM

static的作用:1、变量;2、方法;3、类;4、其他用途;5、多线程环境;6、性能优化;7、单例模式;8、常量;9、局部变量;10、内存布局优化;11、避免重复初始化;12、在函数中使用。详细介绍:1、变量,静态变量,当一个变量被声明为static时,它属于类级别,而不是实例级别,这意味着无论创建多少个对象,都只有一个静态变量存在,所有对象都共享这个静态变量等等。

php的static静态方法是什么php的static静态方法是什么Oct 31, 2022 am 09:40 AM

php static静态方法中的“静态”指的是无需对类进行实例化,就可以直接调用这些属性和方法;而static就是一个关键字,用来修饰类的属性及方法,其使用语法如“class Foo {public static $my_static = 'hello';}”。

Java修饰符abstract、static和final怎么用Java修饰符abstract、static和final怎么用Apr 26, 2023 am 09:46 AM

修饰符abstract(抽象的)一、abstract可以修饰类(1)被abstract修饰的类称为抽象类(2)语法:abstractclass类名{}(3)特点:抽象类不能单独创建对象,但是可以声明引用抽象类类名引用名;(4)抽象类可以定义成员变量和成员方法(5)抽象类有构造方法,用于创建子类对象时,jvm默认创建一个父类对象;抽象的构造方法应用在jvm创建父类对象时应用。二、abstract可以修饰方法(1)被asbtract修饰的方法被称为抽象方法(2)语法:访问修饰符abstract返回值

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool