Richtiges Persistence Framework mit echten Gettern und Settern + Creator
authorMarkus Bröker<broeker.markus@googlemail.com>
Sat, 19 Dec 2015 02:07:59 +0100
changeset 30 5751ca5a55c0
parent 29 42432a207bd0
child 31 3a5ee3ba6006
Richtiges Persistence Framework mit echten Gettern und Settern + Creator
classes/bfw/core/Entity.php
classes/bfw/core/ReadonlyEntity.php
classes/bfw/database/Database.php
classes/bfw/entities/TGroup.php
classes/bfw/entities/TUser.php
classes/bfw/entities/core/RUserGroup.php
classes/bfw/entities/core/TGroup.php
classes/bfw/entities/core/TUser.php
classes/bfw/mvc/controller/BenutzerverwaltungController.php
classes/bfw/mvc/view/benutzerverwaltung/index.tpl
patches/Patch.php
patches/PersistenceCreator.php
patches/Test.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];
--- 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)) {
--- 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);
 
--- 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
--- 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>
      *
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');
+    }
+}
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');
+    }
+}
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');
+    }
+}
--- 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);
 
--- 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>
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();
+}
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
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