I have this scenario
I have four classes
User
profiles
Activity
Workbook
user can have many profiles based on per year. Every year diff profile
User profile will have many to many with Activities
so there will be profile_activities table with profile_id and activity_id
Now User will do 1 workbook per activity per profile
so i am confused how to map in database
I mean for profile table , i can have
class profile
@many to many
protected $activities
many to one
protected $user
But in class workbook how to define foreign key which belongs to activity and profile relationship table
For every activity child has to complete workbook. How should i define that
@[one to one [PK of activity_profile table]
protected $workbook
Symfony provides you different methods for do this operation.
One of those methods is annotation. With annotation you can write directly into php classes this relationships and more (like db column’s type, specify if an attribute is mandatory and so on …)
So, let’s took this example (because i dind’t understand the relationships of your entities)
Consider two entities: User and Groups.
One user can belong to n Groups and a Groups can have m Users. This mean that we have to “break up” m-n cardinality into an
m-1-nrelationship.In symfony (with doctrine; i don’t know if with mongodb and similar is the same) you haven’t to create this “intermediate table” as a php class. All you have to do is, with annotation into php classes involves, specify what tables are related and in what way.
Let’s see how!
}
As you can see, this is a part of a php class mapped into db table with doctrine (ORM).
This class han an attribute
$groupsthat tells (take a look to annotations) that is a many-to-many relationship between this class and another class (identified bytargetEntity) andinversedBytells what attribute (db column; attribute if you talk about class) is involved into the relationship (external key).This entity is group entity and have the “other side” of relationship:
$userAs you can see, there is
@ORM\ManyToMany(targetEntity="User", mappedBy="groups")that indicates that realtionship is with classUserand field intoUserclass isgroups.Now you can run doctrine command for entity generation onto db php app/console
doctrine:generate:entities AcmewhereAcmeis bundle’s name and the trick is done.Some words on
mappedByandinversedBy:mappedBykeyword and it’s value is the name of owning side classinversedBykeyword and it’s value is the name of the inversed side class