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: Entity's original is not set #3370

Closed
haugli92 opened this issue Jul 21, 2020 · 2 comments
Closed

Bug: Entity's original is not set #3370

haugli92 opened this issue Jul 21, 2020 · 2 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@haugli92
Copy link

Describe the bug
\CodeIgniter\Entity class does not set original's when initialized with data as a parameter to __construct function.

CodeIgniter 4 version
4.0.4

Affected module(s)
\CodeIgniter\Entity

Expected behavior, and steps to reproduce if appropriate
When initializing a new instance of Entity I assume that attributes:protected and original:protected should be set. Otherwise i cant use functions like hasChanged().

Code:

$entity = new Entity(['username' => 'Haugli92']);

print_r($entity);

Result:

App\Entities\Entity Object
(
    [datamap:protected] => Array
        (
        )

    [dates:protected] => Array
        (
            [0] => created_at
            [1] => updated_at
            [2] => deleted_at
        )

    [casts:protected] => Array
        (
        )

    [attributes:protected] => Array
        (
            [username] => Haugli92
        )

    [original:protected] => Array
        (
        )

    [_cast:CodeIgniter\Entity:private] => 1
)

Context

  • OS: Ubuntu 18.04.4
  • Web server Nginx nginx/1.14.0
  • PHP version PHP 7.3.16
@haugli92 haugli92 added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 21, 2020
@haugli92
Copy link
Author

By switching positions of syncOriginal() and fill() in the __construct function of Entity-class seems to solve the problem.

/**
 * Allows filling in Entity parameters during construction.
 *
 * @param array|null $data
 */
public function __construct(array $data = null)
{
	$this->fill($data);
	$this->syncOriginal();
}

Could this have been a solution?

@michalsn
Copy link
Member

This is not a bug. Some developers may want to set default values for attributes inside the Entity class.

In your case just use syncOriginal() after instantiating Entity class. It's a public method for a reason.

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

2 participants