Thinking symfony relationship for set - symfony

I have 2 entity product (sigle product: id, name, category and set: id, name). Set is compose with one or more product. I add relation ManyToMany between product and set. Symfony add entity set_product.
I want in product add « product preferential » (of set_product) in case the product is a set.
I don’t know if thinking is correct ? What is the good way ? Thank in advance.

First, if only set is composed by one or more product, you should use OneToMany (set can have one or more product).
I think your thinking with product preferential should work anyway.

Related

Symfony3 Doctrine select with many-to-many

I have 3 entities: User, Category (of posts) and Group. Category has allowedGroups field with many-to-many relation with groups. User also has groups. I'm looking for the simplest way show to current user only these categories which has at least one of the user's groups in allowedGroups. I tried to fetch all categories and "manualy" filter them in controller, but it is not perfect way I think. How can I do that?
You could add a function in the user entity which would be getCategories, which would do a for each loop on the $user->getGroups() and array_merge all the categories of each group then use array unique to remove duplicates.

Comment entity related to Article or Product

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.

Adding extra field in many_to_many symfony2 relationship

Im looking for a way to handle many_to many relationship in symfony2 with extra field, I have 3 entities : Skill/Training/Skill-Training .Every training should affect some skills and increment the level of those skills.
So Skill-Training table is like this (skill_id,training_id,target_level)
In the Form of the Training i want to list all skills and the target level with each skill, so i can choose a skill and add the target level (integer from 1 to 5) that will be affected with this Training. I want to do this in the Add Form of the Training entity.
I use Sonata Admin Bundle To generate my Admin Classes ,i can render the list of skills with the checkbox easly (using type collection , and the option expanded true , multiple true ) but im wondering how to render the extra field target_level in front of each skill in the form of Training ??
I will be very greatful if you help me. Im looking for the solution for 3 days without any progress ... thanks
Here are the similar questions:
Doctrine2: Best way to handle many-to-many with extra columns in reference table
Doctrine 2 and Many-to-many link table with an extra field
TL;DR: Unfortunately, relationships can't have any extra fields. You need to create one more entity, which would have two relations (with Skill and Training entities) and property for target level.

configure entitydatasource with entity set that includes multiple tables

I have an entity data model with two entities. One entity called Posts contains blog posts and another entity called Comment contains comments. I set up the association as a one to many relationship and the primary keys are both postid in both Posts and Comment.
However, when I try to bind this to an entitydatasource control it only lets me select either the Posts or the Comment entity set not both. What I want to do is to be able to bind the post with its comments to the data source, but I can't seem to figure out a way to do this.
Any help would be appreciated!

How to remove the compound PK from a Symfony2 ManyToMany

I need to allow multiple Products to be present in a Cart. I do not want to increase a quantity column, I actually want the same Product entity in the Cart twice. I want to reuse the Product entities and not create a CartProduct intermediary too.
Cart ManyToMany Product
However, the table is created by doctrine:schema:update with a compound primary key of cart_id + product_id. This prevents me adding the same Product twice.
How do I solve this?
This is not the only use-case I have where I need a ManyToMany to support duplicate entries. Is this just not possible with Symfony2/Doctrine?
It's not so much a limitation of doctrine as it is of relational databases. Every row needs to have a unique primary key which, by default in Doctrine 2, would be product_id,cart_id.
The only way around it is to make yourself an explicit CartProduct entity and add at least one more column. Not that hard to do. Just establish OneToMany relations to it from Cart and Product.

Resources