Skip to content

Fix insert() and update() ignoring typed public properties#35

Merged
n0nag0n merged 1 commit into
flightphp:masterfrom
enlivenapp:fix/dirty-typed-properties
Apr 24, 2026
Merged

Fix insert() and update() ignoring typed public properties#35
n0nag0n merged 1 commit into
flightphp:masterfrom
enlivenapp:fix/dirty-typed-properties

Conversation

@enlivenapp
Copy link
Copy Markdown
Contributor

I created a subclass with typed properties and attempted an insert:

class User extends \flight\ActiveRecord {
    public function __construct($pdo = null) {
        parent::__construct($pdo, 'users');
    }
    public int $id;
    public string $name;
}

$user = new User($pdo);
$user->name = 'John';
$user->insert();

I expected a new row to be inserted into the database.

Instead, nothing is inserted — the method returns early because $dirty is empty.

The same issue affects update() after find():

$user = new User($pdo);
$user->eq('id', 1)->find();
$user->name = 'Updated';
$user->save();

I expected the name to be updated to "Updated". Instead, no update is issued.

Subclasses without typed properties are not affected.

Tests included. All existing tests continue to pass.

Direct assignment to typed public properties bypasses __set(), so the
internal $dirty array stays empty. This causes insert() to return early
with nothing inserted, and update() to issue no UPDATE statement.

Added syncDirtyFromProperties() which detects initialized declared
properties and copies them into $dirty. For insert, all properties are
synced. For update, only properties whose value differs from the last
find() are synced.

enlivenapp <mw@enlivenapp.com>
@n0nag0n n0nag0n merged commit 01c2372 into flightphp:master Apr 24, 2026
@n0nag0n
Copy link
Copy Markdown
Contributor

n0nag0n commented Apr 24, 2026

Thanks for this! I just have used comment attributes in the past (when using Runway to generate a record for example), but this works too.

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

Successfully merging this pull request may close these issues.

2 participants