문제

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