Systemdatensatz: $initial_Id vs $sys
authorMarkus Bröker <broeker.markus@googlemail.com>
Sat, 14 Nov 2015 02:39:17 +0100
changeset 23 975b2839f1f3
parent 22 16ebd5c503fe
child 24 ef2914b7af78
Systemdatensatz: $initial_Id vs $sys
classes/bfw/core/Entity.php
classes/bfw/core/Model.php
classes/bfw/database/DBInterface.php
classes/bfw/database/Database.php
tests/bfw/DatabaseTest.php
--- a/classes/bfw/core/Entity.php
+++ b/classes/bfw/core/Entity.php
@@ -104,13 +104,12 @@
     /**
      * <b>Die Findall Methode erzeugt typsicher den Supertyp</b>
      *
-     * @param bool|false $sys
+     * @param int $initial_id
      * @return Entity[]
      */
-    public function findAll($sys = false) {
-        $this->logger->info(sprintf('%s() ', __METHOD__));
+    public function findAll($initial_id = 1) {
+        $this->logger->info(sprintf('%s(%d) ', __METHOD__, $initial_id));
 
-        $initial_id = ($sys) ? 0 : 1;
         $rows = $this->db->findAll($this->table, $initial_id);
 
         if (count($rows) == 0) {
@@ -229,4 +228,23 @@
         return false;
     }
 
+    /**
+     * Experimental Feature
+     *
+     * @param $array
+     * @return $this
+     */
+    public function merge($array) {
+        $this->data = array_merge($this->data, $array);
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getLastError() {
+        return $this->db->getLastError();
+    }
+
 }
\ No newline at end of file
--- a/classes/bfw/core/Model.php
+++ b/classes/bfw/core/Model.php
@@ -54,13 +54,13 @@
      * @return Entity[]
      */
     public function getUsers() {
-        return $this->user->findAll(false);
+        return $this->user->findAll();
     }
 
     /**
      * @return Entity[]
      */
     public function getGroups() {
-        return $this->group->findAll(false);
+        return $this->group->findAll();
     }
 }
\ No newline at end of file
--- a/classes/bfw/database/DBInterface.php
+++ b/classes/bfw/database/DBInterface.php
@@ -17,7 +17,7 @@
     /* find methoden */
     public function find($table, $id);
 
-    public function findAll($table, $sys);
+    public function findAll($table, $initial_id);
 
     public function findByField($table, $field, $value);
 
--- a/classes/bfw/database/Database.php
+++ b/classes/bfw/database/Database.php
@@ -225,11 +225,11 @@
 
     /**
      * @param $table
-     * @param bool $sys
+     * @param int $initial_id
      * @return array|null
      */
-    public function findAll($table, $sys) {
-        $this->logger->info(sprintf('%s(%s, %d) ', __METHOD__, $table, $sys));
+    public function findAll($table, $initial_id = 0) {
+        $this->logger->info(sprintf('%s(%s, %d) ', __METHOD__, $table, $initial_id));
 
         $stmt = $this->pdo->prepare(
             sprintf('
@@ -239,11 +239,11 @@
                 %s
               WHERE
                 id > :id
+              ORDER BY ID ASC
             ', $table
             ));
 
-        $id = ($sys ? 0 : 1);
-        $stmt->bindParam(':id', $id);
+        $stmt->bindParam(':id', $initial_id);
 
         if ($stmt->execute()) {
             return $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -329,16 +329,16 @@
 
     /**
      * @param $sql
-     * @return int
+     * @return bool
      */
     public function execute($sql) {
-        return $this->pdo->exec($sql);
+        return ($this->pdo->exec($sql) !== FALSE);
     }
 
     /**
      * @param $table
      * @param $array
-     * @return int
+     * @return bool
      */
     public function persist($table, $array) {
         $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, print_r($array, true)));
@@ -366,14 +366,14 @@
             (id, %s) VALUES (NULL, %s)
         ", $table, $fieldList, $fields);
 
-        return $this->pdo->exec($sql);
+        return ($this->pdo->exec($sql) !== FALSE);
     }
 
     /**
      * @param $table
      * @param $id
      * @param $array
-     * @return int
+     * @return bool
      */
     public function store($table, $id, $array) {
         $this->logger->info(sprintf('%s(%s, %d, %s) ', __METHOD__, $table, $id, print_r($array, true)));
@@ -393,7 +393,7 @@
             WHERE id = %d
         ", $table, $listItems, $id);
 
-        return $this->pdo->exec($sql);
+        return ($this->pdo->exec($sql) !== FALSE);
     }
 
     /**
@@ -409,10 +409,14 @@
             WHERE id = %d;
         ", $table, $id);
 
-        return $this->pdo->exec($sql);
+        return ($this->pdo->exec($sql) !== FALSE);
     }
 
     public function getLastInsertedId() {
         return $this->pdo->lastInsertId();
     }
+
+    public function getLastError() {
+        return $this->pdo->errorCode();
+    }
 }
--- a/tests/bfw/DatabaseTest.php
+++ b/tests/bfw/DatabaseTest.php
@@ -44,8 +44,8 @@
     }
 
     public function testFindAll() {
-        $data1 = $this->object->findAll('t_user', true);
-        $data2 = $this->object->findAll('t_user', false);
+        $data1 = $this->object->findAll('t_user');
+        $data2 = $this->object->findAll('t_user', 5);
 
         $this->assertTrue(count($data1) != count($data2));
     }