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"). --- core/namespace/ORM/Repository.php | 44 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'core/namespace/ORM/Repository.php') diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php index 423a286..e7750cb 100644 --- a/core/namespace/ORM/Repository.php +++ b/core/namespace/ORM/Repository.php @@ -76,40 +76,46 @@ abstract class Repository { # Insert entity #=========================================================================== public function insert(EntityInterface $Entity): bool { - $attributes = $Entity->getFilteredAttributes(); - - foreach($attributes as $field => $value) { - $fields[] = $field; + foreach($Entity->getModifiedKeys() as $attribute) { + $params[] = $Entity->get($attribute); + $fields[] = $attribute; $values[] = '?'; } - $fields = implode(', ', $fields ?? []); - $values = implode(', ', $values ?? []); + if(isset($params, $fields, $values)) { + $fields = implode(', ', $fields); + $values = implode(', ', $values); - $query = 'INSERT INTO %s (%s) VALUES(%s)'; - $query = sprintf($query, static::getTableName(), $fields, $values); + $query = sprintf('INSERT INTO %s (%s) VALUES(%s)', + static::getTableName(), $fields, $values); - $Statement = $this->Database->prepare($query); - return $Statement->execute(array_values($attributes)); + $Statement = $this->Database->prepare($query); + return $Statement->execute($params); + } + + return FALSE; } #=========================================================================== # Update entity #=========================================================================== public function update(EntityInterface $Entity): bool { - $attributes = $Entity->getFilteredAttributes(); - - foreach($attributes as $field => $value) { - $params[] = "$field = ?"; + foreach($Entity->getModifiedKeys() as $attribute) { + $params[] = $Entity->get($attribute); + $fields[] = "$attribute = ?"; } - $params = implode(', ', $params ?? []); + if(isset($params, $fields)) { + $fields = implode(', ', $fields); - $query = 'UPDATE %s SET %s WHERE id = '.intval($Entity->getID()); - $query = sprintf($query, static::getTableName(), $params); + $query = sprintf('UPDATE %s SET %s WHERE id = %d', + static::getTableName(), $fields, $Entity->getID()); - $Statement = $this->Database->prepare($query); - return $Statement->execute(array_values($attributes)); + $Statement = $this->Database->prepare($query); + return $Statement->execute($params); + } + + return FALSE; } #=========================================================================== -- cgit v1.2.3