-
-
Save doctrinebot/9c43acb2ee7307de0296 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-414 - https://github.com/doctrine/doctrine2/issues/4907
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Property changes on: . | |
___________________________________________________________________ | |
Modified: svn:ignore | |
- build | |
+ reports | |
logs | |
build | |
dist | |
Index: tests/Doctrine/Tests/ORM/Functional/AllTests.php | |
=================================================================== | |
--- tests/Doctrine/Tests/ORM/Functional/AllTests.php (revision 7334) | |
+++ tests/Doctrine/Tests/ORM/Functional/AllTests.php (working copy) | |
@@ -47,6 +47,7 @@ | |
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OrderedJoinedTableInheritanceCollectionTest'); | |
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\ReferenceProxyTest'); | |
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\LifecycleCallbackTest'); | |
+ #$suite->addTestSuite('Doctrine\Tests\ORM\Functional\FlushEventTest'); | |
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\StandardEntityPersisterTest'); | |
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\MappedSuperclassTest'); | |
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\EntityRepositoryTest'); | |
Index: tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php | |
=================================================================== | |
--- tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php (revision 7334) | |
+++ tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php (working copy) | |
@@ -1,6 +1,7 @@ | |
<?php | |
namespace Doctrine\Tests\ORM\Functional; | |
+use Doctrine\ORM\Event\PreInsertUpdateEventArgs; | |
require_once __DIR__ . '/../../TestInit.php'; | |
@@ -111,6 +112,32 @@ | |
$childMeta = $this->_em->getClassMetadata(__NAMESPACE__ . '\LifecycleCallbackChildEntity'); | |
$this->assertEquals(array('prePersist' => array(0 => 'doStuff')), $childMeta->lifecycleCallbacks); | |
} | |
+ | |
+ public function testLifecycleListener_ChangeUpdateChangeSet() | |
+ { | |
+ $listener = new LifecycleListenerPreUpdate; | |
+ $this->_em->getEventManager()->addEventListener(array('preUpdate'), $listener); | |
+ | |
+ $user = new LifecycleCallbackTestUser; | |
+ $user->setName('Bob'); | |
+ $user->setValue('value'); | |
+ $this->_em->persist($user); | |
+ $this->_em->flush(); | |
+ $this->_em->clear(); | |
+ | |
+ $dql = "SELECT u FROM Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser u WHERE u.name = 'Bob'"; | |
+ $bob = $this->_em->createQuery($dql)->getSingleResult(); | |
+ $bob->setName('Alice'); | |
+ | |
+ $this->_em->flush(); // preUpdate reverts Alice to Bob | |
+ $this->_em->clear(); | |
+ | |
+ $this->_em->getEventManager()->removeEventListener(array('preUpdate'), $listener); | |
+ | |
+ $bob = $this->_em->createQuery($dql)->getSingleResult(); | |
+ | |
+ $this->assertEquals('Bob', $bob->getName()); | |
+ } | |
} | |
/** @Entity @HasLifecycleCallbacks */ | |
@@ -219,3 +246,11 @@ | |
/** @Id @Column(type="integer") @GeneratedValue */ | |
private $id; | |
} | |
+ | |
+class LifecycleListenerPreUpdate | |
+{ | |
+ public function preUpdate(PreInsertUpdateEventArgs $eventArgs) | |
+ { | |
+ $eventArgs->setNewValue('name', 'Bob'); | |
+ } | |
+} | |
\ No newline at end of file | |
Index: lib/Doctrine/ORM/Event/PreInsertUpdateEventArgs.php | |
=================================================================== | |
--- lib/Doctrine/ORM/Event/PreInsertUpdateEventArgs.php (revision 7334) | |
+++ lib/Doctrine/ORM/Event/PreInsertUpdateEventArgs.php (working copy) | |
@@ -2,33 +2,97 @@ | |
namespace Doctrine\ORM\Event; | |
-use Doctrine\Common\EventArgs; | |
+use Doctrine\Common\EventArgs, | |
+ Doctrine\ORM\EntityManager; | |
/** | |
* Class that holds event arguments for a preInsert/preUpdate event. | |
* | |
* @author Roman Borschel <roman@code-factory.org> | |
+ * @author Benjamin Eberlei <kontakt@beberlei.de> | |
* @since 2.0 | |
*/ | |
-class PreInsertUpdateEventArgs extends EventArgs | |
+class PreInsertUpdateEventArgs extends LifecycleEventArgs | |
{ | |
- private $_entity; | |
+ /** | |
+ * @var array | |
+ */ | |
private $_entityChangeSet; | |
- public function __construct($entity, array $changeSet) | |
+ /** | |
+ * | |
+ * @param object $entity | |
+ * @param EntityManager $em | |
+ * @param array $changeSet | |
+ */ | |
+ public function __construct($entity, $em, array &$changeSet) | |
{ | |
- $this->_entity = $entity; | |
- $this->_entityChangeSet = $changeSet; | |
+ parent::__construct($entity, $em); | |
+ $this->_entityChangeSet = &$changeSet; | |
} | |
- public function getEntity() | |
+ public function getEntityChangeSet() | |
{ | |
- return $this->_entity; | |
+ return $this->_entityChangeSet; | |
} | |
- | |
- public function getEntityChangeSet() | |
+ | |
+ /** | |
+ * Field has a changeset? | |
+ * | |
+ * @return bool | |
+ */ | |
+ public function hasChangedField($field) | |
{ | |
- return $this->_entityChangeSet; | |
+ return isset($this->_entityChangeSet[$field]); | |
} | |
+ | |
+ /** | |
+ * Get the old value of the changeset of the changed field. | |
+ * | |
+ * @param string $field | |
+ * @return mixed | |
+ */ | |
+ public function getOldValue($field) | |
+ { | |
+ $this->_assertValidField($field); | |
+ | |
+ return $this->_entityChangeSet[$field][0]; | |
+ } | |
+ | |
+ /** | |
+ * Get the new value of the changeset of the changed field. | |
+ * | |
+ * @param string $field | |
+ * @return mixed | |
+ */ | |
+ public function getNewValue($field) | |
+ { | |
+ $this->_assertValidField($field); | |
+ | |
+ return $this->_entityChangeSet[$field][1]; | |
+ } | |
+ | |
+ /** | |
+ * Set the new value of this field. | |
+ * | |
+ * @param string $field | |
+ * @param mixed $value | |
+ */ | |
+ public function setNewValue($field, $value) | |
+ { | |
+ $this->_assertValidField($field); | |
+ | |
+ $this->_entityChangeSet[$field][1] = $value; | |
+ } | |
+ | |
+ private function _assertValidField($field) | |
+ { | |
+ if (!isset($this->_entityChangeSet[$field])) { | |
+ throw new \InvalidArgumentException( | |
+ "Field '".$field."' is not a valid field of the entity ". | |
+ "'".get_class($this->getEntity())."' in PreInsertUpdateEventArgs." | |
+ ); | |
+ } | |
+ } | |
} | |
Index: lib/Doctrine/ORM/UnitOfWork.php | |
=================================================================== | |
--- lib/Doctrine/ORM/UnitOfWork.php (revision 7334) | |
+++ lib/Doctrine/ORM/UnitOfWork.php (working copy) | |
@@ -752,15 +752,13 @@ | |
if ($hasPreUpdateLifecycleCallbacks) { | |
$class->invokeLifecycleCallbacks(Events::preUpdate, $entity); | |
- if ( ! $hasPreUpdateListeners) { | |
- // Need to recompute entity changeset to detect changes made in the callback. | |
- $this->recomputeSingleEntityChangeSet($class, $entity); | |
- } | |
+ $this->recomputeSingleEntityChangeSet($class, $entity); | |
} | |
+ | |
if ($hasPreUpdateListeners) { | |
- $this->_evm->dispatchEvent(Events::preUpdate, new LifecycleEventArgs($entity, $this->_em)); | |
- // Need to recompute entity changeset to detect changes made in the listener. | |
- $this->recomputeSingleEntityChangeSet($class, $entity); | |
+ $this->_evm->dispatchEvent(Events::preUpdate, new Event\PreInsertUpdateEventArgs( | |
+ $entity, $this->_em, $this->_entityChangeSets[$oid]) | |
+ ); | |
} | |
$persister->update($entity); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment