Careful! The entity manager's detach method does not return anything!
Marco Pivetta
@Ocramius <http://twitter.com/Ocramius>
http://marco-pivetta.com



On 16 May 2011 08:55, oscar balladares <liebegr...@gmail.com> wrote:

> According what I have read so far, serialize/unserialize $entities with
> proxies associations is not advised.
>
> And of course, if you are saving objects in session() vars, it is an
> innecesary load to save proxies information.
>
> So, it is adviced to detach proxies from entities (in case where entites
> are retrieved with the entity_manager).
>
> For example:
>
> public function someControllerAction($product_id)
> {
>     $product = $em->find('Vendor\someBundle\Entity\Product', $product_id);
>
>     //If you need to save the $product object in a session var, for example
> in a shopping cart implementation, which is actually the one that
>     // lead me to the issue/solution in first place.
>
>     //so, to detach proxy information from $product you must:
>
>     $product = $em->detach($product);
>
>     //then serialize it and save it in a session var.
> }
>
>
> If during unserialization you need the proxy information back, you should
>
> $product = unserialize($SerializedProductContainer);
> $product = $em->merge($product);
>
> Then you should be able to use proxy information of $product to retrieve
> associated entities (Lazy load).
>
> 2011/5/16 oscar balladares <liebegr...@gmail.com>
>
>> Hi Luis, I solved the problem.
>>
>> Before making the summary I must point that Tim was reffering to what I
>> thoght was "Serializable" about erroneously, sorry, my fault :D
>>
>> What I found (I have to start reading docs more deeply :P):
>>
>> The PHP bible says:
>>
>> "serialize() checks if your class has a function with the magic
>> name __sleep. If so, that function is executed prior to any serialization.
>> It can clean up the object and is supposed to return an array with the names
>> of all variables of that object that should be serialized. If the method
>> doesn't return anything then NULL is serialized and E_NOTICE is issued."
>>
>> E_NOTICE  is what I was getting in dev enviroment when trying to serialize
>> an entity object that has reference to another entity object (due to
>> associations). The serializations succeeded but I was getting that buggy
>> message >:(
>>
>> The PHP bible says:
>>
>> "It is not possible for __sleep to return names of private properties in
>> parent classes. Doing this will result in an E_NOTICE level error. Instead
>> you may use the Serializableinterface."
>>
>> So I found that Doctrine (I think in proxies) use the magic function
>> "__sleep()", so "serialize()" function exectues _sleep() method first, and
>> expect it to return the object's properties, but if they are private it will
>> fail, so if you change them to protected it will succeed.
>>
>> But changing properties scope is clumsy. \Serializable interface is the
>> solution, and this is the "how" IMHO :D
>>
>> Class object implements \Serializable
>> {
>>   private $name;
>>   private $address = array();
>>
>>   public function serialize()
>>   {
>>      return $data = array('name' => $this->name, 'address' =>
>> $this->address;
>>   }
>>
>>   public function unserialize($serialized)
>>   {
>>     $data = unserialize($serialized);
>>     $this->name = $data['name'];
>>     $this->address = $data['address'];
>>   }
>>
>>   //setters and getters for $name and $address
>>
>> }
>> Class object must implement \Serializable, this way when
>> serializing/unserializing the object, this will trigger the look for its two
>> abstract implementation methods: serialize() and unserialize()
>>
>> This way, "serialize" will have access to private members for
>> serialization, and when "unserialize" will look for the object's method
>> "unserialize" and will set unserialized $data to its private members.
>>
>> Both methods are like bridges to the private members.
>>
>> Notice that if Object A has a reference to Object B (like in associations)
>> Object B has to implement \Serializabe as well.
>>
>> And hope this help someone when having this same issue (that has nothing
>> to do with the framework itself, but PHP )
>>
>> Regards!
>>
>> 2011/5/15 Luis Cordova <cordo...@gmail.com>
>>
>>> nobody is doubting that Tim
>>>
>>> On Sun, May 15, 2011 at 7:48 PM, Tim Nagel <t...@nagel.com.au> wrote:
>>> > They're talking about: http://php.net/manual/en/class.serializable.php
>>> >
>>> > Not the Serializer component in Symfony2.
>>> >
>>> >
>>> > t
>>> >
>>> >
>>> > On Mon, May 16, 2011 at 08:52, oscar balladares <liebegr...@gmail.com>
>>> wrote:
>>> >> I'm having this problem too. Core Devs suggest to implement
>>> Serializable
>>> >> interface.
>>> >> Symfony has Serializer classes and Interfaces, but I still dont know
>>> how to
>>> >> implement it/them.
>>> >> If you are lucky, please share what you have found, if any.
>>> >> Regards!
>>> >>
>>> >> 2011/5/11 Luis Cordova <cordo...@gmail.com>
>>> >>>
>>> >>> right but this was not what was happening.
>>> >>> I had a chain of entities, and they all were entities, regular POPOs.
>>> >>> And I had to implement serialize method for one of them for this
>>> thing
>>> >>> to work.
>>> >>>
>>> >>> On Wed, May 11, 2011 at 2:19 AM, Marco Pivetta <ocram...@gmail.com>
>>> wrote:
>>> >>> > The ORM uses proxy classes internally. Thus, while working with the
>>> >>> > proxies
>>> >>> > (when you load stuff from DB you usually receive a proxy from the
>>> Object
>>> >>> > Hydrator), you don't have access to private vars defined in your
>>> >>> > entities.
>>> >>> > That's why serialization of a proxy fails (and you should not
>>> serialize
>>> >>> > proxies! detach them from the entity manager first to get a clean
>>> POPO!)
>>> >>> >
>>> >>> > Marco Pivetta
>>> >>> > @Ocramius
>>> >>> > http://marco-pivetta.com
>>> >>> >
>>> >>> >
>>> >>> >
>>> >>> > On 11 May 2011 01:10, Christophe COEVOET <s...@notk.org> wrote:
>>> >>> >>
>>> >>> >> Le 10/05/2011 13:25, AndyPI a écrit :
>>> >>> >>>
>>> >>> >>> Don't know if it helps, but changing my variables from 'private'
>>> to
>>> >>> >>> 'protected' in Symfony2 entities removed this problem for me.
>>> >>> >>>
>>> >>> >>> The Symfony2 docs use protected in their examples, but the
>>> console
>>> >>> >>> generates variables as private. Something to look at?
>>> >>> >>
>>> >>> >> The Doctrine doc advices to use the \Serializable interface:
>>> >>> >>
>>> >>> >>
>>> http://www.doctrine-project.org/docs/orm/2.0/en/reference/architecture.html
>>> >>> >>
>>> >>> >> --
>>> >>> >> Christophe | Stof
>>> >>> >>
>>> >>> >> --
>>> >>> >> If you want to report a vulnerability issue on symfony, please
>>> send it
>>> >>> >> to
>>> >>> >> security at symfony-project.com
>>> >>> >>
>>> >>> >> You received this message because you are subscribed to the Google
>>> >>> >> Groups "symfony users" group.
>>> >>> >> To post to this group, send email to
>>> symfony-users@googlegroups.com
>>> >>> >> To unsubscribe from this group, send email to
>>> >>> >> symfony-users+unsubscr...@googlegroups.com
>>> >>> >> For more options, visit this group at
>>> >>> >> http://groups.google.com/group/symfony-users?hl=en
>>> >>> >
>>> >>> > --
>>> >>> > If you want to report a vulnerability issue on symfony, please send
>>> it
>>> >>> > to
>>> >>> > security at symfony-project.com
>>> >>> >
>>> >>> > You received this message because you are subscribed to the Google
>>> >>> > Groups "symfony users" group.
>>> >>> > To post to this group, send email to
>>> symfony-users@googlegroups.com
>>> >>> > To unsubscribe from this group, send email to
>>> >>> > symfony-users+unsubscr...@googlegroups.com
>>> >>> > For more options, visit this group at
>>> >>> > http://groups.google.com/group/symfony-users?hl=en
>>> >>> >
>>> >>>
>>> >>> --
>>> >>> If you want to report a vulnerability issue on symfony, please send
>>> it to
>>> >>> security at symfony-project.com
>>> >>>
>>> >>> You received this message because you are subscribed to the Google
>>> >>> Groups "symfony users" group.
>>> >>> To post to this group, send email to symfony-users@googlegroups.com
>>> >>> To unsubscribe from this group, send email to
>>> >>> symfony-users+unsubscr...@googlegroups.com
>>> >>> For more options, visit this group at
>>> >>> http://groups.google.com/group/symfony-users?hl=en
>>> >>
>>> >> --
>>> >> If you want to report a vulnerability issue on symfony, please send it
>>> to
>>> >> security at symfony-project.com
>>> >>
>>> >> You received this message because you are subscribed to the Google
>>> >> Groups "symfony users" group.
>>> >> To post to this group, send email to symfony-users@googlegroups.com
>>> >> To unsubscribe from this group, send email to
>>> >> symfony-users+unsubscr...@googlegroups.com
>>> >> For more options, visit this group at
>>> >> http://groups.google.com/group/symfony-users?hl=en
>>> >>
>>> >
>>> > --
>>> > If you want to report a vulnerability issue on symfony, please send it
>>> to security at symfony-project.com
>>> >
>>> > You received this message because you are subscribed to the Google
>>> > Groups "symfony users" group.
>>> > To post to this group, send email to symfony-users@googlegroups.com
>>> > To unsubscribe from this group, send email to
>>> > symfony-users+unsubscr...@googlegroups.com
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/symfony-users?hl=en
>>> >
>>>
>>> --
>>> If you want to report a vulnerability issue on symfony, please send it to
>>> security at symfony-project.com
>>>
>>> You received this message because you are subscribed to the Google
>>> Groups "symfony users" group.
>>> To post to this group, send email to symfony-users@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> symfony-users+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/symfony-users?hl=en
>>>
>>
>>
>  --
> If you want to report a vulnerability issue on symfony, please send it to
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony users" group.
> To post to this group, send email to symfony-users@googlegroups.com
> To unsubscribe from this group, send email to
> symfony-users+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/symfony-users?hl=en
>

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to