Skip to content

Save entity after selected find results in null data #1234

Description

@Martin-4Spaces

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.

Activity

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions