Welcome Guest, Not a member yet? Register   Sign In
Acrive Record Class or_where and where
#1

[eluser]Unknown[/eluser]
Hi there,

I use the Active Record Class and it's really great! I like it!

But I have some troubles. I have a foreach clause which creates an or_where if the field name is the same as in the iteration before and a where if the field name is not the same as in the iteration before. It looks like this:

Code:
if(is_array($filters)) {
            foreach($filters as $filter => $filtervalues) {
                $valueCount = 0;
                if($filter == 'search') {
                    $this->db->like('ELEMENT', $filtervalues);
                    continue;
                }
                if(is_array($filtervalues)) {
                    foreach($filtervalues as $value) {
                        if($valueCount > 0) {
                            $this->db->or_where($filter, $value);
                        } else {
}
                            $this->db->where($filter, $value);
                        }
                        $valueCount++;
                    }
                }
            }
        }
        $this->db->order_by($order);
        $q = $this->db->get('bpmpallover');
        if($q->num_rows() > 0) {
            foreach($q->result() as $row) {
                $data[] = $row;
            }
            $q->free_result();
            return $data;
        }

For example, this produces the following SQL code:

Code:
SELECT *
FROM (`bpmpallover`)
WHERE `ELEMENTPLATFORM` = 'bpm-platform-windows-2003'
OR `ELEMENTPLATFORM` = 'bpm-platform-windows-generic'
OR `ELEMENTPLATFORM` = 'bpm-platform-windows-2000'
AND `ELEMENTSTATUS` = 'critical'
ORDER BY `ELEMENT`

But it should create the following SQL code:

Code:
SELECT *
FROM
(`bpmpallover`)
WHERE
(`ELEMENTPLATFORM` = 'bpm-platform-windows-2003'
OR `ELEMENTPLATFORM` = 'bpm-platform-windows-generic'
OR `ELEMENTPLATFORM` = 'bpm-platform-windows-2000') AND `ELEMENTSTATUS` = 'critical'
ORDER BY `ELEMENT`

Please note the brackets for the OR statements with the field ELEMENTPLATFORM.

I know, I could wite a custom query with
Code:
$this->db->query("SQL")
but I'm wondering if there is a possible way to do this with the Active Record Class.
#2

[eluser]Unknown[/eluser]
Hi there,

For all who are interessted, I've found a solution. In now use where_in and it works perfect:

Code:
if(is_array($filters)) {
    foreach($filters as $filter => $filtervalues) {
        if($filter == 'search') {
            $this->db->like('ELEMENT', $filtervalues);
            continue;
        }
        if(is_array($filtervalues)) {
            $this->db->where_in($filter, $filtervalues);
        }
    }

    $this->db->order_by($order);
    $q = $this->db->get('bpmpallover');
    if($q->num_rows() > 0) {
        foreach($q->result() as $row) {
            $data[] = $row;
        }
        $q->free_result();
        return $data;
    }
}

And I could reduce one foreach clause.




Theme © iAndrew 2016 - Forum software by © MyBB