![](/img/trans.png)
[英]Join 2 tables in a Single Eloquent Laravel using multiple where clause
[英]How to fetch data from multiple tables using Join and where clause
我寫了一個簡單的symfony控制器,我有兩個表稱為specialarea和doctor。
這是表格的基本結構
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從醫師表中檢索所有記錄?
使用“physician”存儲庫的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]);
請參閱: 使用對象 - 查詢 - 按簡單條件
我相信你可以在你的醫生庫中調用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.