php - cakephp hasmany查询问题

标签 php mysql cakephp

我有三个表,它们的关联如下: 房源有很多预订 列表有很多特价

表格:

bookings.id、bookings.start、bookings.stop、bookings.listing_id

special_prices.id、special_prices.start、special_prices.stop、special_prices.price、special_prices.listing_id、

当然还有Listings表。

关联全部设置在模型文件中,由蛋糕烘焙提供。用户可以根据区域、开始日期和结束日期搜索所有列表。我正在尝试获取一组结果,其中仅返回列表(如果列表的开始日期不介于该列表可能具有的任何预订的日期之间)。

在我的搜索功能中,我有:

$conditions = array(
"AND" => array(
    "OR" => array(
                "Listing.address_one LIKE" => "%".$area."%",
                "Listing.address_two LIKE" => "%".$area."%",
                "Listing.city LIKE" => "%".$area."%",
                "Listing.postcode LIKE" => "%".$area."%",
                "Listing.title LIKE" => "%".$area."%",
                ),
                )
            );

$this->Listing->bindModel(array('hasMany' => array('Booking' => array('conditions' =>array('Booking.checkin BETWEEN ? AND ?' => array($to, $from))))));
$data = $this->paginate('Listing', $conditions);
$this->set('data', $data);

我在这里读到:http://ibndawood.com/2011/10/how-to-filter-hasmany-related-model-records-in-cakephp-when-using-find-function/另一种选择是使用

$this->Listing->hasMany['Booking']['conditions'] = array('Booking.checkin BETWEEN ? AND ?' => array($to, $from));

但不幸的是,这两个都只是返回数据库中与该区域匹配的所有条目,但似乎完全忽略了 Bookings 表。我已经尝试过设定条件

'Booking.id' => '5'

要测试,但它也忽略了这一点。

有人可以帮我解释一下吗?文档中没有太多示例,其中的示例对我不起作用。

编辑

我设法发现,因为 cakephp 在分页时不会自动加入 hasmany 关系,因此我必须添加一些代码来告诉它加入预订表。

现在它似乎与 Bookings 表连接正常,但它返回多组列表。任何人都可以看一下并帮助我解决这个问题吗?这是我现在拥有的:

$conditions = array(
                            "OR" => array(
                                "Listing.address_one LIKE" => "%".$area."%",
                                "Listing.address_two LIKE" => "%".$area."%",
                                "Listing.city LIKE" => "%".$area."%",
                                "Listing.postcode LIKE" => "%".$area."%",
                                "Listing.title LIKE" => "%".$area."%",
                            )
                        );


    $this->paginate = array(
               'joins'=>array(
                          array(
                           'table'=>'bookings',
                           'alias'=>'Booking',
                           'type' =>'inner',
                           'conditions' =>array('Booking.checkin NOT BETWEEN ? AND ?' => array($to, $from))
                          )
                        ),
                'conditions'=> $conditions
                );

    $data = $this->paginate();

最佳答案

预订已经绑定(bind)到模型中的列表,对吗?那么就不需要动态使用bindModel。

只需将您的 Booking.checkin BETWEEN $to AND $from 添加到 $conditions 数组中

关于php - cakephp hasmany查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17527833/

相关文章:

mysql - Utf8 和 latin1

CakePHP:在单个下拉列表中显示多个字段

php - Cakephp 权限被拒绝 Fileengine.php

php - 如何在 PHP 中缩写 MySQL 查询?

php - 如何使整个表格单元格充当可点击的表单区域

php - 如何防止 PHP 中的 SQL 注入(inject)?

CakePHP 表单助手输入日期

php - undefined variable argc PHP

python - MYSQL 语句中的动态表名 (Google Cloud SQL)

mysql - 将mysql结果保存到datatable