CakePHP Query with associations

Posted on

Query format with associations

Rooms belong to properties. To fetch all the properties with name and location as the query search/filter parameter below is a how one gets to achieve this:


$q = $this->getRequest()->getQuery('q');
$q =  strtolower($q);
$rooms = $this->paginate($roomsTable->find()
    ->matching('Properties.Landlord', function ($query) {
        return $query->where([
            'active ' => 'yes',
        ]);
    })
    ->where([
        'Rooms.occupied' => 'no',
        'Rooms.occupant_id IS' => null,
        'OR' => [
            'LOWER(Rooms.name) LIKE' => "%$q%",
            'Rooms.cost LIKE' => "%$q%",
            'LOWER(Properties.name) LIKE' => "%$q%",
            'LOWER(Properties.location) LIKE' => "%$q%",

        ],
    ])
    ->contain(['Properties.Agents.Managers']));