在之前的博客中,我们实现并完善了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方法很相似,明显可以将公共的部分抽出来,然后我们就多了如下两个方法:
<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中where部分的方法和将查找到的Array转换成Model的方法。大家会奇怪第一个方法中为什么需要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方法就不一一详细说明了,直接给出代码。其基本思想都是一致的,都是按照规则拼接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>
这样基本的Model就算是暂时完成了,虽然可能还有很多问题和局限,但暂时先这样了,我们之后有机会会一步一步的去完善。
好了,今天就先到这里。项目内容和博客内容也都会放到Github上,欢迎大家提建议。
code:https://github.com/CraryPrimitiveMan/simple-framework/tree/0.7
blog project:https://github.com/CraryPrimitiveMan/create-your-own-php-framework
以上就介绍了构建自己的PHP框架--实现Model类3,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。