phpormkohana

Kohana ORM count_all() working but find_all() is not


I'm having an issue where I'm building an ORM query based on several conditions from a $_POST. The final query looks fine and returns records in a direct SQL query (phpmyadmin) but in my application does not return any records. here is the code...

        $records = ORM::factory('record')->where(array('date >='=>$_POST['fromdate'],'date <='=>$_POST['todate']));
        if ($_POST['agent'] != '0') $records->where(array('ccp_id'=>$_POST['agent']));
        if ($_POST['supervisor'] != '0') {
            $ccps = ORM::factory('employee')->where(array('supervisor_id'=>$_POST['supervisor'],'active'=>'1'))->find_all();
            foreach ($ccps as $ccp) {
                $agents[] = $ccp->id;
            }
            // echo kohana::debug($agents);
            $records->in('ccp_id',$agents);
        }
        if ($_POST['lead'] != '0') $records->where(array('lead_id'=>$_POST['lead']));
        if ($_POST['reasons'] != '[]') {
            $reasons = explode(',',str_replace(array('[',']','"'),'',$_POST['reasons']));
            $records->in('reason_id',$reasons);
        }
        $records->find_all();

$records->loaded is false. If I change out the find_all() with count_all() I get an accurate count.

With sample data in the $_POST I have this query in $records->last_query()

SELECT `records`.*
FROM (`records`)
WHERE `date` >= '2010-10-10'
AND `date` <= '2010-11-09'
AND `ccp_id` IN ('E128092','E128093','E124874','E124414','E129056','E137678','E078952','E112701','E084457','E098047','E099221','E001131','E120892')
AND `lead_id` = 'E110873'
AND `reason_id` IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
ORDER BY `records`.`id` ASC

this returns 4 records in phpmyadmin and (4) for count_all(). I do not understand why this is happening. Any insights would be helpful. Thank you.


Solution

  • In your last line you should have

    $records = $records->find_all();
    

    instead of

    // this actually returns you the resultset and resets the query builder object
    $records->find_all()