[icinga-checkins] icinga.org: icingaweb2/feature/announce-banner-11198: AnnounceIniRepository: introduce dedicated primary key column

git at icinga.org git at icinga.org
Thu Oct 27 14:37:54 CEST 2016


Module: icingaweb2
Branch: feature/announce-banner-11198
Commit: 43602b02c55fc03a63b4a6ffd78402bee7f81c63
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=43602b02c55fc03a63b4a6ffd78402bee7f81c63

Author: Alexander A. Klimov <alexander.klimov at icinga.com>
Date:   Thu Oct 27 14:11:15 2016 +0200

AnnounceIniRepository: introduce dedicated primary key column

refs #11198

---

 .../Icinga/Repository/AnnounceIniRepository.php    |   35 ++++++++++++--------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/library/Icinga/Repository/AnnounceIniRepository.php b/library/Icinga/Repository/AnnounceIniRepository.php
index af16508..d3789c9 100644
--- a/library/Icinga/Repository/AnnounceIniRepository.php
+++ b/library/Icinga/Repository/AnnounceIniRepository.php
@@ -23,7 +23,7 @@ class AnnounceIniRepository extends IniRepository
         }
         $config = $ds->getConfigObject();
         if ($config->getKeyColumn() === null) {
-            $config->setKeyColumn('hash');
+            $config->setKeyColumn('id');
         }
         parent::__construct($ds);
     }
@@ -31,7 +31,7 @@ class AnnounceIniRepository extends IniRepository
     /**
      * {@inheritDoc}
      */
-    protected $queryColumns = array('announce' => array('hash', 'author', 'message', 'start', 'end'));
+    protected $queryColumns = array('announce' => array('id', 'author', 'message', 'hash', 'start', 'end'));
 
     /**
      * {@inheritDoc}
@@ -70,6 +70,9 @@ class AnnounceIniRepository extends IniRepository
      */
     public function insert($target, array $data)
     {
+        if (! isset($data['id'])) {
+            $data['id'] = uniqid('', true);
+        }
         if (! isset($data['hash'])) {
             $announce = new Announce($data);
             $data['hash'] = $announce->getHash();
@@ -82,18 +85,22 @@ class AnnounceIniRepository extends IniRepository
      */
     public function update($target, array $data, Filter $filter = null)
     {
-        $query = $this->select(array('hash', 'author', 'message', 'start', 'end'));
-        if ($filter !== null) {
-            $query->applyFilter($filter);
-        }
-        foreach ($query->fetchAll() as $row) {
-            $row = (array) $row;
-            $hash = $row['hash'];
-            unset($row['hash']);
-            $announce = new Announce(array_merge($row, $data));
-            $data['hash'] = $announce->getHash();
-            parent::update($target, $data, Filter::expression('hash', '=', $hash));
-            unset($data['hash']);
+        if (isset($data['message'])) {
+            $query = $this->select(array('id', 'author', 'message', 'start', 'end'));
+            if ($filter !== null) {
+                $query->applyFilter($filter);
+            }
+            foreach ($query->fetchAll() as $row) {
+                $row = (array) $row;
+                $id = $row['id'];
+                unset($row['id']);
+                $announce = new Announce(array_merge($row, $data));
+                $data['hash'] = $announce->getHash();
+                parent::update($target, $data, Filter::where('id', $id));
+                unset($data['hash']);
+            }
+        } else {
+            parent::update($target, $data, $filter);
         }
     }
 }



More information about the icinga-checkins mailing list