Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 move immutable fields into separate class

I need move some immutable fields into separate class, but I don't really want to use "join", because I need all data together every time.

Is any way to have some entity attributes as classes that mapped into same table?

Something like:

/**
 * @ORM\Entity
 */
class User {
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    ...

    /**
     * @var Address
     * @ORM\... ??
     */
    protected $address
}

/**
 * @ORM\ValueObject ??
 */
class Address {
    /**
     * @var string
     * @ORM\Column(type="string", name="address_zipcode", length=12)
     */
    protected $zipcode;

    /**
     * @var string
     * @ORM\Column(type="string", name="address_country_iso", length=3)
     */
    protected $countryIso;
    ...
}

And table structure would be:

CREATE TABLE User (
    `id` INT(11) NOT NULL auto_increment,
    `address_zipcode` VARCHAR(12) NOT NULL,
    `address_country_iso` VARCHAR(3) NOT NULL,
    PRIMARY KEY (`id`)
);
like image 951
ikhrustalev Avatar asked Oct 10 '13 08:10

ikhrustalev


People also ask

How to move the configuration of the student entity to another class?

Now, you can move all the configurations related to the Student entity to a separate class which derives from EntityTypeConfiguration<TEntity> . Consider the following StudentEntityConfigurations class which includes all the configurations for the Student entity.

Is it possible to configure multiple domain classes in onmodelcreating?

However, it becomes hard to maintain if you configure a large number of domain classes in the OnModelCreating . EF 6 allows you to create a separate class for each entity and place all the configurations related to an entity. Consider the following example where we configured the Student entity.

What is immutable class in Java?

Immutable class is a class which once created, it's content can not be changed. Immutable classes are good choice for HashMap key as their state cannot be changed once they are created. Objects of immuable classes are also thread safe as threads can not change the value of its fields once it is created.

What is identity map pattern in doctrine?

This is called “Identity Map” pattern, which means Doctrine keeps a map of each entity and ids that have been retrieved per PHP request and keeps returning you the same instances. In the previous example the echo prints “Hello World dude!” to the screen.


1 Answers

What you're asking about is called Value Objects.

There was an open issue in there Jira DDC-93 to add support. It's current marked as Resolved in version 2.5, which was just released in Beta.

like image 121
Daniel Avatar answered Sep 21 '22 15:09

Daniel