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

Calling update() with an Entity throws an error #4143

Closed
luispastendev opened this issue Jan 21, 2021 · 4 comments · Fixed by #4145
Closed

Calling update() with an Entity throws an error #4143

luispastendev opened this issue Jan 21, 2021 · 4 comments · Fixed by #4145
Labels
documentation Pull requests for documentation only

Comments

@luispastendev
Copy link

Describe the bug
suppose I have the following example:

<input type="hidden" name="id" value=<?=$data->id?>>
<input type="checkbox" name="active" <?=$data->active ? 'checked' : ''?> >

If the checkbox is not activated, no key arrives in the request so I assign a default value in an entity

class Test extends Entity
{
    protected $attributes = [
        'active' => 0,
    ];

	protected function setActive(string $val){
        if($val == 'on'){
            $this->attributes['active'] = 1;
        }
        return $this;
    }
}

Now I fill the entity and proceed to save the change, if the value is set active it works perfectly but when no value is sent and it is about saving the default value 0 of the entity it sends the following error

// controller
$test = new Test($this->request->getPost());
$model = model('TestModel');
$model->update($test->id, $test);

imagen

I have solved it using it in the following way but with the update() method as shown in the documentation that problem is found

$model->where('id', $test->id)
			->set(['active' => $test->active])
			->update();

Thank you very much team!

CodeIgniter 4 version
dev develop

Affected module(s)
model update()

Expected behavior, and steps to reproduce if appropriate
The registry must be updated when you use entities with default values

Context

  • OS: Windows 10
  • Web server: apache 2.4.35
  • PHP version 7.2
@luispastendev luispastendev added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 21, 2021
@luispastendev luispastendev changed the title Bug: Bug: update($id, $fields) throws an error Jan 21, 2021
@michalsn
Copy link
Member

This is happening because on Model::update() we grab only these values from the Entity that have changed.

I think that this is the desired design for this method, so I would suggest making small changes to your view.

<input type="hidden" name="id" value=<?=$data->id?>>
<input type="hidden" name="active" value="0">
<input type="checkbox" name="active" value="1" <?=$data->active ? 'checked' : ''?> >
class Test extends Entity
{
    protected $attributes = [
        'active' => null,
    ];
}

This way, the active field will always have a value.

@luispastendev
Copy link
Author

@michalsn It works but if I change the position of the html suppose that I invert the lines it would stop working correctly because they are repeated names, it takes the last value to be found within the html

I understand how update works internally, I think it created confusion when using it in this type of case, it would be a good idea maybe to add to the documentation that only updates the fields that detect a change in the case of using entities. or if the user prefers, specify explicitly with set()

thanks for answering!

@michalsn
Copy link
Member

@luispastendev Yes, the correct order for inputs is necessary to do the trick here.

It's not the only way to deal with it, I guess calling toArray() on your Entity before calling an update() method would also work.

I see we're already mentioning in the user guide that Entities are working this way but only for Model::save(). I guess adding a note about insert() and update() will be a good idea.

@michalsn michalsn added documentation Pull requests for documentation only and removed bug Verified issues on the current code behavior or pull requests that will fix them labels Jan 21, 2021
@michalsn michalsn mentioned this issue Jan 21, 2021
1 task
@michalsn michalsn changed the title Bug: update($id, $fields) throws an error Calling update() with an Entity throws an error Jan 21, 2021
@luispastendev
Copy link
Author

thank you very much michalsn :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Pull requests for documentation only
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants