activerecord - Codeigniter Active Record - WHERE inside IFNULL and COUNT -


i have application simulates locker. inside each locker have n racks. inside each rack have n boxes, n folders , n books. of objects not acessible (they have boolean attribute called acessible).

i want show each locker , sum of objects ordering type (book, folder , box). like:

rack_id: 1
rack_number: 001
locker_number: 54
sum_boxes: 10
sum_folders: 20
sum_books: 10

my query:

$this->db->select('r.id rack_id, r.number rack_number, l.number locker_number)   ->select('ifnull(count(distinct `box`.`id`), 0) `sum_boxes`', false)   ->select('ifnull(count(distinct `book`.`id`), 0) `sum_books`', false)   ->select('ifnull(count(distinct `folder`.`id`), 0) `sum_folders`', false);  $this->db->from('rack r');  $this->db->join('locker l', 'l.id = r.locker_fk', 'inner'); $this->db->join('box box', 'box.rack_fk = r.id', 'left'); $this->db->join('book book', 'book.rack_fk = r.id', 'left'); $this->db->join('folder folder', 'folder.rack_fk = r.id', 'left');  $this->db->where('r.locker_fk', $this->session->userdata('locker_id'));  $this->db->group_by("r.id"); 

this works fine, count returns boxes, books , folders each rack, acessible or not. want input condition on each count returns acessible objetcs (something boxes.acessible = 1). how can that?

thanks.

edit: ok found solution: changing this:

$this->db->join('box box', 'box.rack_fk = r.id , box.acessible= "1"', 'left'); $this->db->join('book book', 'book.rack_fk = r.id , book.acessible= "1"', 'left'); $this->db->join('folder folder', 'folder.rack_fk = r.id , folder.acessible= "1"', 'left'); 

i guessing this:

$this->db->where('r.locker_fk', $this->session->userdata('locker_id')); $this->db->where('box.acessible', '1'); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -