This question already has answers here:
Doctrine2: Best way to handle many-to-many with extra columns in reference table
(13 answers)
Closed 7 years ago.
I have "customers", "products" and "versions" tables.Each customer, can have more than one product, and each product has more than one version. I have created many-to-many relation "customers_products" and everything works perfect.
Also I have created one-to-many relation between "products" and "versions".
QUESTION:
How to add an extra column (version_id) in "customers_products" table so I can build form where user can choose products and version to create new customer.
Once an association has data, it's no more an association.
You have to implement two ManyToOne instead of a ManyToMany.
See this great answer on this question for a full example.
You can get a lot of other examples by googling the title of your question.
Adding an extra column to the association of a many-to-many it actually changes the meaning of that relationship. In order to represent that with Doctrine, you'd need to change the association to be a one-to-many/many-to-one between the three entities.
You'll end up with three entities in your domain model, which would allow you to access to the version of a CustomerProduct entity.
You can read a bit more detailed explanation in Doctrine's docs.
Related
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?
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
I have three tables: groups, questions and answers.
groups is connected to questions in a one to many relationship. Similarly, questions has a one to many relationship with answers. Editors provide an answer to a given question after an admin submits the groups and questions.
How do I create this in Laravel 5? I think to creating a pivot table for group_question and question_answer is the right place to start, but I am not sure because I don't know how to later select questions and answers together. Should I instead use json and save all questions and answers to one record?
There are a number of ways to tackle your problem in Laravel, but the easiest way is probably by using the hasManyThrough relationship. Which can be defined in your Group class something like this:
class Group extends Model {
public function answers(){
return $this->hasManyThrough('App\Answer','App\Question');
}
}
just be sure your tables have the following foreign key fields
groups
id - integer
name - string
questions
id - integer
group_id - integer
text - string
answers
id - integer
question_id - integer
text - string
you can read up on more on this in the laravel docs under eloquent relationships.
See more from Laravel 5's docs on the hasManyThrough relationship here.
I'm trying to work out the model for a fairly simple application, but I haven't been able to find good information regarding my idea for dealing with user comments. I was thinking that I could have a Comment table, with "related ID" and "related type" fields. These fields would be the composite foreign key back to whatever other table I wanted to link to. For example, you could leave a comment about a User, or a Location, or really any other entity. Is this kind of design possible in Symfony2/Doctrine? If so, is there a good example to reference somewhere?
This is what you're looking for http://doctrine-orm.readthedocs.org/en/2.0.x/reference/inheritance-mapping.html
You'll need to use a discriminator field using Single Table Inheritance or Class Table Inheritance
I have Doctrine setup with all my tables in my database, and working properly.
I have those three entities: Users, Groups and News
There is a many-to-many relationship between Users and Groups. (I put my users in groups)
There is also a many-to-many relationship between News and Groups. (I give access to a News item to a few Groups)
Database Schema:
I want to get the News that a given User has access to.
Is there an efficient way to do this in Doctrine?
EDIT: I should add that I already had the solution nailed down with a straight SQL query before I started to use Doctrine, I want to know the Doctrine way to do this.
This thread explain the issues about many to many relationships in doctrine.
many-to-many relationship in doctrine
This blog post explain how to handle such events.
http://melikedev.com/2009/12/09/symfony-w-doctrine-saving-many-to-many-mm-relationships/