Kohana 3 - "查询"构建器提供0行

pig*_*fox 1 php builder kohana kohana-3

从phpmyadmin运行时,以下查询按预期返回一行.

SELECT units .  * , locations .  *
FROM units, locations
WHERE units.id = '1'
AND units.location_id = locations.id
LIMIT 0 , 30 
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在Kohana 3中做到这一点时:

$unit = DB::select('units.*', 'locations.*')
->from('units', 'locations')
->where('units.id', '=', $id)->and_where('units.location_id', '=', 'locations.id')
->execute()->as_array();
var_dump($unit);
Run Code Online (Sandbox Code Playgroud)

它打印

数组(0){}

我究竟做错了什么?

ale*_*lex 5

我不能立即告诉该查询构建器有什么问题,但是,为了调试目的,请将其签出.

在调用execute()数据库链后,试试这个.

echo Database::instance()->last_query;
Run Code Online (Sandbox Code Playgroud)

这将在纯SQL中显示最后执行的查询.值得查看查询生成器生成的内容,以及它与您在phpmyadmin中使用的SQL的不同之处.

如果所有其他方法都失败了,只需使用普通查询方法.

$query = "SELECT units .  * , locations .  *
FROM units, locations
WHERE units.id = :id
AND units.location_id = locations.id
LIMIT 0 , 30 ";

$unit = Db::query(Database::SELECT, $query)
          ->bind(':id', (int) $id)
          ->execute()
          ->as_array();
Run Code Online (Sandbox Code Playgroud)