2

I have 3 models in cakephp

  1. properties
property id   name    area    sub area
1             name    1       india   --
2             name    2       india   --
3             name    3       uk    --
4             name    4       pakistan  --
  1. portals
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

I have a grid system which search properties on different attributes.
Now i want to search only those properties which will advertise on specific portal.

I am using pagination.

$this->set('properties', $this->Paginator->paginate('Property'));
Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
Kamran Ali
  • 21
  • 1
  • i am searching on properties and now i want to check how many properties are advertised on a specific portal. so my search query contains "properties" and "properties_portals" models – Kamran Ali Nov 25 '15 at 18:10

2 Answers2

0

I recommend that you try the CakeDC Search plugin. You can configure almost any type of search (no matter how complex), and it's compatible with Pagination.

The first example in the docs includes a sample on how to configure $filterArgs to perform a HABTM search.

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
0

I assume you have declared a HABTM relationship in your models like so:

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' => '',
    )
);

And vice versa for your Portal Model

Then you should be able set the query parameters for your pagination like this:

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

And then query with:

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

Did not test it but it should work.

Max90
  • 193
  • 2
  • 14