سؤال

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