【问题标题】:cakephp join for selectboxcakephp 加入选择框
【发布时间】:2015-11-23 16:24:24
【问题描述】:

这是cakephp 3.0

的问题

我在mysql上有三个表:

--------------
Users
--------------
id
name
lastname
phone
rol_id

--------------
Rols
--------------
id
name

--------------
Doctors
--------------
id
user_id
speciality

我想获取 rol 为 3 的医生姓名和身份证(例如)

执行此操作的查询是:

SELECT users.name, doctors.id
FROM users
LEFT JOIN doctors ON doctors.userid = users.id
WHERE users.rol_id = 3

我需要将此查询传递给 cakephp 以显示选择框 选择框的值是 user.name,键是医生.id

有人可以帮助我。

真的很赞!

harpax 评论后更新:

我在患者控制器中,在患者视图中添加,患者必须选择医生,所以我需要将它们全部显示出来。 医生 ID 在医生表中 医生姓名在用户表中 医生可以是医生或护士(我只需要医生) 这是由用户、医生、护士或患者的角色定义的

我在控制器中的查询

$doctors = $this->Patients->Doctors->find()
->select(['Users.user_name','Doctors.id'])
->where(['Users.rol_id' => '3'])
->contain('Users');

通过查看医生变量:

$this->set(compact('patient', 'users', 'countries', 'rols', 'doctors'));

我的添加模板

echo $this->Form->input('patient.doctor_id', ['class' =>'css_input_field css_color_input css_background_color_input', 'label'=>false, 'options' => $doctors]);

预期结果

在文本上显示医生姓名和医生 ID 值的选择框。

结果

Click here for view image

【问题讨论】:

    标签: mysql join cakephp-3.0


    【解决方案1】:

    在您的用户控制器中

    $doc = $this->Users->find()
        ->select(['Users.name', 'Doctors.id')
        ->where(['Users.role_id' => 3])
        ->contain(['Doctors'])
        ->first();
    

    应该生成需要的查询

    【讨论】:

    • 没错。真的谢谢。但是是不完整的。我需要将结果传递给选择框模板。 – 更新我的问题以显示它
    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 2011-08-06
    • 1970-01-01
    • 2011-08-17
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多