# HG changeset patch # User Markus Bröker<broeker.markus@googlemail.com> # Date 1450487279 -3600 # Node ID 5751ca5a55c02762806ab9aa667c904efd22c17a # Parent 42432a207bd025dfd27478c14e7038a7f05d1e99 Richtiges Persistence Framework mit echten Gettern und Settern + Creator diff --git a/classes/bfw/core/Entity.php b/classes/bfw/core/Entity.php --- a/classes/bfw/core/Entity.php +++ b/classes/bfw/core/Entity.php @@ -70,11 +70,13 @@ * * @param $methodName * @param null $params - * @return $this + * @return mixed */ public function __call($methodName, $params = null) { $prefix = substr($methodName, 0, 3); - $key = strtolower(substr($methodName, 3)); + + $key = lcfirst(substr($methodName, 3)); + $key = strtolower(preg_replace('/([A-Z])/', "_$1", $key)); if ($prefix == 'set') { $value = $params[0]; diff --git a/classes/bfw/core/ReadonlyEntity.php b/classes/bfw/core/ReadonlyEntity.php --- a/classes/bfw/core/ReadonlyEntity.php +++ b/classes/bfw/core/ReadonlyEntity.php @@ -23,7 +23,9 @@ */ public function __call($methodName, $params = null) { $prefix = substr($methodName, 0, 3); - $key = strtolower(substr($methodName, 3)); + + $key = lcfirst(substr($methodName, 3)); + $key = strtolower(preg_replace('/([A-Z])/', "_$1", $key)); if ($prefix == 'get') { if (array_key_exists($key, $this->data)) { diff --git a/classes/bfw/database/Database.php b/classes/bfw/database/Database.php --- a/classes/bfw/database/Database.php +++ b/classes/bfw/database/Database.php @@ -316,7 +316,7 @@ * @return array|null */ public function query($sql) { - $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $sql)); + $this->logger->info(sprintf('%s(%s) ', __METHOD__, $sql)); $stmt = $this->pdo->query($sql); diff --git a/classes/bfw/entities/TGroup.php b/classes/bfw/entities/TGroup.php --- a/classes/bfw/entities/TGroup.php +++ b/classes/bfw/entities/TGroup.php @@ -7,28 +7,11 @@ namespace bfw\entities; -use bfw\core\Entity; - -/** - * Class TGroup - * - * @method string getName() - * @mthod setName($param) - */ -class TGroup extends Entity { +class TGroup extends \bfw\entities\core\TGroup { const SYSTEM = 1; const ADMIN = 2; - public function __construct() { - parent::__construct('t_group'); - - $this->data = array( - 'id' => NULL, - 'name' => '', - ); - } - /** * @param $name * @return array|null diff --git a/classes/bfw/entities/TUser.php b/classes/bfw/entities/TUser.php --- a/classes/bfw/entities/TUser.php +++ b/classes/bfw/entities/TUser.php @@ -7,39 +7,13 @@ namespace bfw\entities; -use bfw\core\Entity; - /** - * Class TUser - * - * @method string getUsername() - * @method int getGroup_Id() - * @method string getPassword() - * @method string getFirstname() - * @method string getLastname() - * @method setUsername() - * @method setGroup_Id() - * @method setPassword() - * @method setFirstname() - * @method setLastname() + * */ -class TUser extends Entity { +class TUser extends \bfw\entities\core\TUser { const SYSTEM = 1; - public function __construct() { - parent::__construct('t_user'); - - $this->data = array( - 'id' => NULL, - 'group_id' => TGroup::SYSTEM, - 'username' => '', - 'firstname' => '', - 'lastname' => '', - 'password' => '', - ); - } - /** * <b>Liefert eine User-Instanz für den per Parameter übergebenen Nutzernamen.</b> * diff --git a/classes/bfw/entities/core/RUserGroup.php b/classes/bfw/entities/core/RUserGroup.php new file mode 100644 --- /dev/null +++ b/classes/bfw/entities/core/RUserGroup.php @@ -0,0 +1,24 @@ +<?php + +/** + * Entities for The BFW Framework + * + * @copyright Copyright(C) 2016 4customers + * @author Markus Bröker<mbroeker@4customers.de> + */ + +namespace bfw\entities\core; + +use bfw\core\Entity; + +/** + * @method int getUserId() + * @method setUserId($int) + * @method int getGroupId() + * @method setGroupId($int) +*/ +class RUserGroup extends Entity { + public function __construct() { + parent::__construct('r_user_group'); + } +} diff --git a/classes/bfw/entities/core/TGroup.php b/classes/bfw/entities/core/TGroup.php new file mode 100644 --- /dev/null +++ b/classes/bfw/entities/core/TGroup.php @@ -0,0 +1,22 @@ +<?php + +/** + * Entities for The BFW Framework + * + * @copyright Copyright(C) 2016 4customers + * @author Markus Bröker<mbroeker@4customers.de> + */ + +namespace bfw\entities\core; + +use bfw\core\Entity; + +/** + * @method string getName() + * @method setName($string) +*/ +class TGroup extends Entity { + public function __construct() { + parent::__construct('t_group'); + } +} diff --git a/classes/bfw/entities/core/TUser.php b/classes/bfw/entities/core/TUser.php new file mode 100644 --- /dev/null +++ b/classes/bfw/entities/core/TUser.php @@ -0,0 +1,30 @@ +<?php + +/** + * Entities for The BFW Framework + * + * @copyright Copyright(C) 2016 4customers + * @author Markus Bröker<mbroeker@4customers.de> + */ + +namespace bfw\entities\core; + +use bfw\core\Entity; + +/** + * @method int getGroupId() + * @method setGroupId($int) + * @method string getUsername() + * @method setUsername($string) + * @method string getPassword() + * @method setPassword($string) + * @method string getFirstname() + * @method setFirstname($string) + * @method string getLastname() + * @method setLastname($string) +*/ +class TUser extends Entity { + public function __construct() { + parent::__construct('t_user'); + } +} diff --git a/classes/bfw/mvc/controller/BenutzerverwaltungController.php b/classes/bfw/mvc/controller/BenutzerverwaltungController.php --- a/classes/bfw/mvc/controller/BenutzerverwaltungController.php +++ b/classes/bfw/mvc/controller/BenutzerverwaltungController.php @@ -88,7 +88,7 @@ if ($model->getPassword() == md5($password)) { $request->setKey('user_id', $model->getId()); - $request->setKey('group_id', $model->getGroup_Id()); + $request->setKey('group_id', $model->getGroupId()); $request->setKey('isLoggedIn', true); diff --git a/classes/bfw/mvc/view/benutzerverwaltung/index.tpl b/classes/bfw/mvc/view/benutzerverwaltung/index.tpl --- a/classes/bfw/mvc/view/benutzerverwaltung/index.tpl +++ b/classes/bfw/mvc/view/benutzerverwaltung/index.tpl @@ -25,7 +25,7 @@ <label for="groupBox{$user->getId()}">Gruppenzugehörigkeit</label> <select id="groupBox{$user->getId()}" name="groupbox"> {foreach $groups as $group} - {if $group->getId() == $user->getGroup_id()} + {if $group->getId() == $user->getGroupId()} <option value="{$group->getId()}" selected="'selected'">{$group->getName()}</option> {else} <option value="{$group->getId()}">{$group->getName()}</option> diff --git a/patches/Patch.php b/patches/Patch.php new file mode 100644 --- /dev/null +++ b/patches/Patch.php @@ -0,0 +1,27 @@ +<?php + +/** + * Patch-Management for The BFW Framework + * + * @copyright Copyright(C) 2015 4customers + * @author Markus Bröker<mbroeker@4customers.de> + */ +define("VERSION", "0.1"); +require_once '../config/config.php'; + +use bfw\database\Database; + +abstract class Patch { + + protected $db; + + public function __construct() { + $this->db = Database::getInstance(); + + printf("PatchManagement Version %s\n\n", VERSION); + } + + abstract public function setUp(); + + abstract public function tearDown(); +} diff --git a/patches/PersistenceCreator.php b/patches/PersistenceCreator.php new file mode 100644 --- /dev/null +++ b/patches/PersistenceCreator.php @@ -0,0 +1,85 @@ +<?php + +/** + * PersistenceCreator for the BFW Framework + * + * @copyright Copyright(C) 2016 4customers + * @author Markus Bröker<mbroeker@4customers.de> + */ +define("VERSION", "0.1"); +require_once '../config/config.php'; + +$header = '<?php + +/** + * Entities for The BFW Framework + * + * @copyright Copyright(C) 2016 4customers + * @author Markus Bröker<mbroeker@4customers.de> + */ + +namespace bfw\entities\core; + +use bfw\core\Entity; + +/** +'; + +$sql = "SHOW TABLES"; +$db = bfw\database\Database::getInstance(); + +$rows = $db->query($sql); + +foreach ($rows as $row) { + foreach ($row as $name) { + $fields = $db->query(sprintf("DESCRIBE %s", $name)); + $className = $name; + $className = preg_replace('/^t_([a-z])/e', 'strtoupper("T$1")', $className); + $className = preg_replace('/^r_([a-z])/e', 'strtoupper("R$1")', $className); + $className = preg_replace('/_([a-z])/e', 'strtoupper("$1")', $className); + $filename = sprintf("%s/classes/bfw/entities/core/%s.php", BFW_PATH, $className); + + $handle = fopen($filename, "w+"); + $content = $header; + foreach ($fields as $field) { + if ($field['Field'] != 'id') { + $methodName = ucfirst($field['Field']); + $methodName = preg_replace('/_([a-z])/e', 'strtoupper("$1")', $methodName); + $type = substr($field['Type'], 0, strpos($field['Type'], '(')); + + switch ($type) { + case 'int': + case 'bigint': + case 'smallint': + $type = "int"; + break; + case 'tinyint': + $type = "bool"; + break; + case 'real': + case 'float': + $type = "float"; + break; + case 'varchar': + case 'text': + case 'char': + $type = "string"; + break; + } + + $content .= sprintf(" * @method %s get%s()\n", $type, $methodName); + $content .= sprintf(" * @method set%s(\$%s)\n", $methodName, $type); + } + } + $content .= "*/\n"; + + $content .= sprintf("class %s extends Entity {\n", $className); + $content .= sprintf(" public function __construct() {\n"); + $content .= sprintf(" parent::__construct('%s');\n", $name); + $content .= sprintf(" }\n"); + $content .= sprintf("}\n"); + + fwrite($handle, $content); + fclose($handle); + } +} \ No newline at end of file diff --git a/patches/Test.php b/patches/Test.php new file mode 100644 --- /dev/null +++ b/patches/Test.php @@ -0,0 +1,12 @@ +<?php + +require_once '../config/config.php'; + +use bfw\entities\TUser; + +$tuser = new TUser(); +$users = $tuser->findAll(); + +foreach ($users as $user) { + printf("Benutzer: %s %s", $user->getFirstname(), $user->getUsername()); +} \ No newline at end of file