在CakePHP中搜索模型中的数据

时间:2012-06-17 07:08:57

标签: php database cakephp

我正在研究我的第一个使用CakePHP进行实验的项目。基本上我有一个网站,我希望用户能够搜索存储在MySQL数据库中的不同训练。

当用户输入搜索词时,我想简单地返回名称中包含搜索词的任何练习。最重要的是,我希望他们能够应用过滤器将搜索范围缩小到特定的肌肉群。

过滤器通过查询字符串传递给搜索操作,搜索项使用post方法传递。

首先,这是我现在关注的两个模型:

class Workout extends AppModel {
    public $name = 'Workout';
    public $hasAndBelongsToMany = array(
        'MuscleGroup' =>
            array(
                'className'              => 'MuscleGroup',
                'joinTable'              => 'workouts_muscle_groups',
                'foreignKey'             => 'workout_id',
                'associationForeignKey'  => 'muscle_group_id',
                'unique'                 => true,
                'conditions'             => '',
                'fields'                 => '',
                'order'                  => '',
                'limit'                  => '',
                'offset'                 => '',
                'finderQuery'            => '',
                'deleteQuery'            => '',
                'insertQuery'            => ''
            )
    );
}


class MuscleGroup extends AppModel {
    public $name = 'MuscleGroup';
    public $hasAndBelongsToMany = array(
        'Workout' =>
            array(
                'className'              => 'Workout',
                'joinTable'              => 'workouts_muscle_groups',
                'foreignKey'             => 'muscle_group_id',
                'associationForeignKey'  => 'workout_id',
                'unique'                 => true,
                'conditions'             => '',
                'fields'                 => '',
                'order'                  => '',
                'limit'                  => '',
                'offset'                 => '',
                'finderQuery'            => '',
                'deleteQuery'            => '',
                'insertQuery'            => ''
            )
    );
}

现在,下面是我的WorkoutsController中的搜索操作:

function search() {
    $hasFilters = false;
    $filters = array(
        "abs" => false, 
        "back" => false, 
        "biceps" => false, 
        "chest" => false, 
        "forearms" => false, 
        "legs" => false, 
        "shoulders" => false, 
        "triceps" => false
    );

    if(isset($this->params['url']['abs']))
        if($this->params['url']['abs'] == "1")
            $filters['abs'] = $hasFilters = true;
    if(isset($this->params['url']['back']))
        if($this->params['url']['back'] == "1")
            $filters['back'] = $hasFilters = true;
    if(isset($this->params['url']['biceps']))
        if($this->params['url']['biceps'] == "1")
            $filters['biceps'] = $hasFilters = true;
    if(isset($this->params['url']['chest']))
        if($this->params['url']['chest'] == "1")
            $filters['chest'] = $hasFilters = true;
    if(isset($this->params['url']['forearms']))
        if($this->params['url']['forearms'] == "1")
            $filters['forearms'] = $hasFilters = true;
    if(isset($this->params['url']['legs']))
        if($this->params['url']['legs'] == "1")
            $filters['legs'] = $hasFilters = true;
    if(isset($this->params['url']['shoulders']))
        if($this->params['url']['shoulders'] == "1")
            $filters['shoulders'] = $hasFilters = true;
    if(isset($this->params['url']['triceps']))
        if($this->params['url']['triceps'] == "1")
            $filters['triceps'] = $hasFilters = true;


    $query = $this->request->data['search'];

    //insert code here to actually perform the search!     

}

我真的希望有一个简单的方法来做到这一点,但我整晚都在浏览文档,似乎无法找到任何相关的例子。在EntityFramework中,我可以简单地说:

List<Workout> results = context.Workouts.Where(w => w.Name.Contains(searchTerm) && w.MuscleGroups.Any(g => g.Id == mg_id)).ToList();

注意:不知道C#语法是否接近正确,但是从模型访问数据的难易程度接近于我记忆中的数据。

如何从CakePHP中获得相同的效果?

1 个答案:

答案 0 :(得分:0)

尝试使用MuscleGroup ID进行过滤,而不是使用其名称。我想用户无论如何都会从列表中选择它们。

然后,find()应该像这样工作:

$this->Workout->find(
    'all',
    array(
        'conditions' => array(
            'Workout.name LIKE' => '%'.$searchTerm.'%',
            // List of included MuscleGroups:
            'MuscleGroup.id' => array(1, 5, 9, 17)  
        )   
    )   
);

请注意,您需要注意相关的MuscleGroup包含在Workout查询中,方法是设置$this->Workout->recursive = 1或(推荐)使用更灵活的ContainableBehavior。