Reputation: 2793
I am working with Doctrine2 (within Zend Framework, no Symfony2 around).
I have a "complex" doctrine2 object which has a one-to-many relation with two other objects.
The structure of my object looks like this:
$object->attribute1 = "foo";
$object->attribute2 = "bar";
$object->doctrineCollection1 = <DOCTRINE_COLLECTION2>;
$object->doctrineCollection1 = <DOCTRINE_COLLECTION2>;
I want to store it into Zend Cache somehow. What is the best way to serialize the complete object? I also tried to figure out how to encode it to JSON to get a hint, but wasn't successful yet.
Upvotes: 4
Views: 8976
Reputation: 2300
What you are trying to do is 'normalize' your Entity.
In your ZF2 Project's Composer:
"symfony/serializer" : "dev-master",
In your code:
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
$normalizer = new PropertyNormalizer();
$array = $normalizer->normalize( $entity );
That gives you an array, and from there, your options are limitless.
Good luck. Alex
Upvotes: 0
Reputation: 4130
Just initialize every proxy object of your collection, and related entities. Call some method (except getId) on it. Then you can serialize and unserialize your object. For example:
class Shop {
protected $owner;
protected $visitors;
}
// get shop from databse
...
// initialize proxy entitites
$shop->getOwner()->getName();
$shop->getVisitors()->forAll(function($key, $visitor) {
$visitor->getName();
})
// then you can serialize;
Upvotes: 0
Reputation: 9
I am facing same problem. Waiting for better solution for serializing Doctrine object. I have written code that converts doctrine object to Array [ array ==> json]
<?php
use Doctrine\ORM\PersistentCollection;
class MyDoctrineEntity
{
public function arrayFy($level=1 ,array $ignore=array()){
$maxLevel=3;
$dateFormat='Y-m-d H:i:s';
if(is_array($level)){
$ignore=$level;
$level=1;
}
$level=$level>$maxLevel?$maxLevel:$level;
$arr=array();
foreach($this as $key=>$val){
if(in_array($key ,$ignore))
continue;
elseif(is_bool($val)|| is_int($val)||is_string($val)||is_null($val)|| is_float($val) )
$arr[$key]=$val;
elseif( $val instanceof \DateTime)
$arr[$key]=$val->format($dateFormat);
elseif($val instanceof PersistentCollection && $level>0 )
{
$childArr=array();
if( count($val))
foreach($val as $x)
$childArr[]=$x->arrayFy($level-1,$ignore);
$arr[$key]=$childArr;
}elseif($key!='_entityPersister'&&$key!='_identifier'&&$key!='__isInitialized__' && !($val instanceof PersistentCollection))
$arr[$key]=$val->arrayFy($level-1,$ignore);
}
return $arr;
}
}
Extend this class while creating Doctrine entity
/**
* @Entity
* @Table(name="user")
*/
class User extends MyDoctrineEntity
{
And to convert doctrine to array
$user->arrayFy(); //
$user->arrayFy(2); // how deep you want to print ; default value : 1 , max value:3
$user->arrayFy( array('created_by','salary')); // columns that you dont want to store
$user->arrayFy(1,arra('created_by'));
Upvotes: 0
Reputation: 5454
Unlike what others have suggested, just using serialize()
won't work because it serializes a lot of internal doctrine stuff that you don't need.
IIRC, there is no easy way to serialize a Doctrine entity. You can use the EntityManager to retrieve the Class Metadata from which you can loop over properties and retrieve them into an array. You'd have to recursively nest down into related entities to get their values too.
I sort of started a library to help with serializing complex objects (but never finished it). If you dig into the minimal source code in that project you can get an idea of how to pull the values out. You can also take a look at this class which does the reverse (but doesn't nest into related objects).
I highly recommend digging into Doctrine's source code, it's quite educational. Also take a look the API docs, specifically you should look at Doctrine\ORM\Mapping\ClassMetadataInfo
.
Upvotes: 1