如何使用Join和where子句从多个表中获取数据

时间:2017-05-14 11:34:48

标签: php mysql symfony

我写了一个简单的symfony控制器,我有两个叫做specialarea和医生的表。

这是表格的基本结构

specialtyarea
-----------------
id | name       |
-----------------
1  | dentist    |
2  | physician  |

医生表与specialtyarea相关,如下所示:

Physician
--------------------
id | name | specfk |
--------------------
1  | John  | 1     |
2  | Doe   | 2     |
3  | Ann   | 2     |

我正在尝试取得专业领域为2的所有医生

这是我的代码片段

public function getAllPhysicianAction($specId)
{
     $id = $this->getDoctrine()->getRepository('Bundle:SpecArea')
        ->find($specId); //assuming that this returns all the id from the specialty table

    $allPhysician = $id->getPhysician()->...   //kind of lost here    
}

如何使用专业版ID从医师表中检索所有记录?

2 个答案:

答案 0 :(得分:0)

使用"医生的findBy()方法"存储库:

(您没有发布您的实际实体,因此我猜测了字段/实体名称。)

$spec = $this->getDoctrine()->getRepository('Bundle:SpecArea')->find($specId); 

$physicians = $this->getDoctrine()->getRepository('Bundle:Physician')->findBy(['spec' => $spec]);

您也可以直接使用$specId ID,而不是从数据库中获取整个实体:

    $physicians = $this->getDoctrine()->getRepository('Bundle:Physician')->findBy(['spec' => $specId]);

请参阅:Working with Objects - Querying - By Simple Conditions

答案 1 :(得分:0)

我相信你可以在你的医生储存库中调用findBy。

/**
 * @Route("/physicians/{specId}", name="getAllPhysician")
 */
public function getAllPhysicianAction($specId)
{
    $allPhysician = $this->getDoctrine()
    //call the repository of your physician table
    ->getRepository('Bundle:physician')
    ->findBy(['specfk' => $specId]);

    var_dump($allPhysician);
}
相关问题