How can I serialize an object (complex) doctrine 2?

I work with Doctrine2 (as part of the Zend Framework, without Symfony2).

I have a "complex" doctrine2 object that has a one-to-many relationship with two other objects.

The structure of my object is as follows:

$object->attribute1 = "foo"; $object->attribute2 = "bar"; $object->doctrineCollection1 = <DOCTRINE_COLLECTION2>; $object->doctrineCollection1 = <DOCTRINE_COLLECTION2>; 

I want to somehow save it to Zend Cache. What is the best way to serialize a complete object? I also tried to figure out how to encode it in JSON to get a hint, but so far failed.

+4
source share
4 answers

Unlike others suggested, just using serialize() will not work, because it serializes many internal doctrines that you don't need.

IIRC, there is no easy way to serialize a Doctrine entity. You can use EntityManager to retrieve class metadata from which you can iterate over properties and retrieve them into an array. You will also have to recursively enter related objects to get their values.

I kind of started a library that will help with the serialization of complex objects (but never finished). If you delve into the minimal source code in this project, you can get an idea of ​​how to pull the values. You can also take a look at this class , which does the opposite (but does not embed in related objects).

I highly recommend delving into the Doctrine source code, which is pretty educational. Also look at the API docs , in particular, you should look at Doctrine\ORM\Mapping\ClassMetadataInfo .

+1
source

I faced the same problem. Waiting for a better solution to serialize a Doctrine object. I wrote code that converts a doctrine object to Array [array ==> json]

 <?php use Doctrine\ORM\PersistentCollection; class MyDoctrineEntity { public function arrayFy($level=1 ,array $ignore=array()){ $maxLevel=3; $dateFormat='Ymd 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 when creating a Doctrine object

  /** * @Entity * @Table(name="user") */ class User extends MyDoctrineEntity { 

And to convert the doctrine into an 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')); 
0
source

Just initialize each proxy object in your collection and related objects. Call on it some method (except getId). Then you can serialize and non-sterilize your object. For instance:

 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; 
0
source

What you are trying to do is "normalize" your Entity.

In your ZF2 Project Composer:

 "symfony/serializer" : "dev-master", 

In your code:

 use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; $normalizer = new PropertyNormalizer(); $array = $normalizer->normalize( $entity ); 

This gives you an array, and from there your options are endless.

Good luck. Alex

0
source

Source: https://habr.com/ru/post/1394810/


All Articles