Symfony: how to relation a Entity field to several Entities - php

My database diagram corresponds to:
Each table has his own Symfony Entity class. My app view shows:
I need to associate a Discount Entity to multiple entities: Sneaker, Tshirt, Trouser or even more entities.
First solution:
Create N:N tables between discounts and the others. The problem is that I could need create new tables to apply discount and then I would have to create more N:N tables. So I discard this solution.
Second solution:
Create a generic table with to_entity and to_entity_id fields that let me map discount to to_entity entity destination (Sneaker, Tshirt, Trouser or other):
How can I implement second solution in Symfony ? Or is there another solution possible ?
Thanks.

Polymorphism with Doctrine is not easy at all.
Try to solve it through mapped superclasses and/or inheritance. Your first solution could be simplified by using single-table inheritance. You should change the discriminator map for every new "discountable" entity.
Investigate on Dynamic mapping through loadClassMetadata event. You can create OneToMany relationships for every entity implementing a custom interface (i.e. DiscountableInterface).

Related

Archive table implementation with Symfony 4 and Doctrine 2

I read about some arguments on Soft delete vs. Archive table which led me to this question.
I thought I could implement an entity Product which is mapped to two identical tables Product and ArchivedProduct, but there is a known limitation of Doctrine 2
Mapping many tables to one entity
It is not possible to map several equally looking tables onto one entity. For example, if you have a production and an archive table of a certain business concept then you cannot have both tables map to the same entity.
So I thought I could use two entities, Product and ArchivedProduct, that are mapped to the same table, but it led to another problem that the One-to-Many reference $products in the entity Category should be mapped to two different entities.
Should I use Inheritance Mapping on Product and ArchivedProduct? What's the best practice of Archive table with Symfony 4 and Doctrine 2?

Symfony 2.8 : one to many relationship

I have two tables, Event and Product, one Event has multiple products and one product can be in multiple Events. Here I think it's a OneToMany Unidirectional relationship. In the form of Event creation I want to have the list of available products in the database and add some products to the event (collection of products).
What's the best way to do that?
Implement the onetomany or
Go in the way to implement ManyToMany and create a third table EventProducts
Can anyone help me to choose the best way and how to implement it?
1)Implement the onetomany:
In this case you can get the event products list : $event->getProducts
But when you need to access to the event from the product entity via $product->getEvent() !!!!!! this is impossible because the relation is unidirectional
2) Go in the way to implement ManyToMany and create a third table EventProducts :
The solution is this one and create the third entity EventProduct
Hope this can helps
For me logical answer is to use Many-To-Many relationship and crate bridge table, and depending on your needs you can use bidirectional or unidirectional relationship
I think go and generate EventProduct is a better option so as Event will make oneTomany relation with EventProduct and Product also will make a relation with EventProduct.So EventPeoduct join Event and Product
Personally I prefer to add a third entity instead of doing manytomany it's more maintainable especially when one day you will need to add another fields like (order, creation date ,etc)
it's better to use ManyToMany with ORM\JoinTable(name="table_name")
class Product {
/**
* #ORM\ManyToMany(targetEntity="Event")
* #ORM\JoinTable(name="products_events")
*/
private $events;
.....

Symfony Tow relations berween tow entities

I need to create a link between two entities with the following attributes:
First entity (MSG):
Author (one person)
Recipients (several ersons)
Nota : The author can be present on the recipients list.
Second entity (Person)
RecipientOf (list of MSG)
AuthorOf (list of MSG)
But i don't know how to define the relationship between the two entities.
Can you help me please ?
For your information i'm using SYMFONY2 (Doctrine, PHP, anotations)
First of all, you need to determine the relation's type between the two entities :
One-to-one
Many-to-one
Many-to-many
After that, you must map your database tables to specific PHP classes or entities.
I recommend to you the
official documentation.
Otherwise try to look in this tutorial.

How to tell doctrine:mapping:import to create own entity instead of junction-table

I have three tables:
banners (id, url, img)
banner_on_position (banner, position, loading)
banner_positions (id, name)
When I run the doctrine:mapping:import, doctrine creates only two tables. Banners and BannerPositions. Auto-generation doesn't create an own entity for the banner_on_position table.
Is there a way to access the loading attribute with querybuilder?
OR
How to tell doctrine to create own entity?
Until now I had no problems with auto generation of entities and it was very comfortable. I hope I don't have to create to code manually.
You have to create code manually in that case. If your many_to_many table have some extra properties then you need to use that trick: Both side need to use oneToMany relation for banner_on_position and banner_on_position need to have manyToOne to both tables.
Check how we did it in similar case (ContainerWidget have link to container, widget and custom parameter - position). https://github.com/superdesk/web-publisher/blob/master/src/SWP/Bundle/TemplateEngineBundle/Resources/config/doctrine/ContainerWidget.orm.yml
Here is also really good question and answers on stackoverflow: Doctrine2: Best way to handle many-to-many with extra columns in reference table

Zend Framework 2, Doctrine2 ORM - Multiple entity relation

I'm using the most recent versions of ZF2 and doctrine2 orm in a project.
What I want to achieve, is a generic class "Comment" that has a relation to another entity. This entity could be from various classes "Post", "Image" aso., depending on what the comment should be related to.
The idea was to have one table for all comments and a column that defines the type of related entity, just like using the discriminator mapping.
Problem with the discriminator is that I have to create different classes using extends, for every use case. This does not seem efficient as I will have to create quite a lot of subclasses to reflect all relations needed.
I would like to use just the base class of Comment and have one or two column(s) defining the relation like "Post:25" (target class and id in one column) or "Post" | "25" (target class an id in separate columns).
Is this possible in doctrine or is there a besser way of dealing with this?

Categories