2

请帮我在 Codeigniter 4 中使用多个条件?

我使用了以下代码,但它不起作用,抛出了一个错误,其中一个像它工作正常。

$users = $model->like('title', $this->request->getVar('search'))->like('name', $this->request->getVar('search'))->get();
 $db  = \Config\Database::connect();
 $user = $db->table('staff');
 $user = $db->like('title', $this->request->getVar('search'));
 $user = $db->or_like('name', $this->request->getVar('search'));
 $query = $user->get();
4

2 回答 2

3

您的第二个代码正在使用or_like,但在 Codeigniter 4 中您应该使用orLikehttps ://codeigniter4.github.io/userguide/database/query_builder.html

我不知道它会抛出什么类型的错误(你应该已经发布它)以及两个代码中哪一个是正确的,但你的代码应该是这样的:

$db = \Config\Database::connect();
$builder = $db->table('staff');
$users = $builder->like('title', $this->request->getVar('search'))->orLike('name', $this->request->getVar('search'))->get();
于 2020-01-17T09:34:00.687 回答
0

这是我自己的做法。

namespace App\Models;

use CodeIgniter\Model;

class SearchData extends Model
{
    protected $table = 'Staff';
    
    /**
     * An array of search result
     * 
     * @var $Keyword string
     *
     * @return array
     */
public function FindStaffMember($Keyword){
$ResponseData="No Record Found!";
//::You could past third parameter
//$this->like('title', 'match', 'before'); // Produces: WHERE `title` LIKE '%match' ESCAPE '!'
//$this->like('title', 'match', 'after');  // Produces: WHERE `title` LIKE 'match%' ESCAPE '!'
//$this->like('title', 'match', 'both');   // Produces: WHERE `title` LIKE '%match%' ESCAPE '!'
        $this->like("Staff_Name", $Keyword);
        $this->orLike("Staff_Id", $Keyword);
        $this->limit(10);
        $this->orderBy("Staff_Name", "ASC");
        //:::::
        $query = $this->get();

//$RecordCounted=count($query->getResultArray());   //::: 
    
    if($query->getResultArray()){
        
        $ResponseData=$query->getResultArray();
    }
    
    return $ResponseData;
}


}
于 2021-02-17T08:16:41.550 回答