Skip to content

How to use initialSorting feature in DataGrid with oneHasMany field? #748

Answered by jonasnobile
kausik10 asked this question in Support
Discussion options

You must be logged in to vote

Hi,

Contember doesn’t support aggregate fields directly at this time, so to sort Records by the number of files they contain, you’ll need to calculate the file count separately. One effective approach is to use a custom SQL view. This view will compute the count of files for each record, which you can then reference in your model.

For example, you could modify your models as follows:

export class Record {
	// ... other fields
	files = c.oneHasMany(File, 'record')
	stats = c.oneHasOneInverse(RecordStats, 'record').notNull()
}

@c.View(`
	SELECT
		-- Data binding requires an id for each entity; here we use Record's id to keep it consistent.
		r.id AS id,
		(SELECT count(*) FROM files WHERE …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kausik10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants