Symfony + Doctrine与链接表的多对多关系

时间:2010-10-13 06:00:28

标签: php symfony1 doctrine many-to-many

情况:

我有3个表:学生,地址,StudentAddressLink如下注意*不是完整的yaml文件,但你明白了

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  

从Student对象我想获得相关的“地址街” 目前我必须这样做:

foreach($student->StudentAddressLink as $address)
{
    echo $address->getStreet();
}

这很有效......但我有办法做一些使链接表透明的东西,像这样神奇的东西:

foreach($student->Addresss as $address)
{
  echo $address->getStreet();
}

任何方向都会很棒!

1 个答案:

答案 0 :(得分:4)

如果你想要多对多关系,你需要使用代码here之类的东西(对于symfony的过时版本,但仍然正确)。

这涉及将refClass设置为StudentAddressLink,允许您想要的透明关系。然后,您将能够使用$student->Address[0]->getStreet。那里的文档可以解释得比我更好!


编辑我认为您的架构需要看起来像这样:

Student:  
  columns:   
    name: string
  relations:
    Address:
      refClass: StudentAddressLink
      local: student_id
      foreign: address_id
Address:  
  columns:  
    street: ~
StudentAddressLink:  
  columns:  
    student_id: ~  
    address_id: ~