在之前的部落格中,我們實作並完善了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>
分別是構建中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教程有興趣的朋友有所幫助。