Pregunta

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.

¿Fue útil?

Solución

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
            )
        )
    ));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top