Search code examples
joinphpactiverecord

phpactiverecord joins with limit


What's the correct way of adding a limit to an activerecord query which includes join statements? I've already got an activerecord join statement which converts to the correct SQL

static $belongs_to = array(
array('job_request')
);

$requests = $this->all(array(
    'joins' => array('job_request'),
    'conditions' =>  array('DATE(job_date) >= ? AND DATE(job_date) <= ? AND'
                . ' user_id = ?', $start_date, $end_date, $userid))
); 

This converts to SQL as

SELECT `job_applications`.* FROM `job_applications` INNER JOIN `job_requests`  
ON(`job_applications`.job_request_id = `job_requests`.id) WHERE 
DATE(job_date) >= '2013-01-28' AND DATE(job_date) <= '2013-03-04' 
AND user_id = '1'

but if I add a limit, the join is removed from the SQL query

$requests = $this->all(array(
    'joins' => array('job_request'),
    'conditions' =>  array('DATE(job_date) >= ? AND DATE(job_date) <= ? AND'
                . ' user_id = ?', $start_date, $end_date, $userid)),
             array('limit' => 200, 'offset' => 0)

);

Generates

SELECT * FROM `job_applications` LIMIT 0,999999

Solution

  • limit and offset belong within the first array.

    array('conditions'=>'...',
          'joins'=>'...',
          'limit'=>5',
          'offset'=>5
    );
    

    Your array looks like

     array('conditions'=>'...',
          'joins'=>'...'),
          array('limit'=>5','offset'=>5)
    );