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

标签 php mysql symfony

我编写了一个简单的 symfony Controller ,其中有两个表,分别称为 specialtyarea 和 physician。

这是表的基本结构

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

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 从医生表中检索所有记录?

最佳答案

我相信您可以在您的医生资料库上调用 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);
}

关于php - 如何使用 Join 和 where 子句从多个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963396/

相关文章:

php - 为什么我不能使用 PDO 创建触发器?

具有动态参数的 Symfony2 KnpMenuBundle 子级

php - FOSUserBundle 和 HWIOAuthBundle 注册

php - html提交按钮不起作用

php - 日期+时间的API响应格式(iso thing?)

php - 创建一个动态的单页 Web 应用程序

mysql - cPanel MySQL 服务管理器已关闭,但/var/run/mysqld/mysqld.pid 正在运行

php - 确定 PHP 脚本的命令行与 http 执行的规范方法是什么?

mysql - 基于单个列值的 SQL 循环和插入

php - GCP SQL Postgres 权限问题 : can't run a query with postgres user with generated symfony db