如何根据关联模型的值筛选分页结果?(CakePHP 2.x)


How can I filter paginated results based on the value of associated models? (CakePHP 2.x)

我在cakefp 中有3个型号

  1. 特性
属性id名称区域子区域1名称1印度--2名称2印度--3名称3英国--4巴基斯坦--
  1. 门户
portals_id门户名称1个谷歌xml2 zameen.com3购买房产4出租房产
  1. 属性门户
property_portals_id portal_id property_id1 1 12 1 13 2 24 2 25 2 36 1 47 1 1

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

我正在使用分页。

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

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

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

我假设您已经在模型中声明了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' => '',
    )
);

对于您的Portal Model ,反之亦然

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

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

然后用查询

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

没有测试,但应该可以工作。