quick icon indicating copy to clipboard operation
quick copied to clipboard

Virtual attributes not being applied

Open ryanalbrecht opened this issue 1 year ago • 2 comments

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();

ryanalbrecht avatar Aug 16 '24 14:08 ryanalbrecht

Can you include an example of how you are fetching the entity?

Normally, virtual attributes are provided when building a query.

elpete avatar Aug 16 '24 16:08 elpete

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" );
}

ryanalbrecht avatar Aug 16 '24 20:08 ryanalbrecht

Two things:

  1. 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

  2. You can simplify your code with the withCount helper. https://quick.ortusbooks.com/guide/relationships/relationship-counts#withcount

elpete avatar Jun 11 '25 16:06 elpete

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

ryanalbrecht avatar Jun 11 '25 19:06 ryanalbrecht