Home

Awesome

Value objects for Doctrine ORM

Build Status

WARNING: this library is not ready for production yet

This library provides the tools for working with value objects and Doctrine entities.

This is the way you could persist a PhoneNumber value object as a field of a Person entity using the ValueObject annotation:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Acme\DemoBundle\Value\PhoneNumber;
use Noback\DoctrineOrmValueObject\Annotation\ValueObject;

/**
 * @ORM\Entity
 */
class Person
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ValueObject(class="Acme\DemoBundle\Value\PhoneNumber")
     */
    private $phoneNumber;

    private $phoneNumber_number;

    private $phoneNumber_since;

    public function getId()
    {
        return $this->id;
    }

    public function setPhoneNumber(PhoneNumber $phoneNumber)
    {
        $this->phoneNumber = $phoneNumber;
    }

    public function getPhoneNumber()
    {
        return $this->phoneNumber;
    }
}

The Person entity should have a corresponding property for each of the properties of the PhoneNumber class.

<?php

namespace Acme\DemoBundle\Value;

use Doctrine\ORM\Mapping\Column;

class PhoneNumber
{
    /**
     * @Column(type="string", length=255)
     */
    private $number;

    /**
     * @Column(type="datetime")
     */
    private $since;

    public function __construct($number, \DateTime $since)
    {
        $this->number = $number;
        $this->since = $since;
    }

    public function getNumber()
    {
        return $this->number;
    }

    public function getSince()
    {
        return $this->since;
    }
}

The PhoneNumber class contains the column definitions for the corresponding fields of the Person entity.

Event listeners make sure that:

Current limitations