4

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

Businesses,TownsCategories.

一个企业可以属于多个城镇和多个类别,因此我创建了连接表hasManybelongsTo关系。通过使用城镇或类别模型搜索按城镇或类别查找企业时,一切正常,但当我想搜索某个城镇和某个类别的企业时,我完全卡住了,例如。伦敦的水管工。

在使用业务模型进行搜索时,这些关联似乎不起作用,并且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
)
4

3 回答 3

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

于 2013-04-01T20:57:03.070 回答
2

嗨,我有一个非常相似的设置和同样的问题。这就是我将如何解决您的问题:
由于您没有放弃您的大部分代码,我做了一些假设:
-您在 BusinessController 中实现了您的搜索方法
-您的城镇搜索参数存储在变量 $where 和一个 for类别存储在 $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);
于 2014-01-01T13:13:52.743 回答
0

您可以使用

$this->Business->find('all', array(
    'conditions' => array(
          'AND' => array(
             'BusinessesTown.town_id' => $town_id,
             'BusinessesCategory.category_id' => $id
        )
      ),
    'recursive' => 2
));
于 2015-04-09T08:53:17.013 回答