Skip to content
ScorpioT1000 edited this page Nov 18, 2016 · 7 revisions

Introduction

This library allows you to transform Doctrine ORM Entities into nested arrays of scalar data types and vise versa. It can be used together with JSON, XML, YAML or any serializer/deserializer that supports php nested assoc arrays and scalar types.

It provides flexible policies to format and parse your data and follow different security rules.

Features

  • JSON-ready toArray and fromArray Trait (no need to extend class);
  • Manipulating fields and nested sub-fields using Policy for each one;
  • Supports all Doctrine ORM Column types;
  • Supports JavaScript ISO8601 format for "date", "time" and "datetime" types;
  • Supports nested Entities and Collections for all the Association types (be careful with self-referencing);
  • fromArray asks EntityManager to find by "referencedColumnName" or creates new sub-entities (depends on Identifier emptiness and Policy);
  • Same for Collection members (OneToMany, ManyToMany);
  • Static toArrays method transforms multiple entities at once;
  • Has workarounds for CVE-2015-0231 and Doctrine issue #4673;

Installation

in composer.json "require" section add:

    "Indaxia/doctrine-orm-transformations": "^2.*"

then

> composer update

Usage

namespace Myns;

use Doctrine\ORM\Mapping as ORM;
use \Indaxia\OTR\ITransformable;
use \Indaxia\OTR\Traits\Transformable;
use \Indaxia\OTR\Annotations\Policy;

/** @ORM\Entity */
class Simple implements ITransformable {
    use Transformable;
    
    /** @ORM\Id
     * @ORM\Column(type="integer") */
    protected $id;
    
    /** @ORM\Column(type="string") */
    protected $value;
    
    public function getId() { return $this->id; }
    public function setId($v) { $this->id = $v; return $this; }
    public function getValue() { return $this->value; }
    public function setValue($v) { $this->value = $v; return $this; }
}

$e = (new Simple())->setId(1)->setValue('example');
$output = $e->toArray(); // Output for JSON/XML/YAML/... serializer
/* $output equals to
[ 
    '__meta' => ['class' => 'Myns\Simple'],
    'id' => 1,
    'value' => 'example'
]
*/

// Input from JSON/XML/YAML/... serializer
$input = [ 
    '__meta' => ['class' => 'Myns\Simple'],
    'value' => 'example'
];

$e = new Simple();
$e->fromArray($input, $entityManager); // Doctrine ORM Entitymanager instance

Documentation