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`)
);
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With