From e3f05b25f961e0169185acabd32566e2ae5198fe Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Tue, 10 Aug 2021 17:42:11 +0200 Subject: Add a better mechanism to detect Entity changes Implement and use a better mechanism to detect changes of attributes of the Entity objects by using a private variable which keeps track of the changed Entity attributes ("properties") via the "set" method. The "insert" and "update" method of the Repository now calls the method "getModifiedKeys" of the Entity class to get a list of properties which have been changed and builds the database query accordingly. This makes the use of "FALSE" as default value for the Entity attributes obsolete, so they have been set to the initial PHP default ("NULL"). --- .../ORM/Repositories/CategoryRepository.php | 23 +++++++++------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'core/namespace/ORM/Repositories') diff --git a/core/namespace/ORM/Repositories/CategoryRepository.php b/core/namespace/ORM/Repositories/CategoryRepository.php index b62485a..8692079 100644 --- a/core/namespace/ORM/Repositories/CategoryRepository.php +++ b/core/namespace/ORM/Repositories/CategoryRepository.php @@ -96,36 +96,31 @@ class CategoryRepository extends Repository { # Update category (and check for parent/child circular loops) #=============================================================================== public function update(EntityInterface $Entity): bool { - # Entity parent might have changed *in memory*, so we re-fetch the original - # parent of the entity from the database and save it in a variable. - # TODO: Repository/Entity class should have a mechanism to detect changes! + # Skip circular loop check if parent is unchanged + if(!in_array('parent', $Entity->getModifiedKeys())) { + return parent::update($Entity); + } + $query = 'SELECT parent FROM %s WHERE id = ?'; $query = sprintf($query, static::getTableName()); $Statement = $this->Database->prepare($query); $Statement->execute([$Entity->getID()]); - $parent = $Statement->fetchColumn(); - - # If parent is unchanged, circular loop check is not needed. - if($parent === $Entity->get('parent')) { - return parent::update($Entity); - } - $_parent = $Entity->get('parent'); # Fetch the parent of the *new* parent category and let the while loop run through # the tree until either a parent of "NULL" was found or if the new parent category # is a *child* of the *current* category which would cause a circular loop. while($Statement->execute([$_parent]) && $_parent = $Statement->fetchColumn()) { - if($_parent == $Entity->get('id')) { + if($_parent == $Entity->getID()) { # Set parent of the *new* parent category to the *original* parent category # of the *current* category (one level up) to prevent a circular loop. - $query = 'UPDATE %s SET parent = ? WHERE id = ?'; - $query = sprintf($query, static::getTableName()); + $query = 'UPDATE %s SET parent = (SELECT parent FROM %s WHERE id = ?) WHERE id = ?'; + $query = sprintf($query, static::getTableName(), static::getTableName()); $UpdateStatement = $this->Database->prepare($query); - $UpdateStatement->execute([$parent, $Entity->get('parent')]); + $UpdateStatement->execute([$_parent, $Entity->get('parent')]); break; } else if($_parent === NULL) { break; -- cgit v1.2.3