代码之家  ›  专栏  ›  技术社区  ›  pigfox

Kohana 3-查询生成器提供0行

  •  1
  • pigfox  · 技术社区  · 16 年前

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

    SELECT units .  * , locations .  *
    FROM units, locations
    WHERE units.id = '1'
    AND units.location_id = locations.id
    LIMIT 0 , 30 
    

    但当我在《科哈纳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);
    

    数组(0){}

    我做错什么了?

    2 回复  |  直到 16 年前
        1
  •  5
  •   alex    16 年前

    我不能立即告诉什么是错误的查询生成器,但是,检查这个调试的目的。

    execute() 在你的数据库链上,试试这个。

    echo Database::instance()->last_query;
    

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

    $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();
    
        2
  •  0
  •   Ixmatus    15 年前

    你会注意到你打给 last_query 返回(在 where 部分):

    WHERE units.location_id = 'locations.id' 传递用于比较的值被引用为字符串,因此结果集为空。这可能有助于您: http://kohanaframework.org/guide/api/DB#expr

    推荐文章