Cakephp:按相关模型查找订单

时间:2015-02-05 16:38:08

标签: cakephp cakephp-2.3

我有两个模型:事件和日期。一个事件有很多日期。我想按最近的日期订购活动。例如:

[0] => Array
        (
            [Event] => Array
                (
                    [id] => 1
                    [name] => Event 1
                )

            [Date] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [date] => 2015-02-05
                            [start_time] => 06:00:00
                            [end_time] => 17:00:00
                            [evento_id] => 1
                        )
                    [1] => Array
                        (
                            [id] => 2
                            [date] => 2015-02-06
                            [start_time] => 06:00:00
                            [end_time] => 17:00:00
                            [evento_id] => 1
                        )

                )

        )
[1] => Array
        (
            [Event] => Array
                (
                    [id] => 2
                    [name] => Event 2
                )

            [Date] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [date] => 2015-02-11
                            [start_time] => 06:00:00
                            [end_time] => 17:00:00
                            [event_id] => 2
                        )    
                )

        )
[2] => Array
        (
            [Event] => Array
                (
                    [id] => 3
                    [name] => Event 3
                )

            [Date] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [date] => 2015-02-02
                            [start_time] => 06:00:00
                            [end_time] => 17:00:00
                            [event_id] => 3
                        )
                    [1] => Array
                        (
                            [id] => 6
                            [date] => 2015-02-03
                            [start_time] => 06:00:00
                            [end_time] => 17:00:00
                            [event_id] => 3
                        )

                )

        )

因此,我想要检索事件的顺序是:事件3,事件1,事件2

这就是我调用find方法的方法:

$this->Event->find('all', array('contain' => array('Date' => array('conditions' => array('Date.date >=' => date('Y-m-d'))))));

使用Cakephp find方法可以做到这一点吗?感谢。

1 个答案:

答案 0 :(得分:0)

这样的事情应该这样做

$this->Event->find('all', array(
    'contain' => array(
        'Date' => array(
            'conditions' => array('
                Date.date >=' => date('Y-m-d')
            )
        )
    ),
    order => array(
        'COUNT(Date.id)' => 'DESC'
    )

));