[PR #5841] Embeddable value object #9753

Open
opened 2026-01-22 16:05:18 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/5841

State: closed
Merged: No


Doctrine and Value Object (not Embeddable)

Many times I need to return with a Value Object from an Entity. Frequently we simply create the Value Object in the Entity`s getter. When we do, we add extra cost every time when we call the getter, because it always create a new instance of a VO + need to convert back and forth the value in the Entity's setter / getter.

/**
 * @Entity
 */
class DDC2016User
{
    /** @Id @Column(type="integer") @GeneratedValue */
    public $id;

    /**
     * @var string
     *
     * @Column(type="string", length=128, nullable=false)
     */
    public $username;

    /** Constructor
     *
     * @param DDC2016Username $username
     */
    public function __construct(DDC2016Username $username)
    {
        $this->username = $username->__toString(); //Convert back
    }

    /**
     * @return DDC2016Username
     */
    public function getUsername()
    {
        return new Username($this->username); //Convert forth
    }

This PR add a DoctrineValueObject interface to the project. If a Value Object implements this interface, doctrine will call the equals method on the VO to compare instances instead of using the === comparison. Developer can define the rules how the changes has been determined.

Example of usage:

/**
 * @Entity
 */
class DDC2016User
{
    /** @Id @Column(type="integer") @GeneratedValue */
    public $id;

    /**
     * @var string
     *
     * @Column(type="string", length=128, nullable=false)
     */
    public $username;

    /** Constructor
     *
     * @param DDC2016Username $username
     */
    public function __construct(DDC2016UsernameVO $username)
    {
        $this->username = $username; //Convert back gone away
    }

    /**
     * @return DDC2016Username
     */
    public function getUsername()
    {
        return ($this->username instanceof DDC2016UsernameVO)
                   ? $this->username
                   : $this->username = new Username($this->username); //Create instance one per lifecycle
    }
**Original Pull Request:** https://github.com/doctrine/orm/pull/5841 **State:** closed **Merged:** No --- ## Doctrine and Value Object (not Embeddable) Many times I need to return with a `Value Object` from an Entity. Frequently we simply create the Value Object in the Entity`s getter. When we do, we add extra cost every time when we call the getter, because it always create a new instance of a VO + need to convert back and forth the value in the Entity's setter / getter. ``` php /** * @Entity */ class DDC2016User { /** @Id @Column(type="integer") @GeneratedValue */ public $id; /** * @var string * * @Column(type="string", length=128, nullable=false) */ public $username; /** Constructor * * @param DDC2016Username $username */ public function __construct(DDC2016Username $username) { $this->username = $username->__toString(); //Convert back } /** * @return DDC2016Username */ public function getUsername() { return new Username($this->username); //Convert forth } ``` This PR add a `DoctrineValueObject` interface to the project. If a Value Object implements this interface, doctrine will call the `equals` method on the VO to compare instances instead of using the `===` comparison. Developer can define the rules how the changes has been determined. ## Example of usage: ``` php /** * @Entity */ class DDC2016User { /** @Id @Column(type="integer") @GeneratedValue */ public $id; /** * @var string * * @Column(type="string", length=128, nullable=false) */ public $username; /** Constructor * * @param DDC2016Username $username */ public function __construct(DDC2016UsernameVO $username) { $this->username = $username; //Convert back gone away } /** * @return DDC2016Username */ public function getUsername() { return ($this->username instanceof DDC2016UsernameVO) ? $this->username : $this->username = new Username($this->username); //Create instance one per lifecycle } ```
admin added the pull-request label 2026-01-22 16:05:18 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#9753