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

时间:2014-07-16 08:35:50

标签: php mysql sql yii

我在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;
        }

但我得到了这个错误。

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

有什么想法吗?以及如何使用SQL语法创建一个好的查询生成器。

1 个答案:

答案 0 :(得分:1)

您必须使用结果.. queryRow()不会更改查询对象本身但返回结果。因此使用此

$result = $checkbook->queryRow();

然后

 if ($result['booking'] == 1) {
            $status = true;
        } else {
            $this->actionDoubleBook();
            $status = false;
            return $status;
        }