Count field in list_items

edited August 2015 in Modules
Hey,

is it possible to have a column in the list view of a simple module where I can display the count of a has_many table?
For example I have a model where I display vehicles. In a has_many relationship I have n images for a vehicle. Now in list_items I want to have one column which displays the count value of the has_many relationship...

How can I achieve this?

Comments

  • edited 1:15PM
    You can overwrite the model's list_items method and add your own additional select that will contain a sub select statement to get the count which looks at the fuel_relationships table... below is a really rough example to help explain (have not tested):
    public function list_items($limit = NULL, $offset = NULL, $col = 'nav_key', $order = 'desc', $just_count = FALSE) { = mytable.id AND fuel_pages_relationships.candidate_table="my_images" ', "left"); $this->db->select('my_table.id, mycolumn1, mycolumn2, SELECT(COUNT(*) FROM fuel_relationships where candidate_table ="mytable" AND candidate_key = mytable.id) as n', FALSE); return $data; }
    That table also has a foreign_table and foreign_key which, depending on how it's saved may replace the candidate_table and candidate_key.
Sign In or Register to comment.