Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find by conditions in two joining tables in CakePHP

Tags:

cakephp

In my CakePHP app I have three tables:

Businesses, Towns and Categories.

A business can belong to multiple towns and multiple categories so I have created joining tables and hasMany and belongsTo relationships. Everything works fine when finding businesses by either Town or Category by using the Town or Category model to search, but I am completely stuck when I want to search for businesses in a certain town AND a certain category, eg. Plumbers in London.

The associations just don't seem to work when searching with the Business model and I get column not found errors when trying to use the associated tables. I would think that this would be along the lines of what needs to be done, but I can't get it to work:

$this->set('listings', $this->Business->find('all', array(
    'conditions' => array(
        'Business.approved' => 1,
        'BusinessesCategory.category_id' => $id,
        'BusinessesTown.town_id' => $town_id,
        'Business.sasite' => 1
)
like image 632
user1716123 Avatar asked Feb 17 '23 23:02

user1716123


2 Answers

You need to join the tables to do that. I will put above a example how has to work with category and you can do the town yourself.

$this->Business->find("all", array(
        "joins" => array(
            array(
                "table" => "businness_categories",
                "alias" => "BusinessesCategory",
                "type" => "LEFT",
                "conditions" => array(
                    "Businesses.id = BusinessesCategory.business_id"
                )
            ),
            array(
                "table" => "categories",
                "alias" => "Category",
                "type" => "LEFT",
                "conditions" => array(
                    "BusinessesCategory.category_id = Category.id"
                )
            )
        ),
        'conditions' => array(
            'Business.approved' => 1,
            'Category.id' => $id,
        )
    ));

You also could use a behavior to do that for you: https://github.com/Scoup/SuperJoin

like image 140
Scoup Avatar answered Mar 16 '23 05:03

Scoup


Hi I had a very similar setup and the same problem. This is how I would solve your problem:
As you dont give away to much of your code I make some assumptions:
- You implemented your search method in the BusinessController
- Your search argument for the town is stored in vaiable $where and the one for Category is stored in $what

Code if you only have conditions for one table

    $this->Businesses->Town->recursive = -1;
    ....
    $options['joins'] = array(
            array('table' => 'towns',
            'alias' => 'Town',
            'type' => 'inner',
            'conditions' => array(
                'Business.town_id = Town.id',
            )
            )
        );
        $options['conditions'] = array(
            'Town.townName' => $where
        );
        $result = $this->Business->find('all', $options);

Code if you have conditions for two table

    $this->Businesses->Town->recursive = -1;
    $this->Businesses->Category->recursive = -1;
    ....
    $options['joins'] = array(
            array('table' => 'towns',
            'alias' => 'Town',
            'type' => 'inner',
            'conditions' => array(
                'Business.town_id = Town.id',
            )
            ),
           array('table' => 'categories',
            'alias' => 'Category',
            'type' => 'inner',
            'conditions' => array(
                'Business.category_id = category.id',
            )
            )
        );
        $options['conditions'] = array( 
                'Town.townName' => $where,
            'Category.categoryName' => $what
        );
        $result = $this->Business->find('all', $options);
like image 35
David Avatar answered Mar 16 '23 04:03

David