【问题标题】:cakephp contains condition date filter or limit recordscakephp 包含条件日期过滤器或限制记录
【发布时间】:2012-10-10 17:01:24
【问题描述】:

在我的发现中,我使用包含条件来获取当前的日程安排,如图所示:

    $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)
            )
        )
    ));

它工作正常,但是当满足多个记录的条件时,我只需要更接近现在的记录。

我知道这更像是一个 sql 问题,但我需要过滤器来适应包含条件格式。

你能帮忙吗?

非常感谢。

【问题讨论】:

  • 你没有拿到第一条记录吗?更接近现在是什么意思?
  • @Moyed Ansari。我得到 2 条符合条件的记录 ho_valid date('Y-m-d');我只需要离现在更近的那个 (date('Y-m-d')) thx!

标签: mysql cakephp contain


【解决方案1】:

因此您需要在日期添加订单并应用限制,以便您可以获得最新的第一条记录

 $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
            )
        )
    ));

【讨论】:

  • 谢谢@Moyed Ansari;只需添加“desc”即可获得最接近现在的最新信息。我真的很感谢你的帮助。 :-)
猜你喜欢
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2015-11-05
  • 2011-07-09
相关资源
最近更新 更多