Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Retrieve the original object
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  18 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gaëtan Muller  
View profile  
 More options Jan 24 2012, 7:25 am
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Tue, 24 Jan 2012 04:25:58 -0800 (PST)
Local: Tues, Jan 24 2012 7:25 am
Subject: [Symfony2 / Doctrine2] Retrieve the original object

Hello everyone,

I have a method which allow me to edit an Entity in the database. This
method takes an Entity as an argument. This Entity is an edited version of
the existing one in the database.
I would like to get the database version of the Entity to compare the value
and perform certain taks. But it seems that

<?php$this->doctrine->getRepository('Class:Entity')->find($object->getId()) ;

gives me the modified object as well, and the databse has not been updated
yet.

Is there a way to get the unmodified object ?
Thanks for your help :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "[Symfony2 / Doctrine2] Retrieve the original object" by Marco Pivetta
Marco Pivetta  
View profile  
 More options Jan 24 2012, 7:30 am
From: Marco Pivetta <ocram...@gmail.com>
Date: Tue, 24 Jan 2012 13:30:55 +0100
Local: Tues, Jan 24 2012 7:30 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

You can either ask the UnitOfWork for the changes:
$em->getUnitOfWork()->getEntityChangeset($entity);
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Un...

or you can refresh the entity from DB, resetting it's status to what is on
DB.
$em->refresh($entity);

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 24 January 2012 13:25, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 24 2012, 8:21 am
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Tue, 24 Jan 2012 05:21:07 -0800 (PST)
Local: Tues, Jan 24 2012 8:21 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Hey, thanks for your answer.
$em->getUnitOfWork()->getEntityChangeset($entity);
Looks nice, but it returns an empty array. Do I have to call another method
before using this ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Pivetta  
View profile  
 More options Jan 24 2012, 8:22 am
From: Marco Pivetta <ocram...@gmail.com>
Date: Tue, 24 Jan 2012 14:22:12 +0100
Local: Tues, Jan 24 2012 8:22 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Well, it works only on persisted elements.
How are you currently using it?
Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 24 January 2012 14:21, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Allman  
View profile  
 More options Jan 24 2012, 8:49 am
From: Mark Allman <m...@bixbite.co.uk>
Date: Tue, 24 Jan 2012 13:49:31 +0000
Local: Tues, Jan 24 2012 8:49 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

On 24/01/2012 13:21, Ga�tan Muller wrote:

> Hey, thanks for your answer.
> $em->getUnitOfWork()->getEntityChangeset($entity);
> Looks nice, but it returns an empty array. Do I have to call another
> method before using this ?

Hi,

I had a similar problem. I wanted an equivalent of the Doctrine 1.x
$entity->isModified(), and after much documentation and API searching,
UnitOfWork seemed to provide the answers.

I am not an expert on the innards of Doctrine, so bear in mind that this
might be semi-wrong, but my conclusions were that:

$uow->getEntityChangeset($entity) or in my case
$uow->isEntityScheduled($entity) would provide no answers until:

$uow->computeChangeSets(); or $uow->computeChangeSet($class, $entity);
was called.

However, when I then went and later called $em->flush(), the UoW would
then internally call these methods again, and the entity would be queued
*twice*, which then led to nasty errors from the database (e.g. primary
key constraint error due to Doctrine attempting a double INSERT of the
same object).

My conclusions were that UoW was not meant to be messed with, that I
should be interacting with Doctrine at a higher level, and that if I
wanted to know these things, I'd have to track them myself, externally
to Doctrine. This seemed silly since it's a duplication of work
(Doctrine's doing it, as am I), but it seemed like the only viable route :/

Of course, if anyone knows of a solution, that would be wonderful, but I
believe this is the cause of your empty array.

-Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 24 2012, 8:55 am
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Tue, 24 Jan 2012 05:55:22 -0800 (PST)
Local: Tues, Jan 24 2012 8:55 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

I'm using SonataAdminBundle. When I edit an object, I have to perform some
actions (file moving maily) before the object is saved to the database. So
I use the preUpdate($entity)<http://sonata-project.org/bundles/admin/master/doc/reference/saving_h...> method.
So the object is already persisted in the database as I'm editing it.
$entity is the modified object.

@Mark, I'm only want to UPDATE objects, so maybe using
$uow->computeChangeSets() will work for me. I'll give a try and let you
know what happen.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 24 2012, 9:48 am
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Tue, 24 Jan 2012 06:48:42 -0800 (PST)
Local: Tues, Jan 24 2012 9:48 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Using  $uow->computeChangeSets(); seem to work for me, but I need to access
other property of the object as well.
So $em->refresh($entity); must be the best way. I juste need to work on the
correct implementation of the __clone() method, as $em->copy() isn't
implemented yet :(


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Pivetta  
View profile  
 More options Jan 24 2012, 10:36 am
From: Marco Pivetta <ocram...@gmail.com>
Date: Tue, 24 Jan 2012 16:36:39 +0100
Local: Tues, Jan 24 2012 10:36 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

To clone objects, I suggest to avoid __clone  and copy fields with some
static method within your entity, like:

public static function copyFromInstance(Entity $entity) {}

If you want to use __clone then you have to detach the entity and unset
it's identifier.

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 24 January 2012 15:48, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 24 2012, 10:42 am
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Tue, 24 Jan 2012 07:42:30 -0800 (PST)
Local: Tues, Jan 24 2012 10:42 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Thanks for the tip Marco. I'll do it your way. Thank you for your help ;)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Pivetta  
View profile  
 More options Jan 24 2012, 10:45 am
From: Marco Pivetta <ocram...@gmail.com>
Date: Tue, 24 Jan 2012 16:45:28 +0100
Local: Tues, Jan 24 2012 10:45 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

(unset the identifier on the clone object ;) )
Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 24 January 2012 16:42, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 24 2012, 1:28 pm
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Tue, 24 Jan 2012 10:28:17 -0800 (PST)
Local: Tues, Jan 24 2012 1:28 pm
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

In case someone want to do the same thing, here is my solution :

   1. In your entity, implement the __clone() method (remember to clone
   your object property) ;
   2. Where you want to retrieve your original object :

<?php$original = clone $entity; // Create a copy of your object$this->getDoctrine()->detach($entity); // Prevent your object from being refreshed$original = $this->getDoctrine()->merge($original); // Attach the copy to the EntityManager$this->getDoctrine()->refresh($original); // Get the database version of the entity
// Do stuff
$this->getDoctrine()->detach($original); // Detach the copy from the EntityManager$original = $this->getDoctrine()->merge($entity); // Attach the entity back to the EntityManager

Maybe not the best way, but it works :)
As Marco said, you need to unset the id field.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 25 2012, 8:22 am
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Wed, 25 Jan 2012 05:22:42 -0800 (PST)
Local: Wed, Jan 25 2012 8:22 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Actually, it's not working.
When I detech and merge back the entity, Doctrine update AND insert the
object in the database. So the code I gave in my last message update
$entity and insert a copy in the database. In this the normal behaviour ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Pivetta  
View profile  
 More options Jan 29 2012, 3:39 am
From: Marco Pivetta <ocram...@gmail.com>
Date: Sun, 29 Jan 2012 09:39:53 +0100
Local: Sun, Jan 29 2012 3:39 am
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

I don't really understand why you detach and merge the same object to the
entityManager...
Yes, it is the default behavior to refresh it, but still, you got a problem
there.
The logic imho (taking your code and rewriting it) is following:

$em->detach($entity); //first detach. We don't want to track anything!
$copy = clone $entity; //we don't want to clone relations to managed
entities, thus the clone happens after detaching
$copy->setId(null); //we unset the identifier, otherwise the entitymanager
will not insert the new object

//work with your object

$em->persist($copy); //it's about persisting, not merging.
$em->merge($entity); //if you really need the entity
$em->flush();

Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 25 January 2012 14:22, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Jan 29 2012, 2:13 pm
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Sun, 29 Jan 2012 11:13:12 -0800 (PST)
Local: Sun, Jan 29 2012 2:13 pm
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Doctrine is really new for me, so I might be trying to do stuff the wrong
way.
To get the original object, I found $uow->getOriginalEntityData($entity).
It looks fine right now. No need to clone the object I get as a parameter,
no detach or merge.

Do you think it's better ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Pivetta  
View profile  
 More options Jan 29 2012, 3:26 pm
From: Marco Pivetta <ocram...@gmail.com>
Date: Sun, 29 Jan 2012 21:26:45 +0100
Local: Sun, Jan 29 2012 3:26 pm
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Yeah, well, tracking changes to an object is actually the work of the
UnitOfWork, so you're actually playing with the correct tools :)

I was just trying to fix the code you wrote above. What's your final aim?
What should your code (put together) do?
Marco Pivetta

http://twitter.com/Ocramius

http://marco-pivetta.com

On 29 January 2012 20:13, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaëtan Muller  
View profile  
 More options Feb 2 2012, 12:29 pm
From: Gaëtan Muller <m.gaeta...@gmail.com>
Date: Thu, 2 Feb 2012 09:29:13 -0800 (PST)
Local: Thurs, Feb 2 2012 12:29 pm
Subject: Re: [doctrine-user] [Symfony2 / Doctrine2] Retrieve the original object

Hey,

Sorry for time it took me to answer.

I'm managing Products, and each Product belongs to a Category. So when
someone edit a Product, I want to check if the Category has change. If it's
the case, I need to update the Product position in the new Category, and
also update the Poducts position in the old Category (ie. decrementing
other product position so there is no hole).
That's why getting the changeset didn't fit my needs, as the Product
position wouldn't be in the array returned by the unit of work.
I also need to move folders if the product name/category is changed.

So $uow->getOriginalEntityData($entity) is really is best thing to do all
this
Thanks for your help Marco :D


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Retrieve the original object" by Art Hundiak
Art Hundiak  
View profile  
 More options Feb 2 2012, 2:58 pm
From: Art Hundiak <ahund...@gmail.com>
Date: Thu, 2 Feb 2012 11:58:42 -0800 (PST)
Local: Thurs, Feb 2 2012 2:58 pm
Subject: Re: [Symfony2 / Doctrine2] Retrieve the original object
When I first started with D2 I tried using the unit of work but never
seemed to get useful information from it.

Take a look at @ORM\ChangeTrackingPolicy("NOTIFY") and the
NotifyPropertyChanged interface.

Takes a bit to implement but once it's done it's easy to track
changes.

On Feb 2, 11:29 am, Gaëtan Muller <m.gaeta...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sébastien Lavoie  
View profile  
 More options Oct 3 2012, 4:00 pm
From: Sébastien Lavoie <sebast...@lavoie.sl>
Date: Wed, 3 Oct 2012 13:00:02 -0700 (PDT)
Local: Wed, Oct 3 2012 4:00 pm
Subject: Re: [Symfony2 / Doctrine2] Retrieve the original object

Reviving an old thread, after reading all this and trying a lot of stuff,
the only solution that worked for me was, for a preUpdate hook,

<?php
$uow = $em->getUnitOfWork();
$uow->computeChangeSets();
$changeset = $uow->getEntityChangeSet($entity);

It returns this: array('field_name' => array($old, $new), …);
Calling getOriginalEntityData returned null and computeChangeSets also
needed because changes were not computed.

Hope it helps somebody.

Seb
http://blog.lavoie.sl/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »