Skip to content

Commit

Permalink
Implement change set handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Mar 31, 2015
1 parent 353b922 commit b15adad
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.markdown
Expand Up @@ -151,14 +151,14 @@ class UserPersister extends ObjectPersister

public function persistObject($object)
{
$this->users[$object->id] = $this->objectToArray($object);
$this->users[$object->id] = $this->prepareChangeSet($object);

return $this->users[$object->id];
}

public function updateObject($object)
{
$this->users[$object->id] = $this->objectToArray($object);
$this->users[$object->id] = $this->prepareChangeSet($object);

return $this->users[$object->id];
}
Expand All @@ -168,7 +168,7 @@ class UserPersister extends ObjectPersister
unset($this->users[$object->id]);
}

public function objectToArray($object)
public function prepareChangeSet($object, array $changeSet = array())
{
return array(
'id' => $object->id,
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/SkeletonMapper/Persister/DBALObjectPersister.php
Expand Up @@ -54,16 +54,16 @@ abstract public function getTableName();

public function persistObject($object)
{
$data = $this->objectToArray($object);
$data = $this->prepareChangeSet($object);

$this->connection->insert($this->getTableName(), $data);

return $data;
}

public function updateObject($object)
public function updateObject($object, array $changeSet)
{
$data = $this->objectToArray($object);
$data = $this->prepareChangeSet($object, $changeSet);

$this->connection->update(
$this->getTableName(),
Expand Down
13 changes: 9 additions & 4 deletions lib/Doctrine/SkeletonMapper/Persister/MongoDBObjectPersister.php
Expand Up @@ -49,18 +49,23 @@ public function __construct(

public function persistObject($object)
{
$data = $this->objectToArray($object);
$data = $this->prepareChangeSet($object);

$this->mongoCollection->insert($data);

return $data;
}

public function updateObject($object)
public function updateObject($object, array $changeSet)
{
$data = $this->objectToArray($object);
$data = $this->prepareChangeSet($object, $changeSet);

$this->mongoCollection->save($data);
unset($data['_id']);

$this->mongoCollection->update(
$this->getObjectIdentifier($object),
array('$set' => $data)
);

return $data;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/SkeletonMapper/Persister/ObjectPersister.php
Expand Up @@ -28,20 +28,21 @@
abstract class ObjectPersister implements ObjectPersisterInterface
{
/**
* Converts an object to an array.
* Prepares an object changeset for persistence.
*
* @param \Doctrine\SkeletonMapper\Persister\PersistableInterface $object
* @param array $changeSet
*
* @return array
*/
public function objectToArray($object)
public function prepareChangeSet($object, array $changeSet = array())
{
if (!$object instanceof PersistableInterface) {
throw new \InvalidArgumentException(
sprintf('%s must implement PersistableInterface', get_class($object))
);
}

return $object->toArray();
return $object->prepareChangeSet($changeSet);
}
}
Expand Up @@ -35,13 +35,14 @@ interface ObjectPersisterInterface
public function getClassName();

/**
* Converts an object to an array.
* Prepares an object changeset for persistence.
*
* @param object $object
* @param array $changeSet
*
* @return array
*/
public function objectToArray($object);
public function prepareChangeSet($object, array $changeSet = array());

/**
* Performs operation to write object to the database.
Expand All @@ -56,10 +57,11 @@ public function persistObject($object);
* Performs operation to update object in the database.
*
* @param object $object
* @param array $changeSet
*
* @return array $objectData
*/
public function updateObject($object);
public function updateObject($object, array $changeSet);

/**
* Performs operation to remove object in the database.
Expand Down
Expand Up @@ -28,7 +28,9 @@
interface PersistableInterface
{
/**
* @param array $changeSet
*
* @return array
*/
public function toArray();
public function prepareChangeSet(array $changeSet);
}
4 changes: 3 additions & 1 deletion lib/Doctrine/SkeletonMapper/UnitOfWork.php
Expand Up @@ -460,8 +460,10 @@ private function executePersists()
private function executeUpdates()
{
foreach ($this->objectsToUpdate as $object) {
$changeSet = $this->getObjectChangeSet($object);

$this->getObjectPersister($object)
->updateObject($object);
->updateObject($object, $changeSet);

$className = get_class($object);
$class = $this->objectManager->getClassMetadata($className);
Expand Down
Expand Up @@ -514,6 +514,25 @@ public function testPropertyChangedListeners()
$user3 = $this->objectManager->find($this->testClassName, 3);

$this->assertEquals('changed', $user3->getUsername());

$user3->setUsername('testing');

$this->assertEquals(
array('username' => array('changed', 'testing')),
$this->unitOfWork->getObjectChangeSet($user3)
);

$this->objectManager->clear();
$this->objectManager->flush();

$this->assertEquals(
array(),
$this->unitOfWork->getObjectChangeSet($user)
);

$user3 = $this->objectManager->find($this->testClassName, 3);

$this->assertEquals('changed', $user3->getUsername());
}

private function createTestObject()
Expand Down
14 changes: 13 additions & 1 deletion tests/Doctrine/SkeletonMapper/Tests/Model/User.php
Expand Up @@ -119,10 +119,22 @@ public function hydrate(array $data)
/**
* @see PersistableInterface
*
* @param array $changeSet
*
* @return array
*/
public function toArray()
public function prepareChangeSet(array $changeSet)
{
if ($changeSet) {
$changeSet = array_map(function($change) {
return $change[1];
}, $changeSet);

$changeSet['_id'] = (int) $this->id;

return $changeSet;
}

return array(
'_id' => (int) $this->id,
'username' => $this->username,
Expand Down
Expand Up @@ -21,16 +21,25 @@ public function getClassName()

public function persistObject($object)
{
$this->users[$object->getId()] = $this->objectToArray($object);
$data = $this->prepareChangeSet($object);

return $this->users[$object->getId()];
$this->users[$object->getId()] = $data;

return $data;
}

public function updateObject($object)
public function updateObject($object, array $changeSet)
{
$this->users[$object->getId()] = $this->objectToArray($object);
$data = $this->prepareChangeSet($object, $changeSet);

foreach ($data as $key => $value) {
$user = $this->users[$object->getId()];
$user[$key] = $value;

$this->users[$object->getId()] = $user;
}

return $this->users[$object->getId()];
return $data;
}

public function removeObject($object)
Expand Down

0 comments on commit b15adad

Please sign in to comment.