how to use cakedc/search plugin for searching across 3 different tables with 1 search bar?

大城市里の小女人 提交于 2019-12-11 19:39:29

问题


I am using CakePHP2.4 and the search plugin https://github.com/CakeDC/search

I have the following

Employee hasOne EmployeeProfile 
Employee hasMany Qualification

So I have a single search bar.

the search bar will search using LIKE through the following fields

Employee.name
EmployeeProfile.email
Qualification.title

how do I configure the model Employee->filterArgs for this search?

This is a cross-posting of the original issue here


回答1:


The documentation includes an example.

'username' => array('type' => 'like', 'field' => array('User.username', 'UserInfo.first_name')),

You just have to make sure the models you're calling are available in your find() call. In the example the find will do a like on the users table username and on the user_infos first_name field at the same time.




回答2:


I'd like to expand on this as I've been trying to setup a search on a hasMany relationship for a few hours and couldn't find anything. Mark mentionned "custom bindModel (as hasOne) for hasMany relationship (Qualification)". Here's how it's done :

    $this->Employee->bindModel(array(
        'hasOne' => array(
            'Qualification' => array(
                'foreignKey' => false,
                'conditions' => array('Employee.id = Qualification.employee_id')
            )
        )
    ), false);

Just bind it before your paginate and add Qualification.title in your field list in your filterArgs



来源:https://stackoverflow.com/questions/17907146/how-to-use-cakedc-search-plugin-for-searching-across-3-different-tables-with-1-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!