如何在CakePHP中的两个连接表中按条件查找

时间:2013-04-01 19:53:37

标签: cakephp

在我的CakePHP应用程序中,我有三个表:

BusinessesTownsCategories

企业可以属于多个城镇和多个类别,因此我创建了加入表格以及hasManybelongsTo关系。通过使用城镇或类别模型进行搜索时,通过城镇或类别查找商家时,一切正常,但当我想要搜索特定城镇和特定类别的商家时,我完全陷入困境,例如。伦敦的管道工。

在使用Business模型进行搜索时,这些关联似乎不起作用,并且在尝试使用关联表时出现column not found错误。我认为这将是需要做的事情,但我不能让它发挥作用:

$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
)

3 个答案:

答案 0 :(得分:6)

你需要加入表来做到这一点。 我将举例说明如何使用类别,你可以自己做镇。

$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,
        )
    ));

您还可以使用某种行为为您执行此操作: https://github.com/Scoup/SuperJoin

答案 1 :(得分:2)

嗨我有一个非常相似的设置和同样的问题。这就是我如何解决你的问题:
由于你没有放弃你的大部分代码,我做了一些假设:
- 您在BusinessController中实现了搜索方法 - 您对该城镇的搜索参数存储在vaiable $ where中,而Category的搜索参数存储在$ what

代码,如果您只有一个表的条件

    $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);

代码,如果您有两个表的条​​件

    $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);

答案 2 :(得分:0)

您可以使用

$this->Business->find('all', array(
    'conditions' => array(
          'AND' => array(
             'BusinessesTown.town_id' => $town_id,
             'BusinessesCategory.category_id' => $id
        )
      ),
    'recursive' => 2
));