Skip to content

Bug: Model::Insert() does not throw exception when object without data is passed as parameter #2998

Description

@SoulReaver

Describe the bug
Model::insert() throws exception when empty table is passed as parameter. It does not throw one when "empty object" is passed as parameter. For example, if I create a new Entity object and pass it as parameter, it won't throw exception while there is still no data to insert.

CodeIgniter 4 version
4.03

Affected module(s)
Codeigniter\Model

Expected behavior, and steps to reproduce if appropriate
Right after object is turned into array and before validation, that array should be rechecked for being empty or whole emptiness checking should be moved to that point in code.

@ -674,11 +674,6 @@ class Model
			$this->tempData = [];
		}

-		if (empty($data))
-		{
-			throw DataException::forEmptyDataset('insert');
-		}

		// If $data is using a custom class with public or protected
		// properties representing the table elements, we need to grab
		// them as an array.
@ -695,6 +690,11 @@ class Model
			$data = (array) $data;
		}

+		if (empty($data))
+		{
+			throw DataException::forEmptyDataset('insert');
+		}

		// Validate data before saving.
		if ($this->skipValidation === false)
		{

Context

  • OS: Windows 10
  • Web server: Apache 2.4.41
  • PHP version: 7.3.12

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions