In our previous blog, we implemented and perfected the FindOne method of the model class, and we implemented the other methods below.
First look at the FindAll method, which is similar to FindOne.
public static function FindOne ($condition = null) {$sql = ' select * from '. Static::tablename (); $params = []; Empty if (!empty ($condition)) {$sql. = ' where '; $params = Array_values ($condition); $keys = []; foreach ($condition as $key = = $value) {Array_push ($keys, "$key =?"); } $sql. = Implode (' and ', $keys); } $stmt = Static::getdb ()->prepare ($sql); $rs = $stmt->execute ($params); $models = []; if ($RS) {//directly obtains all eligible $rows = $stmt->fetchall (PDO::FETCH_ASSOC); foreach ($rows as $row) {if (!empty ($row)) {$model = new static (); foreach ($row as $rowKey = + $rowValue) {$model $rowKey = $rowValue; } array_push ($models, $model); } } } return null; }
You will find that there are findone and FindAll methods that are very similar, and obviously can be pulled out of the public sector, and then we have the following two methods:
/** * Build a SQL where part * @param mixed $condition A set of column values * @return String */public static Functi On Buildwhere ($condition, $params = null) { if (Is_null ($params)) { $params = []; } $where = "; if (!empty ($condition)) { $where. = ' where '; $keys = []; foreach ($condition as $key = = $value) { Array_push ($keys, "$key =?"); Array_push ($params, $value); } $where. = Implode (' and ', $keys); } return [$where, $params]; } /** * Convert Array to model * @param mixed $row The row data from database * /public static function Arr2model ($row) { $model = new static (); foreach ($row as $rowKey = $rowValue) { $model $rowKey = $rowValue; } return $model; }
The method for constructing the where part of SQL and the method of converting the found array into model are respectively. Everyone will wonder why the first method requires the params parameter and the return value, in fact, for the later use of the UpdateAll method. In fact this place is suitable for use with reference values.
In this way our findone and FindAll handy become the following:
/** * Returns A single model instance by a primary key or an array of values. * * "PHP *//Find the first customer whose age is" and whose status is 1 * $customer = Customer::findone "[' Age ' = ' Status ' = 1]); * ' * * * @param mixed $condition A set of column values * @return static|null Model instance matching the condition, or n ull if nothing matches. */public static function FindOne ($condition = null) {list ($where, $params) = Static::buildwhere ($condition) ; $sql = ' select * from '. Static::tablename (). $where; $stmt = Static::getdb ()->prepare ($sql); $rs = $stmt->execute ($params); if ($rs) {$row = $stmt->fetch (PDO::FETCH_ASSOC); if (!empty ($row)) {return Static::arr2model ($row); }} return null; }/** * Returns A list of models that match the specified primary key value (s) or a set of column values. * * ' PHP *//Find customers whosewhose status is 1 * $customers = Customer::findall ([' age ' = ' + ', ' status ' = 1]); * ' * * * @param mixed $condition A set of column values * @return array An array of Model instance, or an empty array if Nothing matches. */public static function findAll ($condition = null) {list ($where, $params) = Static::buildwhere ($condition) ; $sql = ' select * from '. Static::tablename (). $where; $stmt = Static::getdb ()->prepare ($sql); $rs = $stmt->execute ($params); $models = []; if ($rs) {$rows = $stmt->fetchall (PDO::FETCH_ASSOC); foreach ($rows as $row) {if (!empty ($row)) {$model = Static::arr2model ($row); Array_push ($models, $model); }}} return $models; }
The rest of the updateall/deleteall/insert/update and delete methods are not explained in detail, and the code is given directly. The basic ideas are consistent, and are all based on the rules to splice SQL statements.
/** * Updates models using the provided attribute values and conditions. * For example, to change the status of 2 for all customers whose status is 1: * ~ ~ ~ ~ Customer::updateall ([' Status ' = > 1], [' status ' = ' 2 ']); * ~ ~ ~ * @param array $attributes attribute values (Name-value pairs) to being saved for the model. * @param array $condition The condition that matches the models that should get updated. * An empty condition would match all models. * @return Integer The number of rows updated */public static function UpdateAll ($condition, $attributes) {$ sql = ' Update '. Static::tablename (); $params = []; if (!empty ($attributes)) {$sql. = ' Set '; $params = Array_values ($attributes); $keys = []; foreach ($attributes as $key = = $value) {Array_push ($keys, "$key =?"); } $sql. = Implode (', ', $keys); } list ($where, $params) = Static::buildwhere ($conditIon, $params); $sql. = $where; $stmt = Static::getdb ()->prepare ($sql); $execResult = $stmt->execute ($params); if ($execResult) {//Gets the updated number of rows $execResult = $stmt->rowcount (); } return $execResult; }/** * Deletes models using the provided conditions. * Warning:if You don't specify any condition, this method would delete all rows in the table. * For example, to-delete all customers whose status is 3: * * ~ ~ ~ * Customer::d eleteall ([status = 3]); * ~ ~ ~ * @param array $condition The condition that matches the models that should get deleted. * An empty condition would match all models. * @return Integer The number of rows deleted */public static function DeleteAll ($condition) {list ($where, $ params) = Static::buildwhere ($condition); $sql = ' Delete from '. Static::tablename (). $where; $stmt = Static::getdb ()->prepare ($sql); $execResult = $stmt->execute ($params);if ($execResult) {//Gets the number of rows deleted $execResult = $stmt->rowcount (); } return $execResult; }/** * Inserts the model into the database using the attribute values of this record. * * Usage Example: * * ' php * $customer = new Customer; * $customer->name = $name; * $customer->email = $email; * $customer->insert (); * "* * * @return Boolean whether the model is inserted successfully. */Public Function Insert () {$sql = ' insert INTO '. Static::tablename (); $params = []; $keys = []; foreach ($this as $key = + $value) {Array_push ($keys, $key); Array_push ($params, $value); }//Build by? An array of the same number as the parameter equality $holders = Array_fill (0, Count ($keys), '? '); $sql. = ' ('. Implode (', ', $keys). ') VALUES ('. Implode (', ', $holders). ')'; $stmt = Static::getdb ()->prepare ($sql); $execResult = $stmt->execute ($params); Assign some self-increment values back to the model $primaryKeys = static::p Rimarykey (); foreach ($primaryKeys as $name) {//Get the primary key $lastId = Static::getdb ()->lastinsertid ( $NAME); $this $name = (int) $lastId; } return $execResult; }/** * Saves the changes to the "this" model into the database. * * Usage Example: * * ' php * $customer = Customer::findone ([' id ' = ' + $id]); * $customer->name = $name; * $customer->email = $email; * $customer->update (); * "* * * @return Integer|boolean The number of rows affected. * Note that it was possible that the number of rows affected are 0, even though the * update execution is successful. */Public Function update () {$primaryKeys = static::p Rimarykey (); $condition = []; foreach ($primaryKeys as $name) {$condition [$name] = isset ($this-i $name)? $this, $name: null; } $attributes = []; foreach ($this as $key = + $value) {if (!in_array ($kEY, $primaryKeys, True)) {$attributes [$key] = $value; }} return Static::updateall ($condition, $attributes)!== false; }/** * Deletes the model from the database. * * @return Integer|boolean the number of rows deleted. * Note that it was possible that the number of rows deleted are 0, even though the deletion execution is successful. */Public Function Delete () {$primaryKeys = static::p Rimarykey (); $condition = []; foreach ($primaryKeys as $name) {$condition [$name] = isset ($this-i $name)? $this, $name: null; } return Static::d Eleteall ($condition)!== false; }
This basic model, even if it is temporarily completed, although there may be a lot of problems and limitations, but for the time being, we will have the opportunity to improve step-by-step.
All right, let's get here today. Project content and blog content will also be put on GitHub, welcome suggestions.
code:https://github.com/craryprimitiveman/simple-framework/tree/0.7
Blog Project:https://github.com/craryprimitiveman/create-your-own-php-framework