ITransformable Interface - Indaxia/doctrine-orm-transformations GitHub Wiki

ITransformable provides ORM Entity-Array transformation methods.

Usage

namespace Myns;

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

class MyEntity implements ITransformable {
    use Transformable;

    // ...

toArray

Definition

    /** Converts Entity and it's references to nested array structure.
     *  @param Policy\Interfaces\Policy|null transfromation policy, null equals to Policy\Auto
     *  @param Reader $ar for internal recursive purposes
     *  @param PolicyResolver $pr for internal recursive purposes
     *  @return array ready for JSON serialization.
     *  @see readme.md
     *  @throws Exception when input type or policy aren't acceptable
     *  It excludes any static values.
    */
    public function toArray(
        Policy\Interfaces\Policy $policy = null,
        Reader $ar = null,
        PolicyResolver $pr = null
    );

Usage

$e = new MyEntity();

// ...

$result = $e->toArray();

// echo json_encode($result);

ITransformable::toArray Documentation

fromArray

Definition

    /** Converts fills Entity's fields (including nested Entity and Collection) to the values from the given array.
     *  @param array A special array ready for ITransformable, so it should include '__meta' array
     *  @param EntityManagerInterface $entityManager Doctrine instance to retrieve nested Entities by ID or create new ones.
     *  @param Policy\Interfaces\Policy|null transfromation policy, null equals to Policy\Auto
     *  @param Reader $ar for internal recursive purposes
     *  @param PolicyResolver $pr for internal recursive purposes
     *  @see readme.md
     *  @throws Exception when input type or policy aren't acceptable
     *  @return ITransformable $this
     *  It doesn't process any static values.
     */
    public function fromArray(
        array $src,
        EntityManagerInterface $entityManager,
        Policy\Interfaces\Policy $policy = null,
        Reader $ar = null,
        PolicyResolver $pr = null
    );

Usage

$entityManager; // Doctrine EntityManager reference

$input = [ 
    '__meta' => ['class' => 'MyNs\MyEntity'],
    'someproperty' => 'somevalue'
];

$e = $entityManager->getRepository('MyEntity')->findOneBy(['id' => 1]);
$e->fromArray($input, $entityManager);

ITransformable::fromArray Documentation

toArrays

Definition

    /** Applies toArray to multiple entities.
     * @param array $entities array of entities
     * @param array $policy
     * @param Reader $ar for internal recursive purposes
     * @param PolicyResolver $pr for internal recursive purposes
     * @return array
     * @see ITransformable::toArray */
    public static function toArrays(
        array $entities,
        Policy\Interfaces\Policy $policy = null,
        Reader $ar = null,
        PolicyResolver $pr = null
    );

OTR uses Doctrine CachedReader by default to improve multiple transformations performance.

Usage

$entityManager; // Doctrine EntityManager reference

$entities = $entityManager->getRepository('Myns\MyEntity')->findAll();

$results = Transformable::toArrays($entities);