
Originally Posted by
oddz
Not sure if this is going to be much use to you, but this is what my solution amounts to:
Might help might not…
Decided on a collection object to track changes...
PHP Code:
class Collection
{
private $added;
private $removed;
function attach($object)
{
if (!$this->added)
$this->added = new SplObjectStorage();
$this->added->attach($object);
}
function detach($object)
{
if (!$this->removed)
$this->removed = new SplObjectStorage();
$this->removed->attach($object);
}
function commit($add, $remove)
{
if ($this->added)
{
foreach($this->added as $object)
$add($object);
$this->added = null;
}
if ($this->removed)
{
foreach($this->removed as $object)
$remove($object);
$this->removed = null;
}
}
}
And defining a relationship is just a matter of hooking into a few mapper events...
$class String name of the class in the collection
$getter Closure to retrieve a collection give an object instance.
$insert Closure to insert a row into the association table.
$delete Closure to delete a row from the association table.
$deleteAll Closure to delete all associated rows.
PHP Code:
protected function hasMany($class, $getter, $insert, $delete, $deleteAll)
{
$fn = function(Transaction $transaction, Connection $connection, $owner) use ($getter, $insert, $delete)
{
$transaction->onBeforeCommit(
function() use ($owner, $getter, $insert, $delete, $transaction, $connection)
{
$collection = $getter($owner);
$collection->commit(
function($add) use ($owner, $insert, $transaction, $connection)
{
$insert($transaction, $connection, $owner, $add);
},
function($remove) use ($owner, $delete, $transaction, $connection)
{
$delete($transaction, $connection, $owner, $remove);
}
);
}
);
};
MapperEvent::append($this->afterInsert, $fn);
MapperEvent::append($this->afterUpdate, $fn);
MapperEvent::append($this->beforeDelete, $deleteAll);
}
Bookmarks