检查表codeigniter中是否已存在两个值

I.m trying to check if a value exists in two columns from a table. The column names are on_number and off_number.

I've tried the following in my controller. The check works however for only the off_number columns and not the on_number.

my controller.

 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();
        }
    }

The extends MY_Model has:

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();
    }

I would like it to check both columns if the value exists.

I believe the reason it doesn't work for "off_number" is in this code

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;

That said, I think your code is a hot mess. I say that because to adhere to the MVC pattern a lot of your controller logic should be in the model. IMO, all of it should be.

I would replace the model function getByWhere() with one named check(). It will return true if any record where 'id != $notIdAND'on_number' = $on__number` AND off_number', $off_number. If either of the requred inputs are missing, or if no records are found it returns false.

As you look at the following code it's important to understand that $this->input->get('some_input') will return null if $_GET('some_input') is empty.

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;
    }

}

Then all you need in the controller is

$exists = $this->duty_book_model->check();
echo $exists ? 'false' : 'true';

First of all, you have a typo in 'on_number' => $on__number, You have doubled underscore in $on__number

The second thing - the reason why your check works only for off_number is because you use $exist variable in both cases. Doesn't matter what check result will be for on_number, because it always will be rewritten by off_number checks.

One way you can solve those is by using two different variables:

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';

It's not the best solution, but it must be most clear.