CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

Form Validation is_unique always returning FALSE from isset($this->CI->db) even when database initalized

Open corbywenzelbach opened this issue 5 years ago • 1 comments

CI: 3.1.10 (appears to still be the same code in current release however) PHP: 7.4.2 Using wiredesignz HMVC

I was running into an error with the is_unique rule always returning false. With my setup the database is always loaded, however when I researched this bug the majority of the answers were resolved with loading the database.

I ran a test and noticed that the code inside the is_unique function:

return isset($this->CI->db)
			? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
			: FALSE;

isset($this->CI->db) This line always evaluates to false, even when there is an activate database connection.

I ran some tests and the code is working fine without the if statement checking the DB connection, i confirmed by running $this->db->last_query().

I extended the library and fixed the issue by just doing it inline return – for my case I don't need to check for an active DB connection, but obviously the current method of doing isset() is not working correctly with my setup, and perhaps for others as well:

public function is_unique($str, $field)
	{
		sscanf($field, '%[^.].%[^.]', $table, $field);
		return $this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0;
      }

I noticed in CI version 2.x this DB check never existed for this validation rule, I encountered this bug during an upgrade from 2.x to 3.x

corbywenzelbach avatar Sep 17 '21 17:09 corbywenzelbach

Using wiredesignz HMVC

Report this to that project then. I know this may seem unhelpful, but we aren't responsible for something not working in third-party addons.

narfbg avatar Oct 07 '21 22:10 narfbg