2

我在 cakephp 中有 3 个模型

  1. 特性
属性 id 名称 区域 子区域
1 个名字 1 个印度 --
2 名字 2 印度 --
3 名字 3 英国 --
4名4巴基斯坦——
  1. 门户
portals_id 门户名称
1 谷歌 xml
2 zameen.com
3 购买房产
4出租物业
  1. property_portals
property_portals_id portal_id property_id
1 1 1            
2 1 1
3 2 2
4 2 2
5 2 3
6 1 4
7 1 1

我有一个网格系统,可以搜索不同属性的属性。
现在我只想搜索那些将在特定门户上做广告的属性。

我正在使用分页。

$this->set('properties', $this->Paginator->paginate('Property'));
4

2 回答 2

0

我假设您已经在模型中声明了 HABTM 关系,如下所示:

class Property extends AppModel {

public $hasAndBelongsToMany = array(
    'Portal' => array(
        'className' => 'Portal',
        'joinTable' => 'property_portals',
        'foreignKey' => 'portal_id',
        'associationForeignKey' => 'property_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);

对于您的门户模型,反之亦然

然后你应该能够为你的分页设置查询参数,如下所示:

$this->Property->Portal->paginate('Post', array(
    'conditions' => array(
        'Portal.id' => $idOfPortalYouWantToSearch
    ), 'order' => array(
        'COUNT(property_portals_id )'
    )
));

然后查询:

$this->set('properties', $this->Paginator->paginate('Property'));

没有测试它,但它应该工作。

于 2015-12-18T16:35:15.417 回答
0

我建议您尝试CakeDC 搜索插件。您可以配置几乎任何类型的搜索(无论多么复杂),并且它与分页兼容。

文档中的第一个示例包括有关如何配置$filterArgs执行 HABTM 搜索的示例。

于 2015-12-05T11:04:23.013 回答