I'm trying to build an admin interface using ng-admin.
I'm using a reference
field to link one main entity (Post, for example) to a different entity (Comment) that has a many-to-one relationship (one Post has many Comments).
On a listView()
of Posts I must display the last Comment of each Post, but as I can see from the relationships documentation of ng-admin I can't because in my API the foreign key is not on the main entity but on the secondary one.
In other words, I have post_id
in the Comment entity but I am doing a listView()
of Posts so I can't access any Comment.
Is there a workaround for this issue? Can I reference to an external entity from an entity that does not contain the reference key itself?
I'll add a simple JSON to make the example more clear.
// Post
{
id: 1,
title: 'post n1',
body: 'bla bla bla very cool'
}
// Comment
{
id: 1,
body: 'yo bro'
post_id: 1
}
Thank you
From personal experience and looking at the reference it looks like the only to get results similar to what you're looking for are by using a referenced_list
(see http://ng-admin-book.marmelab.com/doc/reference/Field.html#-referenced-list-field-type). The first sentence matches your case exactly:
"The referenced_list type maps a one-to-many relationship where the foreign key is located in another entity."
Using it would then be something like this:
nga.field('comments', 'referenced_list')
.targetEntity(comments)
.targetReferenceField('post_id')
This should get you a list of the comments connected to that post. HOWEVER, since this returns a list, you cannot use it with listView
(you can't nest a list in a list).
Unfortunately I don't think there's any way to use reference
to get a similar result, because reference uses one of the keys of the object to search.