Question

In my find, I'm using contain with a condition to get the current schedule, as shown:

    $this->request->data = $this->Employee->find('first', array(
        'conditions' => array('Employee.emp_appserial' => $id),
        'contain' => array(
            'Schedule' => array(
                'conditions' => array('ho_valid <=' => date('Y-m-d'), 'ho_status' => 1)
            )
        )
    ));

It works fine, but when the conditions is met for more than one record I need only the one closer to now.

I'm aware is more of a sql question, but I need the filter to fit the contain condition format.

Can you help?

Thank you so much.

Was it helpful?

Solution

So you need to add order by on the date and apply limit so that you can achieve latest first record

 $this->request->data = $this->Employee->find('first', array(
        'conditions' => array('Employee.emp_appserial' => $id),
        'contain' => array(
            'Schedule' => array(
                'conditions' => array('ho_valid <=' => date('Y-m-d'), 'ho_status' => 1),
                'order' => 'ho_valid'
                'limit' => 1
            )
        )
    ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top