[英]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.