Skip to content

Commit

Permalink
#72 - the ActiveRecord::saveAttribute() implementations now also save…
Browse files Browse the repository at this point in the history
… house keeping attributes like updated_ts and updated_by
  • Loading branch information
alphadevx committed Jan 28, 2017
1 parent 74927d6 commit 70e94fb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- memcached
- redis

before_install: echo "extension = memcached.so" >> ~/.phpenv/versions/7.0.14/etc/php.ini
before_install: echo "extension = memcached.so" >> ~/.phpenv/versions/7.0.15/etc/php.ini

before_script:
- wget http://getcomposer.org/composer.phar
Expand Down
1 change: 1 addition & 0 deletions Alpha/Model/ActiveRecordProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function save();
*
* @throws \Alpha\Exception\IllegalArguementException
* @throws \Alpha\Exception\FailedSaveException
* @throws \Alpha\Exception\LockingException
*/
public function saveAttribute($attribute, $value);

Expand Down
27 changes: 24 additions & 3 deletions Alpha/Model/ActiveRecordProviderMySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,27 @@ public function saveAttribute($attribute, $value)
{
self::$logger->debug('>>saveAttribute(attribute=['.$attribute.'], value=['.$value.'])');

$config = ConfigProvider::getInstance();
$sessionProvider = $config->get('session.provider.name');
$session = SessionProviderFactory::getInstance($sessionProvider);

// get the class attributes
$reflection = new ReflectionClass(get_class($this->BO));
$properties = $reflection->getProperties();

if ($this->BO->getVersion() != $this->BO->getVersionNumber()->getValue()) {
throw new LockingException('Could not save the object as it has been updated by another user. Please try saving again.');
}

// set the "updated by" fields, we can only set the user id if someone is logged in
if ($session->get('currentUser') != null) {
$this->BO->set('updated_by', $session->get('currentUser')->getOID());
}

$this->BO->set('updated_ts', new Timestamp(date('Y-m-d H:i:s')));

// assume that it is a persistent object that needs to be updated
$sqlQuery = 'UPDATE '.$this->BO->getTableName().' SET '.$attribute.'=?, version_num = ? WHERE OID=?;';
$sqlQuery = 'UPDATE '.$this->BO->getTableName().' SET '.$attribute.' = ?, version_num = ? , updated_by = ?, updated_ts = ? WHERE OID = ?;';

$this->BO->setLastQuery($sqlQuery);
$stmt = self::getConnection()->stmt_init();
Expand All @@ -1047,8 +1066,10 @@ public function saveAttribute($attribute, $value)
$bindingsType = 's';
}
$OID = $this->BO->getOID();
$stmt->bind_param($bindingsType.'ii', $value, $newVersionNumber, $OID);
self::$logger->debug('Binding params ['.$bindingsType.'i, '.$value.', '.$OID.']');
$updatedBy = $this->BO->get('updated_by');
$updatedTS = $this->BO->get('updated_ts');
$stmt->bind_param($bindingsType.'iisi', $value, $newVersionNumber, $updatedBy, $updatedTS, $OID);
self::$logger->debug('Binding params ['.$bindingsType.'iisi, '.$value.', '.$newVersionNumber.', '.$updatedBy.', '.$updatedTS.', '.$OID.']');
$stmt->execute();
} else {
throw new FailedSaveException('Failed to save attribute, error is ['.$stmt->error.'], query ['.$this->BO->getLastQuery().']');
Expand Down
26 changes: 25 additions & 1 deletion Alpha/Model/ActiveRecordProviderSQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,27 @@ public function saveAttribute($attribute, $value)
{
self::$logger->debug('>>saveAttribute(attribute=['.$attribute.'], value=['.$value.'])');

$config = ConfigProvider::getInstance();
$sessionProvider = $config->get('session.provider.name');
$session = SessionProviderFactory::getInstance($sessionProvider);

// get the class attributes
$reflection = new ReflectionClass(get_class($this->BO));
$properties = $reflection->getProperties();

if ($this->BO->getVersion() != $this->BO->getVersionNumber()->getValue()) {
throw new LockingException('Could not save the object as it has been updated by another user. Please try saving again.');
}

// set the "updated by" fields, we can only set the user id if someone is logged in
if ($session->get('currentUser') != null) {
$this->BO->set('updated_by', $session->get('currentUser')->getOID());
}

$this->BO->set('updated_ts', new Timestamp(date('Y-m-d H:i:s')));

// assume that it is a persistent object that needs to be updated
$sqlQuery = 'UPDATE '.$this->BO->getTableName().' SET '.$attribute.'=:attribute, version_num =:version WHERE OID=:OID;';
$sqlQuery = 'UPDATE '.$this->BO->getTableName().' SET '.$attribute.'=:attribute, version_num=:version, updated_by=:updated_by, updated_ts=:updated_ts WHERE OID=:OID;';

$this->BO->setLastQuery($sqlQuery);
$stmt = self::getConnection()->prepare($sqlQuery);
Expand All @@ -1077,7 +1096,12 @@ public function saveAttribute($attribute, $value)
$stmt->bindValue(':attribute', $value, SQLITE3_TEXT);
}

$updatedBy = $this->BO->get('updated_by');
$updatedTS = $this->BO->get('updated_ts');

$stmt->bindValue(':version', $newVersionNumber, SQLITE3_INTEGER);
$stmt->bindValue(':updated_by', $updatedBy, SQLITE3_INTEGER);
$stmt->bindValue(':updated_ts', $updatedTS, SQLITE3_TEXT);
$stmt->bindValue(':OID', $this->BO->getOID(), SQLITE3_INTEGER);

$stmt->execute();
Expand Down
12 changes: 12 additions & 0 deletions test/Alpha/Test/Model/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,18 @@ public function testSaveAttribute($provider)
} catch (RecordNotFoundException $e) {
$this->fail('Failed to load the BO that was updated with the saveAttribute method');
}

$oldTimestamp = $person->get('updated_ts');

sleep(1);

$person->saveAttribute('displayName', 'unitTestUserNew');

$this->assertNotEquals($oldTimestamp, $person->get('updated_ts'), 'Testing that updated_ts changed');

$person->reload();

$this->assertNotEquals($oldTimestamp, $person->get('updated_ts'), 'Testing that updated_ts changed');
}

/**
Expand Down

0 comments on commit 70e94fb

Please sign in to comment.