diff options
author | Thomas Lange <code@nerdmind.de> | 2021-08-10 17:42:11 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-08-10 19:41:49 +0200 |
commit | e3f05b25f961e0169185acabd32566e2ae5198fe (patch) | |
tree | 8c60ddc5d558983ad3bf4b20ca643b109410082b /core/namespace/ORM/Repositories | |
parent | 49f03ac48efcce79822bed1ec40ad48bf5c6e450 (diff) | |
download | blog-e3f05b25f961e0169185acabd32566e2ae5198fe.tar.gz blog-e3f05b25f961e0169185acabd32566e2ae5198fe.tar.xz blog-e3f05b25f961e0169185acabd32566e2ae5198fe.zip |
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").
Diffstat (limited to 'core/namespace/ORM/Repositories')
-rw-r--r-- | core/namespace/ORM/Repositories/CategoryRepository.php | 23 |
1 files changed, 9 insertions, 14 deletions
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; |