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.

有帮助吗?

解决方案

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
            )
        )
    ));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top