I am using Symfony and Doctrine
This might be a simple question but i cannot seem to find the answer to it...
How can i reference the join-table of a many-to-many association in a querybuilder ?
I have 2 entities: Article and Tag with a many-to-many bidirectional relation
this creates the extra join-table :
article <-> article_tag <-> tag
I can reference the article table by selecting it from the Article Entity :
$this->_em->createQueryBuilder();
->select('a')
->from('Acme\DemoBundle\Entity\Article','a')
And i can reference the tag table by selecting it from the Tag Entity :
$this->_em->createQueryBuilder();
->select('t')
->from('Acme\DemoBundle\Entity\Tag','t')
Now how can i select things from the join-table ? There is no entity for it...
You cant interact with this table. Doctrine will handle it, and like Roger Guasch said on the comments, you have to create accessors to the corresponding entities.
If you need to interact directly with this entity, you must define it by yourself, threating the m-n table as an entity. Then you can make reference to it.
Hope it helps!
Related
Doctrine passes a $targetEntity and $targetTableAlias to the filter constraint like so:
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{ ...
It appears that all entities pass through the filter when a query is being built. Is there any way to determine if $targetEntity is a foreign key or not? I'd like to write a filter that works on the "primary" entity (what Doctrine calls the "base" table) but not its foreign keys.
Your question is not very clear and I am not sure if this will answer your question, but in the Doctrine 2 documentation chapter 30. Filters you can read the following:
In the case of joined or single table inheritance, you always get passed the ClassMetadata of the inheritance root. This is necessary to avoid edge cases that would break the SQL when applying the filters.
So I would say the $targetEntity is always the root entity (so never a foreign key).
If this doesn't answer your question I would like to ask you to be a bit more specific on what exactly you want to do or what you want to find out.
I have a table "Category" and another "Subcategory". Then I have "Products" table.
Some of these products will be related directly to the "Subcategroy" table, but some of them will be related directly to "Category". What should be the tables structure in this case?
Use Association Mapping
This chapter explains mapping associations between objects.
Instead of working with foreign keys in your code, you will always work with references to objects instead and Doctrine will convert those references to foreign keys internally.
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html
Using Doctrine 2 with Zend Framework 2. Been thinking of how I am meant to persist an entity with a field having a Many-To-One association with another entity that already exists in the database. Would I have to fetch the inverse side from the database first and then adding it to the owning Entity before persisting to the database as in the code below.
//$data = $_POST;
$book = new Book();
$author = $em->getRepository('Application\Entity\Book')->find($data['author_id']);
$book->setTitle($data['title'])
->setISBN($data['title'])
->setAbstract($data['abstract'])
->setYear($data['year'])
->setAuthor($author);
$em->persist($book);
$em->flush();
Normally, without using doctrine, all I have to do is update the author_id field of the Book entity above and persist to the Database. But now I have to make a trip to the Database to fetch the author details to create the entity and thus the association and then persist.
Is this the way it should be done or there is another way that doesnt involve fetching the author's details.
As you can read here
The method EntityManager#getReference($entityName, $identifier) lets
you obtain a reference to an entity for which the identifier is known,
without loading that entity from the database. This is useful, for
example, as a performance enhancement, when you want to establish an
association to an entity for which you have the identifier.
You could simply do this:
$book = new Book();
$book->setAuthor( $em->getReference('Application\Entity\Author',$data['author_id']));
I have problem which is easy to solve with pure SQL but I need to solve it with Doctrine in YAML.
I have well known db tables users and friends. Where users table has primary key user_id and friends has friend_id and friend_with_id which uses user_id from table user. My problem starts when I need to add there one more column mood. With SQL I add to table friends onemore column named mood and it's done with doctrine I can't find any solution.
For better understanding I add db scheme:
I dont see any self-reference relationship here. Both friend_id and friend_with_id points to users.user_id. What I see is, two one-to-many relationships from the user side OR two many-to-one relationships from the friends side.
So, there will be two ArrayCollection objects (ManyToOne) in the Friend entity corresponding to friend_id and friends_with_id. Similarly two ArrayCollections (OneToMany) on the User entity, namely, myFriends and friendsWith.
I have a "Product" entity with many "Video" entities, and I only need a unidirectional #OneToMany with foreign key (one product, many videos). My Product-side "key" is not primary or unique, which is why I need it to be unidirectional (eg, "select * from videos where product_family = 2143")
I'm using Doctrine 2.1
Is there yet a way to do uni-directional #OneToMany with only a foreign-key in Doctrine 2.1? If not, soon?
UPDATE: I found a relevant quote from Roman Borschel on May 2010:
"this would need quite some special-case handling in many places. In the light that there are 2 reasonably good alternatives (mapping through a jointable or simply making the association bidirectional) we do not consider this something that really needs to be done."
Has this opinion by the Doctrine2 team changed?
OneToMany by design has the related ID on the "Many" side of the relationship. So to make the child table relate to the parent without an additional field in a join table is not possible.