Skip to content

Fix isHydrated() returning false with typed public properties#33

Open
enlivenapp wants to merge 2 commits intoflightphp:masterfrom
enlivenapp:fix/isHydrated-typed-properties
Open

Fix isHydrated() returning false with typed public properties#33
enlivenapp wants to merge 2 commits intoflightphp:masterfrom
enlivenapp:fix/isHydrated-typed-properties

Conversation

@enlivenapp
Copy link
Copy Markdown
Contributor

I created a subclass with typed public properties:

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

After calling find() on an existing record:

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

I expected isHydrated() to return true, since the property values are populated correctly ($user->name returns "John").

Instead, isHydrated() returns false. This causes patterns like the following to always return null:

$user->eq('id', $id)->find();
return $user->isHydrated() ? $user : null;

Subclasses without typed properties are not affected.

Tests included. All existing tests continue to pass.

@enlivenapp enlivenapp force-pushed the fix/isHydrated-typed-properties branch from b48e22b to 36e76f5 Compare April 24, 2026 01:13
@n0nag0n
Copy link
Copy Markdown
Contributor

n0nag0n commented Apr 24, 2026

This fix might be at odds with your other PR that I just merged. Want to resolve the conflicts and let me know? Thanks!

When a subclass declares typed public properties (e.g. public int $id),
PDO::FETCH_INTO sets them directly, bypassing __set(). This leaves the
internal $data array empty, causing isHydrated() to always return false
after find() and findAll().

Added syncDeclaredProperties() which detects initialized declared
properties and copies them into $data after fetch operations.

enlivenapp <mw@enlivenapp.com>
lastInsertId() returns a string, which throws a TypeError when assigned
to a typed public int property under strict_types. Cast the value to int
when the primary key property has an int type declaration.

enlivenapp <mw@enlivenapp.com>
@enlivenapp enlivenapp force-pushed the fix/isHydrated-typed-properties branch from 36e76f5 to ea2b0eb Compare April 24, 2026 15:31
@enlivenapp
Copy link
Copy Markdown
Contributor Author

It was indeed. Apologies for the messy PR business. in the attempt to keep things separate so it'd be easier for you to pick what you wanted to use I did too much separation. This is now a 33/34 combo merged together (since 33 needed 34 to work). rebased, etc etc...

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