Skip to content

Commit 4582c44

Browse files
committed
fix(refresh): hydrate projected rows with aliased primary keys
1 parent 3ea3300 commit 4582c44

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎models/BaseEntity.cfc‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,12 @@ component accessors="true" {
15271527
if ( !isStruct( freshData ) || structKeyExists( freshData, "isQuickEntity" ) ) {
15281528
return freshData;
15291529
}
1530-
var entity = newEntity().hydrate( freshData );
1530+
// Snapshot rows use database column names, while hydrate() requires aliased keys.
1531+
// Use the same database-row hydration path as QuickBuilder.loadEntity().
1532+
var entity = newEntity()
1533+
.assignAttributesData( freshData )
1534+
.assignOriginalAttributes( freshData )
1535+
.markLoaded();
15311536
return hasRefreshQuery ? entity.set_refreshQuery( variables._refreshQuery ) : entity;
15321537
}
15331538

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
component
2+
extends ="quick.models.BaseEntity"
3+
table ="users"
4+
key ="userID"
5+
accessors="true"
6+
{
7+
8+
property name="userID" column="id";
9+
property name="username";
10+
property name="firstName" column="first_name";
11+
property name="lastName" column="last_name";
12+
13+
function scopeWithFullName( qb ) {
14+
qb.appendVirtualAttribute( "fullName" ).selectRaw( "CONCAT(first_name, ' ', last_name) AS fullName" );
15+
}
16+
17+
}

‎tests/specs/integration/BaseEntity/RefreshSnapshotSpec.cfc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ component extends="tests.resources.ModuleIntegrationSpec" {
22

33
function run() {
44
describe( "Result-set refresh snapshots", function() {
5+
it( "retrieves fresh projected entities with aliased primary keys", function() {
6+
var user = getInstance( "AliasedPrimaryKeyUser" ).withFullName().findOrFail( 1 );
7+
queryExecute( "UPDATE `users` SET `first_name` = ? WHERE `id` = ?", [ "Aliased", 1 ] );
8+
9+
var fresh = user.fresh();
10+
expect( fresh.getUserID() ).toBe( 1 );
11+
expect( fresh.getFullName() ).toBe( "Aliased Peterson" );
12+
expect( fresh.isLoaded() ).toBeTrue();
13+
expect( fresh.isDirty() ).toBeFalse();
14+
expect( fresh.get_refreshQuery() ).toBeSameInstanceAs( user.get_refreshQuery() );
15+
expect( fresh.fresh().getUserID() ).toBe( 1 );
16+
expect( user.getFirstName() ).toBe( "Eric" );
17+
} );
18+
519
it( "shares an isolated projection snapshot across get results", function() {
620
assertSharedSnapshot( "get" );
721
} );

0 commit comments

Comments
 (0)