获取CakePHP的可搜索行为会导致更深层次的关联

时间:2010-12-09 22:27:38

标签: cakephp cakephp-1.3 searchable containable

我正在尝试使用CakePHP 1.3.5的searchable behavior具有可包含的行为来返回指定模型的搜索结果相关模型(文章belongsTo用户)。

暂时忽略可搜索的行为,以下调用 find()

$this->Article->find('all', array(
    'conditions' => array('Article.is_published' => 1),
    'fields' => array('Article.id'),
    'contain' => array('User.name')
));

执行此SQL查询:

SELECT `Article`.`id`, `User`.`name`, `User`.`id` FROM `articles` AS `Article` LEFT JOIN `users` AS `User` ON (`Article`.`user_id` = `User`.`id`) WHERE `Article`.`is_published` = 1 

并返回以下数组:

Array (
    [0] => Array (
        [Article] => Array (
            [id] => 10
        )
        [User] => Array (
            [name] => Author Name
            [id] => 7
        )
    )
    ...
)

这正是预期的结果。但是,以下调用 search()

$this->Article->search($query, array(
    'conditions' => array('Article.is_published' => 1),
    'fields' => array('Article.id'),
    'contain' => array('Article' => array('User.name'))
));

执行此SQL查询:

SELECT `Article`.`id` FROM `search_index` AS `SearchIndex` LEFT JOIN `articles` AS `Article` ON (`SearchIndex`.`model` = 'Article' AND `SearchIndex`.`association_key` = `Article`.`id`) WHERE `Article`.`is_published` = 1 AND MATCH(`SearchIndex`.`data`) AGAINST('search term' IN BOOLEAN MODE) AND `Article`.`id` IS NOT NULL 

并返回此数组:

Array (
    [0] => Array (
        [Article] => Array (
            [id] => 9
        )
    )
    ...
)

查看search()方法,它返回$this->SearchIndex->find('all', $findOptions); $ findOptions 包含以下内容:

Array (
    [conditions] => Array (
        [Article.is_published] => 1
        [0] => MATCH(SearchIndex.data) AGAINST('search term' IN BOOLEAN MODE)
    )
    [fields] => Array (
        [0] => Article.id
    )
    [contain] => Array (
        [Article] => Array (
            [0] => User.name
        )
    )
)

该关联不会在此过程中丢失,因为在SearchableBehavior内部,$this->SearchIndex->Article->belongsTo['User']在search()方法内调用find()之前和之后立即存在且完好无损。

对search()的调用会为以下所有值'contains'返回完全相同的内容

array('Article' => array('User.name'))

array('Article' => array('User'))

array('Article' => array('User' => array()))

array('Article' => array('User' => array('fields' => array('User.name'))))

array('Article' => array('User' => array('fields' => array('name'))))

我做错了吗?我认为我使用的格式与the CakePHP documentation中指示的格式相同,我没有在网上找到任何表明您必须做一些特殊操作才能获得带有关联数据的搜索结果的内容。

我知道我可以轻松地通过查找带有其他调用find()的用户来实现我想要的结果,但是我希望得到可包含的行为,就像它应该的那样工作并减少不必要的额外数据库查询。

1 个答案:

答案 0 :(得分:1)

使用containsable时,将递归选项设置为“true”

$this->Model->Behaviors->attach("Containable",array("recursive"=>true));