Codeigniter 4 中的多个相似条件
Multiple like conditions in Codeigniter 4
请帮我在 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();
您的第二个代码使用 or_like
但在 Codeigniter 4 中您应该使用 orLike
:
https://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();
这是我自己的方法。
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;
}
}
请帮我在 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();
您的第二个代码使用 or_like
但在 Codeigniter 4 中您应该使用 orLike
:
https://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();
这是我自己的方法。
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;
}
}