[英]How can I filter paginated results based on the value of associated models? (CakePHP 2.x)
我在cakephp中有3個模型
property id name area sub area 1 name 1 india -- 2 name 2 india -- 3 name 3 uk -- 4 name 4 pakistan --
portals_id portal name 1 google xml 2 zameen.com 3 buy property 4 rent property
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插件 。 您幾乎可以配置任何類型的搜索(無論多么復雜),並且它與分頁兼容。
我假設您已經在模型中聲明了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.