简体   繁体   English

使用不同功能的where子句的Codeigniter

[英]Codeigniter using different function's where clause

I have a Postgres database with a table for orders and a table order_contents , which includes all items of the order , and the order_id . 我有一个Postgres数据库,其中有一个orders表和一个表order_contents ,其中包括该order所有项目以及order_id I'm trying to make a view with order history. 我正在尝试使用订单历史记录进行查看。

In my model I have two functions, one to retrieve orders, and another to retrieve all the contents of an order, like so: 在我的模型中,我有两个功能,一个用于检索订单,另一个用于检索订单的所有内容,如下所示:

function retrieve_orders($userID){
    $query = $this->db->get('orders');
    $this->db->where('user_id', $userID);
    return $query->result_array();
}

function get_order_contents($orderID){
    $query = $this->db->get('order_contents');
    $this->db->where('order_id', $orderID);
    return $query->result_array();
}

So in my controller I call retrieve_orders() and pass the resulting array to the view. 因此,在我的控制器中,我调用retrieve_orders()并将结果数组传递给视图。 In the view, I want to create an HTML table for each order with all it's contents. 在视图中,我想为每个订单及其所有内容创建一个HTML表。 So I iterate through each order and call get_order_contents in the loop. 因此,我遍历每个订单并在循环中调用get_order_contents。

Everything should work, however what happens is this error shows up in the source: 一切正常,但是发生此错误的原因是在源代码中显示:

Error Number: 错误编号:

ERROR: column "user_id" does not exist LINE 3: WHERE "user_id" = '3' ^ 错误:列“ user_id”不存在第3行:“ user_id” ='3'^

SELECT * FROM "order_contents" WHERE "user_id" = '3' SELECT * FROM“ order_contents”其中“ user_id” ='3'

As you can see, for some reason, when I call get_order_contents() in the view, it uses the where clause I specified for the retrieve_orders() function. 如您所见,由于某种原因,当我在视图中调用get_order_contents()时,它将使用我为retrieve_orders()函数指定的where子句。

What I have done so far is try to manually stop caching, but to no avail. 到目前为止,我所做的是尝试手动停止缓存,但无济于事。 Any help here is appreciated. 感谢您的帮助。

In each function, try to put where() before get(). 在每个函数中,尝试将where()放在get()之前。

When you try to get() order_contents it will use where clause from previous where(). 当您尝试获取get()order_contents时,它将使用先前where()中的where子句。

Common practice is, use get() at the last call of $this->db after other calls such as where(), etc. See the Active Record documentation in codeigniter. 常见的做法是,在$ this-> db的最后一次调用之后,在其他调用(例如where()等)之后使用get()。请参见codeigniter中的Active Record文档。

Chain your db functions like this 像这样链接您的数据库函数

return $this->db->where('user_id', $userID)
                ->get('orders')
                ->result_array();

为什么不使用$this->db->get_where()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM