Last active
August 29, 2015 14:18
-
-
Save Chen-Michael/5b4bb3afc13e097b87ea to your computer and use it in GitHub Desktop.
PHP Template Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include_once("department_operating.php"); | |
| class DepartmentModel{ | |
| private $mId = ""; | |
| private $mName = ""; | |
| public function __construct($id, $name){ | |
| $this->$mId = $id; | |
| $this->$mName = $name; | |
| } | |
| public function getId(){ | |
| return $this->$mId; | |
| } | |
| public function getName(){ | |
| return $this->$mName; | |
| } | |
| } | |
| $a = new DepartmentOperating(); | |
| try { | |
| echo $a->insert(new DepartmentModel(0, "name")); | |
| } catch (Exception $e) { | |
| echo $e->getMessage(); | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include_once("pdo_template.php"); | |
| class DepartmentOperating extends PdoTemplate{ | |
| protected function getSql($type){ | |
| $sql = ""; | |
| switch($type){ | |
| case parent::TYPE_INSERT: | |
| $sql = "INSERT INTO `department`(`Name`) VALUES (:Name)"; | |
| break; | |
| case parent::TYPE_UPDATE: | |
| $sql = "UPDATE `department` SET `Name`= :Name WHERE `Id` = :Id"; | |
| break; | |
| case parent::TYPE_DELETE: | |
| $sql = "DELETE FROM `department` WHERE `Id` = :Id"; | |
| break; | |
| case parent::TYPE_SEARCH: | |
| $sql = "SELECT * FROM `department` WHERE 1=1"; | |
| break; | |
| } | |
| return $sql; | |
| } | |
| protected function setNormalExecuteValue($obj, $type, &$prepare){ | |
| switch($type){ | |
| case parent::TYPE_INSERT: | |
| $this -> setInsertValue($obj, $prepare); | |
| break; | |
| case parent::TYPE_UPDATE: | |
| $this -> setUpdateValue($obj, $prepare); | |
| break; | |
| case parent::TYPE_DELETE: | |
| $this -> setDeleteValue($obj, $prepare); | |
| break; | |
| } | |
| } | |
| protected function prepareSearchSql($sql, $obj){ | |
| $id = $obj -> getId(); | |
| $name = $obj -> getName(); | |
| if(trim($name) != ""){ | |
| $sql .= " AND Name LIKE '%:Name%'"; | |
| } | |
| if(is_int($id) && $id > 0){ | |
| $sql .= " AND Id = ':Id'"; | |
| } | |
| } | |
| protected function setSearchExecuteValue($obj, &$prepare){ | |
| $id = $obj -> getId(); | |
| $name = $obj -> getName(); | |
| if(trim($name) != ""){ | |
| $prepare -> bindValue(":Name", $obj -> getName(), PDO::PARAM_STR); | |
| } | |
| if(is_int($id) && $id > 0){ | |
| $prepare -> bindValue(":Id", $obj -> getId(), PDO::PARAM_INT); | |
| } | |
| } | |
| protected function executeCheck($obj, $type){ | |
| $result = ""; | |
| try { | |
| if(!method_exists($obj, "getId") || !method_exists($obj, "getName")) throw new Exception(); | |
| $id = $obj -> getId(); | |
| $name = $obj -> getName(); | |
| if($type != parent::TYPE_DELETE && trim($name) == ""){ | |
| $result = "Please Enter Department Name"; | |
| } | |
| if($type != parent::TYPE_INSERT && (!is_int($id) || $id < 0)){ | |
| $result = "Error"; | |
| } | |
| }catch(Exception $e){ | |
| $result = "Error"; | |
| error_log("Class 'DepartmentOperating' Function 'executeCheck' Happen Exception", 0); | |
| } | |
| return $result; | |
| } | |
| private function setInsertValue($obj, &$prepare){ | |
| $prepare -> bindValue(":Name", $obj -> getName(), PDO::PARAM_STR); | |
| } | |
| private function setUpdateValue($obj, &$prepare){ | |
| $prepare -> bindValue(":Id", $obj -> getId(), PDO::PARAM_INT); | |
| $prepare -> bindValue(":Name", $obj -> getName(), PDO::PARAM_STR); | |
| } | |
| private function setDeleteValue($obj, &$prepare){ | |
| $prepare -> bindValue(":Id", $obj -> getId(), PDO::PARAM_INT); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include_once("db_selector.php"); | |
| abstract class PdoTemplate{ | |
| /** @var int Insert Status Code */ | |
| const TYPE_INSERT = 1; | |
| /** @var int Update Status Code */ | |
| const TYPE_UPDATE = 2; | |
| /** @var int Delete Status Code */ | |
| const TYPE_DELETE = 3; | |
| /** @var int Search Status Code */ | |
| const TYPE_SEARCH = 4; | |
| /** | |
| * insert | |
| * Execute Insert Action | |
| * | |
| * @access Public | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @return int Last Insert Id | |
| * @version 1.0 | |
| */ | |
| public final function insert($obj){ | |
| $sql = $this -> getSql($this::TYPE_INSERT); | |
| $checkResult = $this -> executeCheck($obj, $this::TYPE_INSERT); | |
| if($checkResult != "") throw new Exception($checkResult); | |
| return $this -> execute($sql, $obj, $this::TYPE_INSERT); | |
| } | |
| /** | |
| * update | |
| * Execute Update Action | |
| * | |
| * @access Public | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @return int Rows Affected Number | |
| * @version 1.0 | |
| */ | |
| public final function update($obj){ | |
| $sql = $this -> getSql($this::TYPE_UPDATE); | |
| $checkResult = $this -> executeCheck($obj, $this::TYPE_UPDATE); | |
| if($checkResult != "") throw new Exception($checkResult); | |
| return $this -> execute($sql, $obj, $this::TYPE_UPDATE); | |
| } | |
| /** | |
| * delete | |
| * Execute Delete Action | |
| * | |
| * @access Public | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @return int Rows Affected Number | |
| * @version 1.0 | |
| */ | |
| public final function delete($obj){ | |
| $sql = $this -> getSql($this::TYPE_DELETE); | |
| $checkResult = $this -> executeCheck($obj, $this::TYPE_DELETE); | |
| if($checkResult != "") throw new Exception($checkResult); | |
| return $this -> execute($sql, $obj, $this::TYPE_DELETE); | |
| } | |
| /** | |
| * search | |
| * Execute Search Action | |
| * | |
| * @access Public | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @return object Result Collection | |
| * @version 1.0 | |
| */ | |
| public final function search($obj){ | |
| $sql = $this -> getSql($this::TYPE_SEARCH); | |
| return $this -> executeSearch($sql, $obj); | |
| } | |
| /** | |
| * getConnection | |
| * Get PDO Connection | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @return object|Null PDO Entity | |
| * @version 1.0 | |
| */ | |
| protected function getConnection(){ | |
| return DbSelector::getMySqlPdoConnection(); | |
| } | |
| /** | |
| * execute | |
| * Execute Update Database | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param string $sql SQL String | |
| * @param object $obj Data Model | |
| * @param int $type Action Status Code | |
| * @return int Update Database Result | |
| * @version 1.0 | |
| */ | |
| protected function execute($sql, $obj, $type){ | |
| $connection = null; | |
| $result = 0; | |
| try{ | |
| if(!($connection = $this -> getConnection())) throw new Exception(); | |
| $connection -> beginTransaction(); | |
| $prepare = $connection -> prepare($sql); | |
| $this -> setNormalExecuteValue($obj, $type, $prepare); | |
| $prepare -> execute(); | |
| $connection -> commit(); | |
| $result = ($type == $this::TYPE_INSERT)? $connection -> lastInsertId(): $prepare -> rowCount(); | |
| }catch (Exception $e) { | |
| error_log("Variable 'connection' Is Null Or Other Exception", 0); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * executeSearch | |
| * Execute Search Database | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param string $sql SQL String | |
| * @param object $obj Data Model | |
| * @return object Result Collection | |
| * @version 1.0 | |
| */ | |
| protected function executeSearch($sql, $obj){ | |
| $connection = null; | |
| $result = array(); | |
| try{ | |
| if(!($connection = $this -> getConnection())) throw new Exception(); | |
| $sql = $this ->prepareSearchSql($sql, $obj); | |
| $prepare = $connection -> prepare($sql); | |
| $this -> setSearchExecuteValue($obj, $prepare); | |
| $prepare -> execute(); | |
| while ($row = $prepare->fetch(PDO::FETCH_ASSOC)) { | |
| $result[] = $this -> resultOfConversion($row); | |
| } | |
| }catch (Exception $e) { | |
| error_log("Variable 'connection' Is Null", 0); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * resultOfConversion | |
| * Conversion Result Type | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param object $row Row Data | |
| * @return object Conversion Result | |
| * @version 1.0 | |
| */ | |
| protected function resultOfConversion($row){ | |
| return $row; | |
| } | |
| /** | |
| * executeCheck | |
| * Execute Data Check | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @param int $type Action Status Code | |
| * @return string Check Result | |
| * @version 1.0 | |
| */ | |
| protected abstract function executeCheck($obj, $type); | |
| /** | |
| * getSql | |
| * Take Out The SQL String | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param int $type Action Status Code | |
| * @return string Sql String | |
| * @version 1.0 | |
| */ | |
| protected abstract function getSql($type); | |
| /** | |
| * setNormalExecuteValue | |
| * Set Normal Action The Required Data | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @param int $type Action Status Code | |
| * @param object $prepare Pdo Prepare Object | |
| * @version 1.0 | |
| */ | |
| protected abstract function setNormalExecuteValue($obj, $type, &$prepare); | |
| /** | |
| * prepareSearchSql | |
| * Deal With Search SQL(Plus Filter Conditions) | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param string $sql SQL String | |
| * @param object $obj Data Model | |
| * @return string Sql String | |
| * @version 1.0 | |
| */ | |
| protected abstract function prepareSearchSql($sql, $obj); | |
| /** | |
| * setNormalExecuteValue | |
| * Set Search Action The Required Data | |
| * | |
| * @access protected | |
| * @author Michael-Chen | |
| * @param object $obj Data Model | |
| * @param object $prepare Pdo Prepare Object | |
| * @version 1.0 | |
| */ | |
| protected abstract function setSearchExecuteValue($obj, &$prepare); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment