douyi8315 2014-07-16 08:35
浏览 172
已采纳

Yii查询生成器结果(带有连接和子查询的纯Sql)

I have this syntax on My Controller

$checkbook = Yii::app()->db->createCommand("SELECT count(b.bookingid) as booking FROM (SELECT :fname AS firstname, :mname AS middlename, :lname AS lastname, :dob AS dob) n 
                                                    LEFT JOIN passenger p ON (p.firstname=n.firstname AND p.middlename=n.middlename AND p.lastname=n.lastname AND p.dob=n.dob)
                                                    LEFT JOIN pax USING (passengerid)
                                                    LEFT JOIN booking b USING (bookingid)
                                                    LEFT JOIN journey USING (bookingid)
                                                    LEFT JOIN flight f USING (flightid)
                                                    WHERE f.origin = :origin AND f.destination = :destination AND f.departure BETWEEN :datestart AND :dateend");
        $checkbook->bindValue(":fname", $fname);
        $checkbook->bindValue(":mname", $mname);
        $checkbook->bindValue(":lname", $lname);
        $checkbook->bindValue(":dob", $dob);
        $checkbook->bindValue(":origin", $origin);
        $checkbook->bindValue(":destination", $destination);
        $checkbook->bindValue(":datestart", $datestart);
        $checkbook->bindValue(":dateend", $dateend);
        $checkbook->queryRow();

        //If Count Result 1, then Status True. If Count Result more than 1,then false. 
        if ($checkbook['booking'] == 1) {
            $status = true;
        } else {
            $this->actionDoubleBook();
            $status = false;
            return $status;
        }

But I got this Error.

Fatal error: Cannot use object of type CDbCommand as array in /home/apihost/public_html/goflight/protected/controllers/BookingController.php on line 653

Any idea? And How to make A Good Query Builder with SQL Syntax like that.

  • 写回答

1条回答 默认 最新

  • douao3636 2014-07-16 08:42
    关注

    You have to use the result.. queryRow() will not change the query object itself but returns the result. Therefore use this

    $result = $checkbook->queryRow();
    

    then

     if ($result['booking'] == 1) {
                $status = true;
            } else {
                $this->actionDoubleBook();
                $status = false;
                return $status;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面