Reputation: 5388
I am using Doctrine 2. Let's say we have two entities: User and Bug. Is it possible to have a bidirectional association with one column on the inversed side (User) and multiple columns on the owning side (Bug)?
If I define columns in the Bug entity like this:
/** @Entity */
class Bug {
/** @ManyToOne(targetEntity="User", inversedBy="associated_bugs") */
protected $reported_by;
/** @ManyToOne(targetEntity="User", inversedBy="associated_bugs) */
protected $assigned_to;
}
then I don't know what to write in the User entity...
/** @Entity */
class User {
/**
* @OneToMany(targetEntity="Bug", mappedBy="???")
* @var Bug[]
**/
protected $associated_bugs;
}
Upvotes: 0
Views: 71
Reputation: 2600
No this is something you can not do with mapping. Lets say you would set a list of bugs to User::associated_bugs
. How would you expect it to store that when calling persist?
You should map the 2 types of bugs separately and next combine them in a method.
/** @Entity */
class User {
/**
* @OneToMany(targetEntity="Bug", mappedBy="reported_by")
* @var Bug[]
**/
protected $reported_bugs;
/**
* @OneToMany(targetEntity="Bug", mappedBy="assigned_to")
* @var Bug[]
**/
protected $assigned_bugs;
protected function getAssociatedBugs()
{
return array_merge($this->reported_bugs, $this->assigned_bugs);
}
}
Upvotes: 1
Reputation: 5158
Something like this:
/** @Entity */
class User {
/**
* @OneToMany(targetEntity="Bug", mappedBy="assigned_to")
**/
protected $associated_bugs;
/**
* @OneToMany(targetEntity="Bug", mappedBy="reported_by")
**/
protected $reported_bugs;
}
In Bug entity, you have to add these annotations:
For assigned bugs:
@JoinColumn(name="assignee_id", referencedColumnName="id", onDelete="cascade")
and
@JoinColumn(name="reporter_id", referencedColumnName="id", onDelete="cascade")
for reported bugs
This should do the job
Upvotes: 0