如何在cakephp中将数据库检索到的数据与输入值匹配

如何在cakephp中将数据库检索到的数据与输入值匹配,php,mysql,regex,cakephp,Php,Mysql,Regex,Cakephp,我希望在数据库表中搜索与输入值匹配的数据,当找到该数据时,我希望选择匹配计数最高的数据并将其保存在字段中,我使用的是cakephp。 以下是我迄今为止所做的工作: public function add() { if ($this->request->is('post')) { $data = $this->Code->find('all',array( 'conditions'=>array('Model.

我希望在数据库表中搜索与输入值匹配的数据,当找到该数据时,我希望选择匹配计数最高的数据并将其保存在字段中,我使用的是cakephp。 以下是我迄今为止所做的工作:

public function add() {
    if ($this->request->is('post')) {

            $data = $this->Code->find('all',array(
            'conditions'=>array('Model.is_expired'=>0),
            'fields'=>array('Model.words'),

            ));

            $str = $this->request->data['Model']['words'] ;//the input value
            $sort_array=array();
            foreach($data as $value){
            $num = similar_text($value, $str);
            $sort_array[$value] = $num;
            }
         arsort($sort_array, SORT_REGULAR);
         $res = array_slice($sort_array,0,1);
         foreach($res as $key=>$value){
               return $key;
         }
            $this->request->data['Model']['invitee_1'] = $key;              
        }
        $this->Model->create();
        if ($this->Model->save($this->request->data)) {
            $this->Session->setFlash(__('The Model has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The Model could not be saved. Please, try again.'));
        }
    }

}
表格字段包括: id |单词|新词|


我想在单词字段中搜索与表单输入值匹配的单词,然后选择匹配计数最高的单词,最后将其保存在表单输入旁边的新单词字段中,表单输入将保存在单词字段中。

where id similate\u text function?我不明白u2460470。我使用similate\u text function来匹配这两个值。很难读取代码,因为它没有以可读的方式缩进-据我所知,如果有,在准备步骤中返回$res,在这种情况下,您没有达到保存状态,而且您还需要$this->request->data['Model']['invitee_1'],但invitee_1不是表中的字段