Laravel Nova: Query params empty for /nova-api/installations/relate-authorization in hasMany relationship - laravel-nova

I have defined a HasMany relationsship in my Nova resource. Nova fires two requests when trying to load the children in the parent detail view:
1. GET /nova-api/childResources?viaResource=parentResources&viaResourceId=4711&viaRelationship=childResources&relationshipType=hasMany
-> This correctly returns the related records.
2. GET /nova-api/childResources/relate-authorization?viaResource=parentResources&viaResourceId=4711&viaRelationship=childResources&relationshipType=hasMany
-> This fails with a HTTP 500 returning Class name must be a valid object or a string.
Digging deeper into the underlying NovaRequest, I found out that all query params are gone inside the request. This is why viaResource cannot be used to instantiate a class and to above error occurs.
I have no idea, what could cause this.

Turned out, the problem originated from an incomplete nginx configuration. I wasn’t passing the query string to PHP.

Related

How to find the distinct error messages of model state validation failure in asp.net mvc

I want to filter the Error Messages that gets populated as part of data annotation modelstate validation failure. As in if an array of objects comes as a part of class, and the validation fails for more than one object, I do not want the same message to be added again and again. Instead, I want to find the distinct error messages
string ValidationFailure= string.Join(";", actionContext.ModelState.Values.Distinct().Select(x.ErrorMessage));
But not able to get the required output.
It looks like your attempt is close, but you’re using Distinct on something that’s already unique (Values). Instead, try the following variation:
string ValidationFailure = string.Join(";", actionContext.ModelState.Values.Select(x => x.ErrorMessage).Distinct());
This ensures that you get a distinct list of ErrorMessages.

Restkit - Core Data One-To-One Relationship Mapping via Foreign Key

I have spent the last few hours trying a number of different solutions to this problem by looking at similar similar problems that have been resolved on StackOverFlow threads.
However, I have hit a brick wall.
Some background.
Using Restkit 0.22 and Core Data to Persist 2 Objects and set up a relationship between them.
The two Objects that I am modelling are as follows, Relationships are also included in the form of a One-One relationship between COAgendaItem and COMapItemLocation (The relationship that isn't working at present) and a One-Many between COMapItemLocation and COAgendaItem (I have not looked at mapping this yet):
-- COAgendaItem
-- COMapItemLocation
They have the Corresponding Attributes and Relationships
The desired result is to have the core data relationship working alongside RestKit, i.e. The property agendaItemLocation accessible via code on a COAgendaItem object.
The JSON Response from the Server looks like this
The above JSON Response is the data required for a COAgendaItem object plus the primary key of a COMapItemLocation Object. I am having a problem mapping the location_id field which is the primary key of a COMapItemLocation. (I have the Response Descriptors and the retrieval code set up and it is working correctly, it has only become a problem since I attempted to map a relationship)
So far I have modified my RKEntityMapping to try and map the relationship.
+ (RKMapping *)agendaItemMapping{
RKEntityMapping *agendaItemMapping = [RKEntityMapping mappingForEntityForName:#"COAgendaItem" inManagedObjectStore:[[COPersistenceCoordinator sharedCOPersistenceCoordinator]restKitObjectStore]];
[agendaItemMapping addAttributeMappingsFromDictionary:#{#"id": #"agendaItemID",#"title": #"agendaItemTitle",#"description": #"agendaItemDescription",#"start": #"agendaItemStartTime",#"end": #"agendaItemEndTime",#"category": #"agendaItemCategory",#"location_id" : #"agendaItemLocation"}];
[agendaItemMapping addConnectionForRelationship:#"agendaItemLocation" connectedBy:#"location_id"];
return agendaItemMapping;
}
However this causes a crash on launch as shown below (Note, I have added the #"location_id" : #"agendaItemLocation" mapping for the foreign key in the default mapping as well, is this necessary)
I have tried modifying the [agendaItemMapping addConnectionForRelationship:#"agendaItemLocation" connectedBy:#"location_id"];
Statement with all the combinations I can think of both in single string and dictionary formats i.e. #{"agendaItemLocation" : #"location_id"} but whatever I try, it always ends up in the same error as before 'Cannot connect relationship: invalid attributes given for source entity 'COAgendaItem': location_id'.
I am completely stumped on this one, can anyone point anything that I have done wrong or need to do differently?
Thanks greatly for any help in advance! :)
You describe 2 different relationships, but really you just have one with an inverse. This is correct, it's just your description which is wrong (and thus probably your understanding of setting the relationship contents).
The connection cannot be made (and you get an error) because RestKit needs to know the id of the other object to find (to connect to). You're telling it to use location_id, but it needs to be a property on the class (usually transient), not an item in the JSON (because the connection is made after the JSON has been processed and gone).
Add a property to the entity and a mapping to place the location_id into it. Update the connection with the name of that property.

Grails searchable returns domain but relation is null

I have 3 domains: A, B, C.
A and C have many-to-many relationship through B.
A and C are searchable.
When I search and get list of A domain all the fields in A are accessible, however relation field is always 'null'. Why I can't access relational field? Why do I get 'null'? If I access object directly, I see a relation, but when searchable returns A domain, I get 'null' on a relation field.
P.S: I tried to make B searchable but it looks like searchable having issue with indexing it on top of that I don't see any point in indexing B since it exists for the sake of many-to-many relationship only.
Please I need help with it. I need to be able to get to C via A on a searchable return.
Thank you.
[Update]: I made a prototype project today (small DB) and made domain B searchable. Now I can access relational field. However in my real project, I don't want to make B searchable since database is big and indexing takes too long. Is there a way around it?
You can refrash all instance in result list or use reload:true property for seach method
searchableService.search(query,[reload:true])
You can find full list of options: http://grails.org/Searchable+Plugin+-+Methods+-+search
reload - If true, reloads the objects from the database, attaching them to a Hibernate session, otherwise the objects are reconstructed from the index. Default is false
Ok, I believe I solved my issue.
First checkout link to a similar question: Grails searchable plugin -- Please give GalmWing some credit.
My solution is following, I am implementing my own controller and following piece of code implements searchable service call:
if (params.q) {
try{
def searchResults = searchableService.search(params.q, params)
searchResults.results.each {
it.refresh()
}
return [carInstanceList:searchResults.results, carInstanceTotal:searchResults.total]
} catch (SearchEngineQueryParseException ex) {
return [parseException: true]
}
As you can see I have a loop where on each item of domain "A" I do refresh. Now refresh gets real record from DB with all the links. Now I return list into GSP and it extracts all of "C" domains associated with the "A" domain.
Now this way you might get performance penalty, but in my case searchable is actually not able to index "B" domain, it is working for a while and then crashes, so I don't have another choice, at least for now.

grails removeFrom removes only one at a time

I have a grails application where I have contacts which belongs to another domain contactGroup. It all seems to be working fine except for removeFromContacts method. I am using following code. The code works correctly but removes only one contact from the group at a time. I even did some debugging and the foreach loop runs as many times as the contacts provided. There is no error message. Any idea what could be going wrong -
ContactGroup group = ContactGroup.findByIdAndOwner(params.groupId, user)
def contactIds = request.JSON.data.contact
contactIds.each {
Contact contact = Contact.findByContactIdAndOwner(it.contactId, user)
if(contact) {
group.removeFromContacts(contact)
}
}
I've read a few things about the findAll methods loading proxies if the associations are lazy-loaded rather than the "real" instance.
Try this:
group.removeFromContacts(Contact.get(contact.id))
The 'get' should bypass the proxies and use the "real" instance. There is a JIRA that talks about this (Grails-5804). An overall fix according to the JIRA (from Burt Beckwith) is to implement the equals and hashCode method in your Contact class.
Thanks for all your support. I realized that I have not defined the relationship at the domain level correctly and that was messing up with the whole thing. When I corrected that it was working correctly.
saurabh

Lazy fetching of objects using FindAllBy , for the first time

When I use criteria queries, the result contains array list of lazy initialized objects. that is, the list has values with handler org.codehaus.groovy.grails.orm.hibernate.proxy.GroovyAwareJavassistLazyInitializer.
This prevent me from doing any array operation (minus, remove etc) in it. When I use, GORM methods, I get array list of actual object types. How can I get the actual objects in criteria query?
The code is listed below.
availableTypes = Type.withCriteria() {
'in'("roleFrom", from)
'in'("roleTo", to)
}
availableTypes (an array list) has one value , but not actual object but value with a handler of GroovyAwareJavassistLazyInitializer
availableTypes (an array list) has values with type Type
availableTypes = Type.findByRoleFrom(from)
---------- Update ----------
I did further troubleshooting, and this is what I found. Probably the above description might be misleading, but I kept it in case it helps.
When using findAllBy for the first time, I get proxy objects rather than the actual instance. Then, I invoke the method through an ajax call, the actual instance is loaded (anything to do with cache loading??). When I refresh the page, it again loads the proxy
def typeFrom = Type.findAllByParty(partyFrom)
there is another use of findAllBy in the same method, which always returns actual instances.
def relFrom = Relation.findAllByParty(partyFrom)
When compared the two classes, the attribute 'party' of class Roles is part of a 1-m relation. like
class Role {
RoleType roleType
LocalDate validFrom
LocalDate validTo
static belongsTo = [party : Party ]
...
}
I know if I do statement like Party.findAll(), the role instances would be proxy till they access. But, when using gorm directly on the class (Role), why I am getting the proxy objects ???
thanks for the help.
thanks.
Turns out are a couple of possible solutions which I came across but didn't try, such as
Overloading the equals method so that the proxy and the domain
object use a primary key instead of the hashCode for equality
Using a join query so that you get actual instances back and not proxies
GrailsHibernateUtil.unwrapProxy(o)
HibernateProxyHelper.getClassWithoutInitializingProxy(object)
One solution that worked for me was to specify lazy loading to be false in the domain object mapping.
History of this problem seems to be discussed here: GRAILS-4614
See also: eager load

Resources