Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[PreUpdateEventArgs] Allows to add a new value on the entity changeset #600

Closed
wants to merge 1 commit into from

4 participants

@egeloen

Hey!

I propose to add a addNewValue method on the PreUpdateEventArgs. This method allows to add a new value on the entity changeset when the entity changeset does not wrap the field.

In one of my app, I need to update a field if an entity is updated but the field is not updated at the beginning.

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DDC-2331

@stof stof commented on the diff
lib/Doctrine/ORM/Event/PreUpdateEventArgs.php
((6 lines not shown))
+ * If the field already exists, it sets the new value.
+ *
+ * @param string $field
+ * @param mixed $value
+ *
+ * @return void
+ */
+ public function addNewValue($field, $value)
+ {
+ $metadata = $this->getEntityManager()->getClassMetadata(get_class($this->getEntity()));
+
+ if ( ! $metadata->hasField($field)) {
+ $this->assertValidField($field);
+ }
+
+ $this->entityChangeSet[$field][1] = $value;
@stof
stof added a note

This will not create a valid changeset as you don't set the old value.

Btw, you can set the value in your entity and use $event->getEntityManager()->getUnitOfWork()->recomputeSingleEntityChangeSet($event->getEntity())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@egeloen

@stof Thx! I didn't think to recompute the changeset in the event...

@egeloen egeloen closed this
@robfrawley

Was a native solution merged?

@egeloen

The native solution is to recompute the entity changeset in the event :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 0 deletions.
  1. +21 −0 lib/Doctrine/ORM/Event/PreUpdateEventArgs.php
View
21 lib/Doctrine/ORM/Event/PreUpdateEventArgs.php
@@ -117,6 +117,27 @@ public function setNewValue($field, $value)
}
/**
+ * Adds a new field value in the entity changeset.
+ *
+ * If the field already exists, it sets the new value.
+ *
+ * @param string $field
+ * @param mixed $value
+ *
+ * @return void
+ */
+ public function addNewValue($field, $value)
+ {
+ $metadata = $this->getEntityManager()->getClassMetadata(get_class($this->getEntity()));
+
+ if ( ! $metadata->hasField($field)) {
+ $this->assertValidField($field);
+ }
+
+ $this->entityChangeSet[$field][1] = $value;
@stof
stof added a note

This will not create a valid changeset as you don't set the old value.

Btw, you can set the value in your entity and use $event->getEntityManager()->getUnitOfWork()->recomputeSingleEntityChangeSet($event->getEntity())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ /**
* Asserts the field exists in changeset.
*
* @param string $field
Something went wrong with that request. Please try again.