Virtual attributes not being applied
I believe there is an issues with virtual attributes not working at all.
When loading an entity, quick will create a new instance of the entity then populate the data and return it. When it creates this new entity the virtual attributes are not persisted.
QuickBuilder.cfc
public any function loadEntity( required struct data ) {
....
return getEntity()
//this is the issue, the virtual attributes from the orginal entity are not persisted
.newEntity()
.assignAttributesData( arguments.data )
.assignOriginalAttributes( arguments.data )
.markLoaded();
....
}
https://github.com/coldbox-modules/quick/blob/4ce7d6d791a733adc82f932883afa2866f1dab97/models/QuickBuilder.cfc#L1327
This is my work around for now
var newEnt = getEntity().newEntity();
// add any virtual attributes present in the original entity to new entity
getEntity()
.get_virtualAttributes()
.each( function( item ) {
newEnt.appendVirtualAttribute( item );
} );
return newEnt
.assignAttributesData( arguments.data )
.assignOriginalAttributes( arguments.data )
.markLoaded();
Can you include an example of how you are fetching the entity?
Normally, virtual attributes are provided when building a query.
Sure. I load my entity like this.
prc.batches = getInstance('InventoryBatch')
.limit(100)
.addTradeCount()
.orderBy("batchDate DESC")
.get();
and the addTradeCount scope looks like this
function scopeAddTradeCount(qb){
qb.selectRaw(" (SELECT count(id) FROM trade WHERE trade.inventoryBatchId = inventory_batch.id) as tradeCount");
appendVirtualAttribute( "tradeCount" );
}
Two things:
-
I'm not able to reproduce this. I added a test with the data you provided and it passed without changes: https://github.com/coldbox-modules/quick/tree/252
-
You can simplify your code with the
withCounthelper. https://quick.ortusbooks.com/guide/relationships/relationship-counts#withcount
Thanks, I think was fixed a while ago and I neglected to close this issue.
https://github.com/coldbox-modules/quick/commit/4ae80fae95d2b40461ad7b23306b4111f785ea40#diff-966057e45f9a6ddc0a36e98de11a4ec28fd247c6d4c9d63cb0d007fd5716bddaR1314