-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2019/07/05/value-objects-doctrine-and-symfony-forms #6
Comments
Hi @romaricdrigon , |
Hello @seb-jean, a value object, stored as a Doctrine embeddable, is usually a very good solution. |
I use ValueObject and Doctrine Embeddable ? |
Sure: /** @ORM\Embeddable */
class Coords
{
/** @ORM\Column(type="string") */
private $latitude;
/** @ORM\Column(type="string") */
private $longitude;
// Below and after I will just put the methods signature
public function __construct(string $latitude, string $longitude)
// As a bonus, you can add more methods to your value object
// For instance, converting to another format (for instance, CH1903 which is popular here)
public function getInCh1903(): array
}
/** @ORM\Entity */
class Place
{
/** @ORM\Embedded(class="Coords") */
private $coords;
// ...
public function setCoords(Coords $coords): void
public function getCoords(): Coords;
} |
Thank you. |
Having a value object is something different than how data is stored. Value object are exclusively PHP-side, and should not be concerned by how Doctrine will handle them. In your case, sure, you can use a |
Thanks @romaricdrigon |
Hello! Good article, thank you. |
Hello, thank you. |
Hello :) I feel that this covers only part of lifecycle of VO. What about optional Value object? How you implement it? ☮️ 👋 |
"As Doctrine entities" was a bit confusing to me. It's possibly a combination of me not being a native English speaker and not being so familiar with the terms used here. I have a few questions. Here's my setupI have an entity named User, and it must be allowed to own multiple addresses. I create an entity named Address with a many-to-one relation to the User.
When you said "it's tempting to..." it sounded like it would be followed by "BUT... don't do it". But in some cases, like mine, it is unavoidable to make Address an entity, right? Or maybe that's not what you were talking about here?
What does this mean, exactly? Expose the
I was thinking... maybe I can use an embeddable named Address in the Order entity to store the Address without relations, which would prevent it from changing in case the user makes changes to their addresses, right? The user would still have a collection of Address entities in relation, but the Order would store just an immutable version of the selected Address as an embeddable. I have yet to try this. I could not find any documentation regarding embeddables in the Symfony Docs. Your examples of integration with Symfony are really helpful for beginners to get the hang of it. The Doctrine documentation on embeddables only goes so far. |
Hello all, Sorry for the long answer, I have been quite busy past weeks. Now I'm catching on. About nullable value objects@timiTao you raise a good point. With embeddable, you can hack your way around, but it feels suboptimal: public function getPrice(): ?Money
{
return $this->price->getValue() ? $this->price : null;
} Though it can be quite adapted if you want to use null object pattern: public function getPrice(): MoneyInterface
{
return $this->price->getValue() ? $this->price : new NullMoney();
} About Doctrine entities@o-alquimista thank you for your feedback, I will consider rephrasing that paragraph. In your scenario, using a Doctrine entity feels right. |
Thanks. So, in my example, Address is a Doctrine entity, but it is also a Value Object. If you want to compare two Address instances, you have to leave the |
Exactly! |
I have another question about Embeddables. I couldn't find the answer anywhere else, so I have to ask here. It may help others with the same question. Can I put multiple embeddables in something like an ArrayCollection? For example, I have an
Now that I come to think more about it, I no longer believe that what I asked is possible. Embeddables map to specific columns, and these columns must exist on the database. It won't add more columns dynamically. I will probably have to create an If you have any better ideas to suggest, I appreciate it. |
Hello, In short, no, embeddables are single objects. An |
Please comment below!
Comments will automatically be published on the blog, too.
The text was updated successfully, but these errors were encountered: