Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SoulReaver opened this issue May 15, 2020 · 1 comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@SoulReaver
Copy link

SoulReaver commented May 15, 2020

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
@SoulReaver SoulReaver added the bug Verified issues on the current code behavior or pull requests that will fix them label May 15, 2020
@element-code
Copy link
Contributor

When fixing this, evaluate moving the data simplification outside of update and insert to

  1. reduce duplicates
  2. allow outside use (maybe in a override)

in my case overriding the save method to add a beforeSave event (with the data like beforeUpdate) leads me to copy 32 lines instead of calling something like dryEntityData($data)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants