【问题标题】:cakephp hasmany query issuecakephp 有很多查询问题
【发布时间】:2013-07-08 13:25:14
【问题描述】:

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

表格:

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 表。

现在它似乎可以正常加入 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();

【问题讨论】:

  • 有人对此有任何想法吗?我把头从桌子上敲下来了:(

标签: php mysql cakephp


【解决方案1】:

Booking 已经绑定到Listing 中的机型了吧?那么就不需要即时使用 bindModel 了。

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

【讨论】:

  • 是的,这就是我认为我必须做的所有事情,但是当我这样做时,我得到“未找到列:1054 Unknown column 'Booking.checkin' in 'where Clause'”。我读了一遍,我在 Qu 中发布的那个链接表明 hasmany 关系并没有像我想象的那样返回结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
相关资源
最近更新 更多