Select All Events with Event->Schedule->Date between start and end dates in CakePHP

后端 未结 4 1981
野性不改
野性不改 2020-12-22 03:16

Trying to figure out how to build a query in CakePHP where I can select all Events that are between X and Y dates (user-entered dates).

The problem lies in that the

4条回答
  •  遥遥无期
    2020-12-22 04:06

    You could try the following, assuming Cake 1.3 and using containable behaviour. I assumed that the date fields in your table are called start_date and end_date, so these conditions might need adjusting.

    This may, or may not work, and will likely produce a massive query if you have a lot of data, so some further refinement is probably necessary - certainly with the fields selected.

    You could post the query generated, if it runs, and we may be able to help adjust it further.

    /* in your EventController (the method) for on the fly */
    $this->Event->Behaviors->attach('Containable');
    
    /* Your dates */
    $x_date = '2011-06-01';
    $y_date = '2011-07-01';
    
    $this->paginate = array(
                'limit'=>10,
                'order'=>'Event.created DESC',
                'contain'=>array(
                    'Schedule',
                    'Event'=>array(
                        'conditions'=>array('Event.start_date'=>$x_date,
                                            'Event.end_date'<=$y_date)
                    )
                ),
            );
    
    $this->set('events',$this->Paginate('Event'));
    
    // print_r($events);
    

提交回复
热议问题