Steven Jones Thu Mar 15 13:04:20 -0400 2012

Subject: How to get number of rows?

This is really straight forward. I need to do pagination and i need to know the number of all the rows from a table.
Now the question is how do i do this from the Model without using ::all()? I would really like to do this from active record.


Luke Stokes Thu Mar 15 16:15:05 -0400 2012

Hey Steven. I had to do the exact same thing and ended up adding this method to our base controller:

    /**
     * Get the total count of items available for getModelArray without a limit or offset
     * @param ActiveRecord\Model $Model
     * @param array $find_options these options are similar to Active Records find() input array
     * @param integer $foreign_key_id
     * @param string $foreign_key_type such as store, user, or customer
     * @return integer
     */
    static function getModelArrayCount($Model, $find_options, $foreign_key_id, $foreign_key_type) {
        $count_options = $find_options;
        unset($count_options['limit']);
        if (isset($count_options['offset'])) {
            unset($count_options['offset']);
        }
        $method_name = 'count_by_' . $foreign_key_type . '_id';
        return $Model::$method_name($foreign_key_id, $count_options);
    }

(1-1/1)