이전 블로그에서는 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 및 삭제 방법은 다릅니다. 자세한 내용은 코드가 직접 제공됩니다. 기본 아이디어는 동일하며 모두 규칙에 따라 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>
이 기본 모델은 아직은 많은 문제와 한계가 있을 수 있지만 기회가 되면 단계적으로 개선해 나가겠습니다. .
자, 오늘은 여기서 그만합시다. 프로젝트 콘텐츠와 블로그 콘텐츠도 Github에 게시될 예정이며 누구나 제안을 환영합니다.
코드: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.7
블로그 프로젝트: https://github.com/CraryPrimitiveMan/create-your-own- PHP 프레임워크
위 내용은 내용의 측면을 포함하여 자신만의 PHP 프레임워크를 구현하는 모델 클래스 3의 구성을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.