簡體   English   中英

如何根據關聯模型的值過濾分頁結果? (CakePHP 2.x)

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

我在cakephp中有3個模型

  1. 性能
property id   name    area    sub area
1             name    1       india   --
2             name    2       india   --
3             name    3       uk    --
4             name    4       pakistan  --
  1. 門戶
portals_id  portal name
1           google xml
2           zameen.com
3           buy property
4           rent property
  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'));

我建議您嘗試使用CakeDC Search插件 您幾乎可以配置任何類型的搜索(無論多么復雜),並且它與分頁兼容。

文檔中第一個示例包含有關如何配置$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' => '',
    )
);

反之亦然,您的門戶網站模型

然后,您應該可以為分頁設置查詢參數,如下所示:

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

然后查詢:

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

沒有測試,但它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM