I have entities "Article" and "Product". Now I want to add comments to these 2 entities. Should I create 2 different entities "ArticleComment" and "ProductComment" with the same properties, and build a ManyToOne relation to their respective entity, or create a single "Comment" entity and find a way to build a relation to both "Article" and "Product" entities. Considering solution #2, how could I do that ?
Considering solution #2, how could I do that ?
One way would be to use Single Table Inheritance
Single Table Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.
This means that you can easily create two separate entities ArticleComment and ProductComment both extending Comment. Then you use the advantages DiscriminatorMap column provides.
Your Comment entity could hold a relation called parent for instance that would refer to either your Article or Product entities. By creating new instance of ArticleComment or ProductComment your discriminator map field would be automatically populated depending on which type you're using.
This would also give you advantages with using DQL to query related comments by their type. Example from documentation:
$query = $em->createQuery('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee');
And more. You can read that chapter here. Of course this is just a sample and you can use completely different approach.
Related
Let's say I have a Setting entity with some fields like IntValue, dateValue, stringValue and some linked entities, like countries (ManyToMany to entity Country), languages (ManyToMany to Language) etc.
Settings are created by users and assigned to specific objects (not important here, but I wanted to clarify).
Now I suddenly need to have UserDefaultSetting, which will be the same, but with additional user field (ManyToOne to User entity).
I tried to extend existing Setting entity class with one more field added. The problem is, as I looked at the schema update SQL, it created new table for the new entity, but without all the tables needed to ORM connections (mostly ManyToMany). Just one table with "scalar" fields.
So previously I've had setting table with int_value, date_value etc. but also setting_country and setting_language tables, linking ManyToMany relations. After creating child entity, Doctrine created only user_default_setting table with int_value, date_value etc. and additionally user_id column, but I can't see any relation/link tables.
I know I should've been do it with abstract base entity class, but at the time I started, I didn't know that and now part of the project is on production (don't look at me like that, I blame the client) and I don't want to change that "base" class now. Can I inherit everything from non-abstract entity class in a way it will work?
UPDATE: everything explained. See Cerad's comment. Thanks!
I have different entities like:
Element, Status, Action, Contributor ...
And I need to create an entity, let say Synonym, which can be related to one of these previous entities
My entity Synonym, can be a synonym of the entity Element or Status or Action ...
I try to use a MappedSuperclass but I'm really lost.
I think this kind of relation is common and must be easy to create with doctrine but I can't find the good way.
Suppose i have an Bundle to manage anything related to advertisements.
This bundle contains an Entity Advertisement. this has an field for relation purposes: lets say relation field
Suppose i have an Entity Company and an Entity Events in different Bundles
(In companies there are companies stored and in events there are events stored.)
Case:
The entities have a relation to multiple Advertisements.
A single Advertisement has a relation to only one of the entities.
From the perspective advertisement:
I want to be able to select one of the entities (entity.id) to view or update the reference (like dropdown)
From the perspective of an event or a company:
I want to be able to select/add/delete multiple advertisements (like the symfony collection form type)
all this preferred without the use of foreign-keys.
the entities are like "modules" so there can be more than just these entities.
I think you just have to use OneToOne (For Advertisement) and OneToMany (For Company and Events) relations.
It's easy to use, read this doc : http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html
Using Docrine2 entities, I have a "list" entity, with a manytomany relationship with "item".
I need to manipulate and save the order of the items in the list. I can't figure out how to accomplish this using Doctrine2. What I want is a joiner table that looks something like:
list_item
=========
list_id
item_id
sort_order
All I can find is this outdated to-do item: http://www.doctrine-project.org/jira/browse/DDC-213
Can I accomplish this using Doctrine? Or is there some other way I should be going about this?
Thanks.
Here is an exerpt from this docs section, that answers your question:
Real many-to-many associations are less common. [...] Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.
In the business logic we map, Tables to Objects and fields of this table to properties.
What to do in one-to many relationship? Just an example: I have Table, Products and Categories.
I need to drag all products and instead of Category_ID (Products table) need to display actual Category name wich is stored in Category table.
What is propal way of doing it? In similar situations?
You can have a property on the Products Entity that is of type Category Entity. Check this link out from the EntitySpaces (an ORM for .Net) documentation for ideas:
link text
Also, you may want to look into using an ORM.
Either use an ORM like LINQ to SQL or ADO.NET Entity Framework or strongly-typed datasets. But if you want to custom code your business layer, your Category class could have a Products collection property which contains those products, loaded from the DB, which you could infer the name.
HTH.