php - yii2: Yii\db\Query 模型函数

标签 php mysql yii2

我正在尝试在我的一个 model.php 中创建一个查询

查询就像

public function getBedCategory(){
     $query = (new \yii\db\Query())
    ->select('room_category')
    ->from('room_charges') 
    ->innerJoin('patient_detail', 
   'patient_detail.bed_type = room_charges.room_name')
   ->where(['room_charges.room_name'=> 'patient_detail.bed_type',
   'patient_detail.id'=> $this->id]);
   $command = $query->createCommand();
   $rows = $command->queryOne();
   //var_dump($command);exit;
   return $rows;
   }

当为 $command 做一个 var_dump 时,我得到这样的 sql 查询:

SELECT `room_category` FROM `room_charges` 
INNER JOIN `patient_detail` ON patient_detail.bed_type = room_charges.room_name 
WHERE (`room_charges`.`room_name`=:qp0) AND (`patient_detail`.`id`=:qp1)

在 $rows 的 var_dump 上,我得到了 boolean:false

我在这里做错了什么以及为什么我得到这个 :qp0 和 :qp1

感谢任何建议。

最佳答案

正如 Tahir 正确指出的那样,:qp0:qp1 是参数的占位符。这些在执行查询时被静态值替换。您的问题是 patient_detail.bed_type 不应被参数化。因此,您的代码应为:

...
->where(['room_charges.room_name = patient_detail.bed_type',
'patient_detail.id'=> $this->id]);

关于where()的更多信息,可以查看the API page .

关于php - yii2: Yii\db\Query 模型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27550331/

相关文章:

php - 需要 mysql 逗号分隔值中最流行的值?

php - UPDATE 反射(reflect)在 DB 但不是 SELECT 查询

twitter-bootstrap - 带有 Bootstrap 3 的 Yii2 复选框组

activerecord - 预加载相关模型(嵌套集)

nginx - 如何将 yii2 与 ispconfig3 集成

php - 如何使用另一个表从一个表中查找值

javascript - 使用 php - safari 网站验证表单

java - com.mysql.cj.jdbc.exceptions.CommunicationsException : Communications link failure when trying to build spring boot docker image 错误

mysql - perl lwp useragent和mysql数据下载和更新字符集麻烦

php - 完整性检查 : Will this SQL query join how I want?