2012-08-10 63 views
0

我有下面的代码,我无法弄清楚CodeIgniter为什么要为它生成的查询添加一个额外的通配符。为什么CodeIgniter为这个查询添加一个额外的星号?

代码

class Foo_Model extends CI_Model { 
    private $table = 'foo'; 

    public function get_all_foos() { 
    $table = $this->table; 
    $this->db->select("$table.*")->from($table); 
    return $this->get()->result(); 
    } 
} 

而且我得到以下错误:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL >server version for the right syntax to use near '* FROM (foo , foo)' at line 1

SELECT foo .*, * FROM (foo , foo)

为什么会出现查询产生不正确的?

+0

您是否尝试过这个查询没有方法链接?你是否还尝试过不将类属性'$ table'分配给一个局部变量,而是实际上只是在from子句中引用'$ this - > $ table'?在from子句中显示两个表引用的事实绝对是奇怪的。然而,正如Catfish提到的那样,如果选择全部,那么'select()'方法并不是真的需要。 – Wolf 2012-08-10 21:50:14

回答

2

如果要选择所有内容,请不要使用->select()语句。这应该等于你的预期。

class Foo_Model extends CI_Model { 
    private $table = 'foo'; 

    public function get_all_foos() { 
    $table = $this->table; 
    $this->db->from($table); 
    return $this->get()->result(); 
    } 
} 

为一个例子见$this->db->get从文档 - http://codeigniter.com/user_guide/database/active_record.html

0

我觉得这是笨2.1.2

的错误这应该为你工作:

$this->db->select("`$table`.*",false)->from($table); 
0

如果你想选择的一切,做到这一点是通过直接使用get()的最快方法与表名。
例如:

class Foo_Model extends CI_Model { 
    private $table = 'foo'; 

    public function get_all_foos() { 
    $table = $this->table; 
    return $this->db->get($table)->result(); 
    } 
}