Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual attributes not being applied #252

Open
ryanalbrecht opened this issue Aug 16, 2024 · 2 comments
Open

Virtual attributes not being applied #252

ryanalbrecht opened this issue Aug 16, 2024 · 2 comments

Comments

@ryanalbrecht
Copy link
Contributor

ryanalbrecht commented Aug 16, 2024

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

.newEntity()

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();
@elpete
Copy link
Collaborator

elpete commented Aug 16, 2024

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

Normally, virtual attributes are provided when building a query.

@ryanalbrecht
Copy link
Contributor Author

ryanalbrecht commented Aug 16, 2024

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

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

No branches or pull requests

2 participants