Symfony + Doctrine Many to Many relations with Linking Tables
Situation:
I have 3 tables: Student, Address, StudentAddressLink As follows Note* not EXACT yaml file but you get the idea
Student:
column:
id: blah blah
name: blah blah
Address:
column:
id: ~
street: ~
StudentAddressLink:
column:
id:~
student_id: ~
address_id: ~
relations:
Student:
local: student_id
foreign: id
Address:
local: address_id
foreign: id
From the Student object I want to get the related "Address Street" currently I have to do this:
foreach($student->StudentAddressLink as $address)
{
echo $address->getStreet();
}
This works...but I though there was a way to do something that makes the link table transparent, something magical like this:
foreach($student->Addresss 开发者_JAVA技巧as $address)
{
echo $address->getStreet();
}
Any direction would be great!
If you want many-to-many relations, you need to use something like the code here (for an out of date version of symfony but still correct).
This involves setting the refClass
as StudentAddressLink
, allowing the transparent relationship that you want. You will then be able to use $student->Address[0]->getStreet
, for example. The documentation there can explain better than I can!
Edit I think your schema needs to look something like this:
Student:
columns:
name: string
relations:
Address:
refClass: StudentAddressLink
local: student_id
foreign: address_id
Address:
columns:
street: ~
StudentAddressLink:
columns:
student_id: ~
address_id: ~
精彩评论