CodeIgniter with Active Records and or_like

笑着哭i 提交于 2019-12-11 18:04:04

问题


I have an admin panel with a view for all the users info gotten from a Database table named "users".

There's a search box and I use Active records to fetch the data. But there seems to be a problem with that and I can't figure it out. I'm pretty sure it's in the or_like part but the error confuses me.

Here is the query I make passing one variable $data posting the info from the search box :

public function get($data) {

    $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');

    if (isset($data['query']) && $data['query'] != '') {
        $fields = json_decode($data['fields'], true);

        $this->db->like($fields[0], $data['query']);

        foreach ($fields as $field) {
            $this->db->or_like($field, $data['query']);
        }

    }

    $this->db->limit($data['limit'], $data['start']);
    if (isset($data['sort'])) {
        $sort = json_decode($data['sort'], true);
        $this->db->order_by($sort[0]['property'], $sort[0]['direction']);
    }

    $query = $this->db->get('users');

Because it's somewhat a refactoring of an existing program, I had to add a new feature - 'groups' which is not part of the users table so I add the key['groups']=>with values looping the query result :

$users = array();
foreach ($query->result_array() as $user) {
    $users[] = array_merge($user, array('groups' =>''));
}

$count = sizeof($users);
//Adding groups id's as values to the group key         
foreach ($this->getGroupsAndUsers() as $group) {
    for ($i = 0; $i < $count; $i++) {
        if ($users[$i]['id'] == $group['user_id']) {
            //Begin check
            if ($users[$i]['groups'] == '') {
                $users[$i]['groups'] .= $group['group_id'];
            } else {
                $users[$i]['groups'] .= "," . $group['group_id'];
            }
            //End of check 
        }
    }       
}
$result = $users;
return $result;

And with passing the final array to the $result variable this should be all, but when I try to search a certain user I get this error:

<p>Error Number: 1054</p><p>Unknown column 'groups' in 'where clause'</p><p>SELECT `id`, `email`, `firstname`, `lastname`, `usertype`, `ts_created`, `ts_last_login`, `position`
FROM (`users`)
WHERE  `firstname`  LIKE '%stefan%'
OR  `firstname`  LIKE '%stefan%'
OR  `lastname`  LIKE '%stefan%'
OR  `email`  LIKE '%stefan%'
OR  `position`  LIKE '%stefan%'
OR  `groups`  LIKE '%stefan%'

it says that groups is unknown column and it's missing from the select but I'm not sure it's the real problem cause I actually see the groups and have a UI in the admin panel for it. Also, being a MySQL noob, I think it's kind of strange to get a query like this:

WHERE  `firstname`  LIKE '%stefan%'
    OR  `firstname`  LIKE '%stefan%'
    OR  `lastname`  LIKE '%stefan%'
    OR  `email`  LIKE '%stefan%'
    OR  `position`  LIKE '%stefan%'
    OR  `groups`  LIKE '%stefan%'

I mean, is this the normal behaviour of or_like to get the same value?

And at the end, because I'm really lost here, if anyone can help or suggest what could cause this problem would be really appreciated.


回答1:


I found the answer for codeigniter like and or_like problem at ellislab forum https://ellislab.com/forums/viewthread/185983/ Instead of using

$this->db->like('location', $your_string);

use:

$this->db->where('location LIKE', '%'.$your_string.'%');

and or_where instead of or_like.



来源:https://stackoverflow.com/questions/9982972/codeigniter-with-active-records-and-or-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!