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

Save entity after selected find results in null data #1234

Closed
Martin-4Spaces opened this issue Sep 23, 2018 · 0 comments
Closed

Save entity after selected find results in null data #1234

Martin-4Spaces opened this issue Sep 23, 2018 · 0 comments
Assignees
Milestone

Comments

@Martin-4Spaces
Copy link

I have an User-entity with two properties:

class User extends Entity {
    public $id, $name;
}

And a UserModel

class UserModel extends Model {
    protected $table = 'users';
    protected $returnType = User::class;
    protected $allowedFields = ['id', 'name'];
}

I want to select only some fields.

$userModel = new UserModel();
$user = $userModel
    ->select('id')
    ->find(1);

I do some work on the entity and then I want to save the changes to the database.
$userModel->save($user);
Because of the select, name is empty. The SQL generated is
UPDATE `users` SET `id` = '1', `name` = NULL WHERE `users`.`id` IN ('1')
I have one idea for a fix. Make a copy of the properties in find and store the properties in a backup array in each entity. Then we can compare the backup property with the one to be saved and only save fields that are actually changed. This will however result in more memory usage.

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

No branches or pull requests

2 participants