[英]check if two values already exist in a table codeigniter
我試圖檢查值是否存在於表的兩列中。 列名是on_number和off_number。
我已經在控制器中嘗試了以下方法。 但是,該檢查僅對off_number列有效,而對on_number不起作用。
我的控制器。
public function check()
{
$on_number = !empty(get('on_number')) ? get('on_number') : false;
$notId = !empty($this->input->get('notId')) ? $this->input->get('notId') : 0;
if($on_number)
$exists = count($this->duty_book_model->getByWhere([
'on_number' => $on__number,
'id !=' => $notId,
])) > 0 ? true : false;
if($on_number)
$exists = count($this->duty_book_model->getByWhere([
'off_number' => $on_number,
'id !=' => $notId,
])) > 0 ? true : false;
echo $exists ? 'false' : 'true';
}
My Model
class Duty_book_model extends MY_Model {
public $table = 'tbl_duty_type';
public function __construct()
{
parent::__construct();
}
}
擴展MY_Model具有:
public function getByWhere($whereArg, $args = [])
{
if(isset($args['order']))
$this->db->order_by($args['order'][0], $args['order'][1]);
return $this->db->get_where($this->table, $whereArg)->result();
}
我希望檢查值是否存在的兩列。
首先,您在'on_number' => $on__number,
有一個錯字,在'on_number' => $on__number,
您的下划線$on__number
第二件事-檢查僅適用於off_number
的原因是因為在兩種情況下都使用$exist
變量。 沒關系on_number
檢查結果是on_number
,因為它總是會被off_number
檢查重寫。
解決這些問題的一種方法是使用兩個不同的變量:
if($on_number){
$exists_on = count($this->duty_book_model->getByWhere([
'on_number' => $on_number,
'id !=' => $notId,
])) > 0 ? true : false;
$exists_off = count($this->duty_book_model->getByWhere([
'off_number' => $on_number,
'id !=' => $notId,
])) > 0 ? true : false;
}
echo (($exists_on===true)&&(exists_off===true)) ? 'false' : 'true';
這不是最佳解決方案,但必須最清楚。
我相信這段代碼不適用於“ off_number”的原因
if($on_number)
$exists = count($this->duty_book_model->getByWhere([
//THE NEXT LINE IS THE PROBLEM - LOOK AT THE VALUE!!!
'off_number' => $on_number, //value should be $off_number - right?
'id !=' => $notId,])) > 0 ? true : false;
就是說,我認為您的代碼是一團糟。 我說這是因為要遵循MVC模式,因此模型中應該包含許多控制器邏輯。 海事組織,所有這些都應該是。
我將模型函數getByWhere()
替換為一個名為check()
。 這將返回true,如果任何記錄,其中'id
!= $ notId AND
'on_number'= $ on__number`和off_number',$ off_number。 如果缺少任何一個必需的輸入,或者找不到任何記錄,則返回false。
當您看下面的代碼時,很重要的一點是,如果$_GET('some_input')
為空,則$this->input->get('some_input')
將返回null。
class Duty_book_model extends MY_Model
{
public $table = 'tbl_duty_type';
public function check()
{
$on_number = $this->input->get('on_number');
$notId = $this->input->get('notId');
if(empty($on_number) || empty($notId))
{
return false;
}
$query = $this->db
->select('id') //could be any field
->where('id !=', $notId)
->where('on_number', $on__number)
->where('off_number', $off_number)
->get($this->table);
//given $notId = 111 AND $on_number = 222 AND $off_number = 333
//produces the query string
//SELECT `id` FROM `tbl_duty_type` WHERE `id` != 111 AND `on_number` = 222 AND `off_number` = 333
if($query) //might be false if the query string fails to work
{
return $query->num_rows > 0; //returns boolean
}
return false;
}
}
然后,您在控制器中所需的就是
$exists = $this->duty_book_model->check();
echo $exists ? 'false' : 'true';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.