2017 © Pedro Peláez
 

library doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

image

indaxia/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  • Wednesday, October 25, 2017
  • by ScorpioT1000
  • Repository
  • 4 Watchers
  • 4 Stars
  • 7,949 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 5 Open issues
  • 36 Versions
  • 30 % Grown

The README.md

JSON-ready Doctrine ORM Entity-Array Transformations

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;

Step 1: Installation

in composer.json add:, (*1)

"require": {

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

then, (*2)

> cd <your doc root>
> composer update

Requirements & Restrictions, (*3)

Step 2: Reference common classes

use \Indaxia\OTR\ITransformable;
use \Indaxia\OTR\Traits\Transformable;
use \Indaxia\OTR\Annotations\Policy;

Documentation

Full Documentation, (*4)

How to transform entities to arrays and vice versa

Let's say we have the following entities:, (*5)

    class Car implements ITransformable {
        use Transformable;

        /** @ORM\Id
         * @ORM\Column(type="integer") */
        protected $id;

        /** @Policy\To\Skip
         * @ORM\Column(type="string") */
        protected $keys;

        /** @ORM\OneToMany(targetEntity="Wheel") ... */
        protected $wheels;

        public function getId();
        public function getKeys() ...
        public function setKeys($v) ...
        ...
    }

    class Engine implements ITransformable {
        use Transformable;

        /** @ORM\Id
         * @ORM\Column(type="integer") */
        protected $id;

        /** @Policy\To\Skip
         * @ORM\Column(type="string") */
        protected $serialNumber;

        public function getId();
        public function getSerialNumber() ...
        public function setSerialNumber($v) ...
        ...
    }

    class Wheel implements ITransformable {
        use Transformable;

        /** @ORM\Id
         * @ORM\Column(type="integer") */
        protected $id;

        /** @Policy\Skip
         * @ORM\Column(type="string") */
        protected $brakes;

        /** @ORM\Column(type="string") */
        protected $model;

        public function getId();
        public function getBrakes() ...
        public function setBrakes($v) ...
        public function getModel() ...
        public function setModel($v) ...
        ...
    }

Here we have some $car. Let's transform it to array., (*6)

// Using global policy
$result = $car->toArray();

// Using local policy
$result = $car->toArray((new Policy\Auto)->inside([
    'wheels' => new Policy\To\FetchPaginate(['offset'=0, 'limit'=4, 'fromTail'=false])
]));

// Local policy overrides global policy
$result = $car->toArray((new Policy\Auto)->inside([
    'keys' => new Policy\Auto
]));

Policies Documentation, (*7)

$result will be something like:, (*8)

[
    '__meta' => ['class' => 'Car'],
    'id' => 1,
    'engine' => [
        '__meta' => ['class' => 'Engine', 'association' => 'OneToOne'],
        'id' => 83
    ],
    'wheels' => [
        '__meta' => ['class' => 'Wheel', 'association' => 'OneToMany'],
        'collection' => [
            [
                '_meta' => ['class' => 'Wheel'],
                'id' => 1,
                'model' => 'A'
            ],
            [
                '_meta' => ['class' => 'Wheel'],
                'id' => 2,
                'model' => 'A'
            ],
            [
                '_meta' => ['class' => 'Wheel'],
                'id' => 3,
                'model' => 'B'
            ],
            [
                '_meta' => ['class' => 'Wheel'],
                'id' => 4,
                'model' => 'B'
            ]
        ]
    ]
]

It's ready for JSON transformation!, (*9)

    echo json_encode($result);

You can also use something like array2XML and more., (*10)

And we can transform it to Entity again. It will retrieve sub-entities by id using EntityManager. Don't forget to use try-catch block to avoid uncaught exceptions., (*11)

$carB = new Car();

// Simple way
$carB->fromArray($result, $entityManager);

// With Policy
$carB->fromArray($result, $entityManager, (new Policy\Auto())->inside([
    'keys' => mew Policy\Skip,
    'engine' => (new Policy\Auto())->inside([
        'serialNumber' => new Policy\From\DenyNewUnset
    ]),
    'wheels' => (new Policy\Auto())->inside([
        'brakes' => new Policy\From\Auto
    ])
]);

Policies Documentation, (*12)

Documentation

Full Documentation, (*13)


Indaxia / 2016, (*14)

The Versions

25/10 2017

dev-master

9999999-dev http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ScorpioT1000

orm json array doctrine entity

18/11 2016

2.0.1-stable

2.0.1.0 http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ScorpioT1000

orm json array doctrine entity

18/11 2016

2.0.0-stable

2.0.0.0 http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

1.0.4

1.0.4.0 http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

1.0.3

1.0.3.0 http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

1.0.2

1.0.2.0 http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

1.0.1

1.0.1.0 http://indaxia.github.io/doctrine-orm-transformations/

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

1.0.0

1.0.0.0 https://github.com/Indaxia/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

0.3.0

0.3.0.0 https://github.com/Indaxia/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

10/11 2016

0.2.13-beta

0.2.13.0-beta https://github.com/Indaxia/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

09/11 2016

0.2.12-beta

0.2.12.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

08/11 2016

0.2.11-beta

0.2.11.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

03/11 2016

0.2.10-beta

0.2.10.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

01/11 2016

0.2.9-beta

0.2.9.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

28/10 2016

0.2.8-beta

0.2.8.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

28/10 2016

0.2.7-beta

0.2.7.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

27/10 2016

0.2.6-beta

0.2.6.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

27/10 2016

0.2.5-beta

0.2.5.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

27/10 2016

0.2.4-beta

0.2.4.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

23/10 2016

0.2.3-beta

0.2.3.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

22/10 2016

0.0.2-beta

0.0.2.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

22/10 2016

0.2.1-beta

0.2.1.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.2.0-beta

0.2.0.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.1.4-beta

0.1.4.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.1.3-beta

0.1.3.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.1.2-beta

0.1.2.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.1.1-beta

0.1.1.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.1.0-beta

0.1.0.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.0.7-alpha

0.0.7.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

18/10 2016

0.0.6-alpha

0.0.6.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

17/10 2016

0.0.5-alpha

0.0.5.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

17/10 2016

0.0.4-alpha

0.0.4.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

17/10 2016

0.0.3-alpha

0.0.3.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

17/10 2016

0.0.2-alpha

0.0.2.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

17/10 2016

0.0.1-alpha

0.0.1.0-alpha https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity

17/10 2016

0.2.2-beta

0.2.2.0-beta https://github.com/ScorpioT1000/doctrine-orm-transformations

Provides JSON-ready Doctrine ORM Entity-Array transfomtaions

  Sources   Download

MIT

The Requires

 

by Avatar ScorpioT1000

orm json array doctrine entity