Assuming I have three entities: Article, Tag & User. Articles and Tags are many-to-many related.
What I'd like to do is add User information to the many-to-many relationship - so I can tell which user added which tag to an article. Basically, the result in SQL should be a third column 'user_id' in article_tags (the many-to-many relationship table containing article_id and tag_id keys) that I can query for.
Right now I'm only aware of one solution: Creating a fourth entity 'ArticleTags' that has three relationship fields article, tag and user. Showing all tags in an article then becomes...
<?php
foreach($article->getArticleTags() AS $articleTag) {
echo $articleTag->getTag()->getName()
}
...which is much less straight-forward than $article->getTags() already, plus it requires all kinds of repository trickery for querying, sorting and so forth.
Is there a more ideal solution for this kind of thing? I'm using Doctrine 2.1.0-DEV right now. Thanks.
IMO, I think the ArticleTags entity is a good approach.
Alternatively, if the User entity has associations with Articles and Tags, you should be able to map out from the User entity which tags belong to which post for that specific user.
Related
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'm currently working on an app backend (business directory). Main "actor" is an "Entry", which will have:
- main category
- subcategory
- tags (instead of unlimited sub-levels of division)
I'm pretty new to OOP but I still want to use it here. The database is MySql and I'll be using PDO.
In an attempt to figure out what database table structure should I use in order to support the above classification of entries, I was thinking about a solution that Wordpress uses - establish relationship between an entry and cats/subcats/tags through several tables (terms, taxonomies, relationships). What keeps me from this solution at the moment is the fact that each relationship of any kind is represented by a row in the relationships table. Given 50,000 entries I would have, attaching to a particular entry: main cat, subcat and up to 15 tags might slow down the app (or I am wrong)?
I then learned a bit about Table Data Gateway which seemed an excellent solution because I liked the idea of having one table per a class but then I read there is virtually no way of successful combating the impedence missmatch between the OOP and relational-mapping.
Are there any other approaches that you may see fit for this situation? I think I will be going with:
tblentry
tblcategory
tblsubcategory
tbltag
structure. Relationships would be based on the parent IDs but I+'m wondering is that enough? Can I be using foreign key and cascade delete options here (that is something I am not too familiar with and it seems to me as a more intuitive way of having relationships between the elements in tables)?
having a table where you store the relationship between your table is a good idea, and through indexes and careful thinking you can achieve very fast results.
since each entry must represent a different kind of link between two entities (subcategory to main entry, tag to subcategory) you need at least (and at the very most) three fields:
id1 (or the unique id of the first entity)
linkid (linking to a fourth table where each link is described)
id2 (or the unique id of the second entity)
those three fields can and should be indexed.
now the fourth table to achieve this kind of many-to-many relationship will describe the nature of the link. since many different type of relationship will exist in the table, you can't keep what the type is (child of, tag of, parent of) in the same table.
that fourth table (reference) could look like this:
id nature table1 table2
1 parent of entry tags
2 tag of tags entry
the table 1 field tells you which table the first id refers to, likewise with table2
the id is the number between the two fields in your relationship table. only the id field should be indexed. the nature field is more for the human reader then for joining tables or organizing data
Tables:
tbl_user(id, name)
tbl_group(id, name);
tbl_user_group(user_id, group_id);
If there were all three classes generated User,Group,UserGroup. How should i setup the relations in User or Group so that they are mapped through the user_group table?
I'm using Doctrine 1.2
It's up to you to define their associations, purely because only you will know how you want your application to work. Can a user have more than one group? Only one? If a group is deleted, will all associated users be deleted? These are the questions you need to ask yourself.
If you follow the example on Doctrine's documentation for Many-to-Many relationships (which coincidentally covers exactly what you need), you'll get there.
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/