i ran into the following problem and after hours of searching on the web i don't find any solution.
I want to have a "3-Entity Relationship" between the Entity Project, User and Role.
A Project have many users, and a User can be member of many projects. But in every Relationship between Project <--> User the User can have a different Role.
How can i solve this with Doctrine2?
Many thanks in advance!
EDIT
An little codeexample would be very nice :)
You should have an N:M association between Project and User. Then every instance of this association has the role property (either as an integer for a fixed list, or as an association to a Role entity). Doctrine unfortunately does not explicitly support properties on associations, so in these cases you should use connector entities: an entity that is in ManyToOne connection to both Project and User. This entity then can hold the role value(s)/association(s), but you have to manage (dis)connecting through these objects.
Related
In cycle orm docs some mappings have such thing as role. Text states that is it entity role (well, not informative at all). What does that mean and why do we need this roles stuff? Doctrine ORM for instance doesn't have such thing.
You can treat role as alias to class name in Doctine. Unlike Doctrine Cycle ORM can map any type of objects, including StdClass or custom models. In order to identify mapping between same class instances it uses roles.
I have an entity called Document. I'd like to make this entity related (many to one) to few other entities (let's say User, Invoice and Transaction). In Laravel I could easily achieve that by using polymorphic relations.
Is there any (easy) way to accomplish the same with Doctrine?
This might be what you're looking for http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html
I currently have a User Doctrine entity and model in my Components. I have a User Bundle that does the basics of working with users, CRUD, etc.
I am developing a resource allocation bundle and I want to extend my User entity to add extra associations without changing the original user. Then the ResourceAllocation Bundle will be completely separate from the User Bundle.
I have setup a mapped superclass of BaseUser, which both User and SkilledUser (the one from the Resource Allocation Bundle). This however, tries to create a table for both User and SkilledUser which is undesired.
The user and skilled user could be the same user, so, Single Table Inheritance is not going to work.
Effectively, the end result should be one table with the users in.
We are working in YAML if answers could keep to this method, that would be great.
"Effectively, the end result should be one table with the users in."
The only way to achieve this is with STI (Single Table Inheritance).
"The user and skilled user could be the same user, so, Single Table Inheritance is not going to work."
I don't see how this matters... you can still create an association between User and SkilledUser if that is what you mean here.
See reference: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html
I have two entities related with many to one relation but each one belongs to a bundle.
When forcing the update in the database I get the following error
the target -entity cannot xxxx\Bundle1\Entity be found in xxxx\bundle2\entity
Any ideas please??
Just create an entity extending xxxx\Bundle1\Entity in your xxxx\bundle2 and make the relationship between two xxxx\bundle2 entities.
I am in the process of upgrading from Doctrine 1.1.4 to Doctrine 2.0.6 in my Zend application.
Currently, I am working on mapping the associations between entities. In Doctrine 2's Documentation it says 'relationships maybe bidirectional or unidirectional. I am confused as to what these terms mean within the given context.
How do I determine if a relationship is unidirectional or bidirectional?
Appreciate the help.
A relationship is bidirectional if both entities contain a reference to the other.
If you omit one of those references, it's unidirectional.
Consider a typical "posts" and "tags" schema. Typically, you'd implement a bidirectional association:
<?php
class Post {
// ...
/**
* #ManyToMany(targetEntity="Tag",inversedBy="posts")
*/
protected $tags;
// ...
}
class Tag {
// ...
/**
* #ManyToMany(targetEntity="Post",mappedBy="tags")
*/
protected $posts
// ...
}
Now, imagine you decided you never (or rarely) needed to answer questions like "Which posts have Tag 'foo'?". You could omit the $posts association in your Tag entity, converting it to a unidirectional association, and take some load off of the ORM.
You could still answer that kind of question, but you'd have to write code to do it.
In fact, it's probably a good way to go in Posts/Tags scenario, as you wouldn't typically be adding/removing Posts from Tags. Typically, you'd add/remove tags from posts only. You'd only ever go from Tags to Posts when looking for "all posts with tag 'x'", which could be trivially implemented in a service class of some sort.
Same as timdev`s answer,
Unidirectional & BiDirectional is just ORM Concepts, these have nothing to do with database,
Suppose you have a OneToMany relation -
user has blogs
So you can add this to your User Entity as OneToMany Property
but obviously there exisits ManyToOne Relation
Blogs Has User
so it is optional for you to create a ManyToOne relation in your Blog Entity, If you want to access user from blog entity then add this property if you dont want then dont add,its not necessary. in both Cases (you are adding bidirectional reference or not) ORM will maintain same database structure (blog table will have user_id column) .