2017-08-30 38 views
0

我有两个相关的表。其中一个表与第二个表有一对多的关系。这是两个表的样本图解Symfony Doctrine - 使用外键字段从相关表中检索记录

**

registration 
id | firstname | membershipfk 
1 | John  | 2 
2 | Foo  | 3 

**

这里是第二个表的说明

membership 
id | type | discount | description  
1 | Gold | xyz  | xyz description 
2 | Silver | xyz  | xyz description 

现在我目前的挑战是检索成员资格表中的成员字段使用注册表中的外键。 例如:Select Type, Discount and Description from Membership Entity where fk in registration entity is equal to 2

目前在我的控制,我做这样的尝试

public function getMembersAction() 
     { 
      $restresults = $this->getDoctrine()->getRepository('XXXBundle:Members')->findAll(); 

      $data = array(); 
      foreach ($restresults as $restresult) { 

       array_push($data, $this->serializeData($restresult)); 

      } 

每一种援助深表感谢

回答

0

用下面的代码尝试

$em->getRepository('XXXBundle:Members') 
    ->createQueryBuilder('m') 
    ->select("m.type, m.discount, m.description") 
    ->innerJoin("XXXBundle:Registration","r","WITH","m.id = r.membershipfk") //Here you will Join with the Registration Bundle 
->where("m.id = 2") 
    ->getQuery() 
    ->getResults(); 
0

你需要的,如果你的实体设置以及调用findBy的例子。

试试这个:

$results = $this->getDoctrine()->getRepository('XXXBundle:Members')->findByMember($memberId); 

但是你需要配置好你的实体第一

+0

请什么WEEL – blend

+0

遗憾的错误,它很好,你需要配置实体之间的关系。请参阅主旨文件请@blend –

+0

关系已配置 – blend

相关问题