Skip to content

Commit

Permalink
add ScheduledChangeabePartially
Browse files Browse the repository at this point in the history
  • Loading branch information
Gladhon committed Sep 16, 2015
1 parent 5f85f9f commit 533f780
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
58 changes: 51 additions & 7 deletions Listener/LoggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Ibrows\LoggableBundle\Entity\LogMany2Many;
use Ibrows\LoggableBundle\Entity\LogParent;
use Ibrows\LoggableBundle\Model\AbstractLogModel;
use Ibrows\LoggableBundle\Model\ScheduledChangeabePartially;
use Ibrows\LoggableBundle\Model\ScheduledChangeable;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
Expand Down Expand Up @@ -498,6 +499,8 @@ protected function addChangeSet($object, Log $logEntry, LoggableAdapter $ea)
$uow = $om->getUnitOfWork();
$meta = $om->getClassMetadata(get_class($object));
$hash = spl_object_hash($object);
$data = $logEntry->getData();
$oldData = $logEntry->getOldData();

if ($logEntry->getAction() == self::ACTION_REMOVE) {
//MANY_TO_ONE
Expand Down Expand Up @@ -530,7 +533,17 @@ protected function addChangeSet($object, Log $logEntry, LoggableAdapter $ea)
$uow->clearEntityChangeSet($hash);
} else {
// $undoSet = array();

$uow->clearEntityChangeSet($hash);
foreach ($logEntry->getOldData() as $field => $oldValue) {
if($object instanceof ScheduledChangeabePartially && !in_array($field,$object->getFieldsToSchedule())){
$value = null;
if(isset($data[$field])){
$value = $data[$field];
}
$uow->propertyChanged($object,$field,$oldValue,$value);
continue;
}
if ($meta->isSingleValuedAssociation($field)) {
$mapping = $meta->getAssociationMapping($field);
$oldValue = $oldValue ? $om->getReference($mapping['targetEntity'], $oldValue) : null;
Expand All @@ -539,27 +552,58 @@ protected function addChangeSet($object, Log $logEntry, LoggableAdapter $ea)
$ea->setOriginalObjectProperty($uow, $hash, $field, $oldValue);
// $undoSet[$field] = array(null,$oldValue);
}

// $uow->scheduleExtraUpdate($object,$undoSet);
// $changeSetMeta = $ea->getObjectManager()->getClassMetadata(get_class($object));
// $uow->computeChangeSet($changeSetMeta, $object);
$uow->clearEntityChangeSet($hash);

}
if($object instanceof ScheduledChangeabePartially ){
$changeSetDataKeys = array_keys($uow->getEntityChangeSet($object));
if(sizeof($changeSetDataKeys)>0){
//partial
foreach($changeSetDataKeys as $field){
unset($oldData[$field]);
unset($data[$field]);
}
$logEntry->setData($data);
$logEntry->setOldData($oldData);

$changeSet = $this->createChangeSet($logEntry,$date);
$om->persist($changeSet);
$changeSetMeta = $ea->getObjectManager()->getClassMetadata(get_class($changeSet));
$uow->computeChangeSet($changeSetMeta, $changeSet);

//create log also
return false;
}
}
}

$data = $logEntry->getData();

$changeSet = $this->createChangeSet($logEntry,$date);
$om->persist($changeSet);
$changeSetMeta = $ea->getObjectManager()->getClassMetadata(get_class($changeSet));
$uow->computeChangeSet($changeSetMeta, $changeSet);
return true;
}

/**
* @param Log $logEntry
* @param \DateTime $date
* @return ChangeSet
*/
private function createChangeSet(Log $logEntry, \DateTime $date){
$changeSet = new ChangeSet();
$changeSet->setChangeAt($date);
$changeSet->setObjectId($logEntry->getObjectId());
$changeSet->setObjectClass($logEntry->getObjectClass());
$changeSet->setData($data);
$changeSet->setData($logEntry->getData());
$changeSet->setOldData($logEntry->getOldData());
$changeSet->setUsername($logEntry->getUsername());
$changeSet->setAction($logEntry->getAction());
$om->persist($changeSet);
$changeSetMeta = $ea->getObjectManager()->getClassMetadata(get_class($changeSet));
$uow->computeChangeSet($changeSetMeta, $changeSet);
return $changeSet;

return true;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions Model/ScheduledChangeabePartially.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Ibrows\LoggableBundle\Model;


interface ScheduledChangeabePartially extends ScheduledChangeable
{
/**
* @return array
*/
public function getFieldsToSchedule();


}
34 changes: 33 additions & 1 deletion Tests/Loggable/Fixture/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Ibrows\LoggableBundle\Model\ScheduledChangeabePartially;
use Ibrows\LoggableBundle\Model\ScheduledChangeable;

/**
* @ORM\Entity
* @ORM\Table(name="fix_user")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class User implements ScheduledChangeable
class User implements ScheduledChangeabePartially
{
/**
* @var Article[]
Expand All @@ -33,6 +34,12 @@ class User implements ScheduledChangeable
* @ORM\Column(name="title", type="string", length=8)
*/
private $name;

/**
* @ORM\Column(name="company", type="string", length=100, nullable=true)
*/
private $company;

/**
* @var $deletedAt \DateTime
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
Expand Down Expand Up @@ -129,4 +136,29 @@ public function setName($name)
$this->name = $name;
}

/**
* @return array
*/
public function getFieldsToSchedule()
{
return array('name');
}

/**
* @return mixed
*/
public function getCompany()
{
return $this->company;
}

/**
* @param mixed $company
*/
public function setCompany($company)
{
$this->company = $company;
}


}
38 changes: 38 additions & 0 deletions Tests/Loggable/ScheduledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ibrows\LoggableBundle\Tests\Loggable\Fixture\Entity\Article;
use Ibrows\LoggableBundle\Tests\Loggable\Fixture\Entity\Comment;
use Ibrows\LoggableBundle\Tests\Loggable\Fixture\Entity\RelatedArticle;
use Ibrows\LoggableBundle\Tests\Loggable\Fixture\Entity\User;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

Expand All @@ -21,6 +22,43 @@ class ScheduledTest extends AbstractTest
*/
protected $changeRepo = null;

public function testPartiallyUpdate()
{
$this->assertCount(0, $this->logrepo->findAll());
$user = new User();
$user->setName('firstName');
$user->setCompany('firstCompany');
$this->em->persist($user);
$this->em->flush();
$this->assertCount(1, $this->logrepo->findAll());
$this->assertEquals(array('name'), $user->getFieldsToSchedule());
$this->assertEquals('firstName', $user->getName());
$this->assertEquals('firstCompany', $user->getCompany());
$user->setName('firstName new');
$user->setCompany('firstCompany new');
$user->setScheduledChangeDate(new \DateTime("+ 100 days"));
$this->em->persist($user);
$this->em->flush();

$this->assertCount(2, $this->logrepo->findAll());
$this->assertEquals('firstName', $user->getName());
$this->assertEquals('firstCompany new', $user->getCompany());

$changes = $this->changeRepo->findAll();
$this->assertCount(1, $changes);
$change = array_pop($changes);
$data = $change->getData();
$dataold = $change->getOldData();
$this->assertEquals($change->getObjectId(), $user->getId());
$this->assertEquals($change->getObjectClass(), get_class($user));
$this->assertArrayHasKey('name', $data);
$this->assertEquals('firstName new', $data['name']);
$this->assertArrayNotHasKey('company', $data);
$this->assertArrayHasKey('name', $dataold);
$this->assertEquals('firstName', $dataold['name']);
}


public function testDontEditDirect()
{
$this->assertCount(0, $this->logrepo->findAll());
Expand Down

0 comments on commit 533f780

Please sign in to comment.