开发者

cakephp check multiple tables

I am getting a list of Rate information from the database with conditions. However I wish to add another condition of minimum Stay if the date range has a date in it and rateID form another table called minimum stay. The Rate table already has a min stay but on certain dates I want to over ride this if the date falls within the date range I pass.

I am new to cakephp so I am unsure how to check the minStay table for dates in the date range. Then get the largest minStay and the add it to the condition.

Default minStay in Rate table is 1

Here is the data in the 开发者_StackOverflow社区minStay table:

date:31-10-2011 rateID:21 minStay:2 date:1-11-2011 rateID:21 minStay:3

Results: If date range is 31-10-2011 to 2-11-2011 / 2 nights then no results. If 3 nights or more then result.

I hope I have explaind that correctly.

Variable containing the date range is $todays

        $conditions = array(
                'Rate.enabled'=>1,'Rate.is_corporate'=>$is_corporate, 'Rate.minimum_stay <='=>$days, 
                'Rate.valid_from < '=>$date_start,'Rate.valid_to >'=>$date_end,
                'OR'=>array('Rate.maximum_stay'=>0,'Rate.maximum_stay >='=>$days)
        );

        $order = 'Rate.list_no';
        $this->Rate->contain('Room.id','Room.title','Room.max_adults','Room.max_children');
        $rates = $this->Rate->find('all',array('conditions'=>$conditions,'order'=>$order));


I think i understood what you want to do, if not please comment.

To do that you need to do somthing like this (assuming you have something like has many MinStay)

    $conditions = array(
            'Rate.enabled'=>1,'Rate.is_corporate'=>$is_corporate, 'Rate.minimum_stay <='=>$days, 
            'Rate.valid_from < '=>$date_start,'Rate.valid_to >'=>$date_end,
            'OR'=>array('Rate.maximum_stay'=>0,'Rate.maximum_stay >='=>$days),
            'AND' => array('OR'=> array('minStay.date BETWEEN ? AND ?' => array($date_start,$date_end)),
                 'minStay.minimum_stay <' =>$days )
    );

    $order = 'Rate.list_no';
    $fields = array ('Room.id','Room.title','Room.max_adults','Room.max_children');
    $rates = $this->Rate->find('all',array('conditions'=>$conditions,'order'=>$order, 'fields'=> $fields));

I think that will do it, hope it works for you

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜