Issues accessing properties of a joined class

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Issues accessing properties of a joined class

lpvoba
This post was updated on .
Here is how my classes are joined:

ClassA has many ClassB
ClassB belongs to ClassA

ClassB also belongs to ClassC
ClassC has many ClassB

ClassA is not joined to ClassC

My issue is, from ClassA's show view, if I iterate through all of its joined ClassB records, can I access ClassC properties in the same view? This is what I had in mind:

ClassA class {
   static hasMany = [classBProperty: ClassB]
   def classAProperty
}

ClassB class {
   static belongsTo = [classAProperty: ClassA, classCProperty: ClassC]
   def classBProperty
}

ClassC class {
   static hasMany = [classBProperty: ClassB]
   def classCProperty
}

ClassAShow.gsp {
   <g:if test="${ClassBInstance?.id}">
      <g:each in="${this}>
         <p>Data from the ClassC record that ClassB belongs to: </p>
         ${ClassBInstance?.someWayToAccessClassAProperty}
      </g:each>
   </g:if>
}

Sorry if this is confusing, please let me know if I can clarify. Any help is appreciated!

~Lan
Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

Jeff Brown-4

On Nov 5, 2012, at 9:07 AM, lpvoba <[hidden email]> wrote:

> Here is how my classes are joined:
>
> ClassA has many ClassB
> ClassB belongs to ClassA
>
> ClassB /also /belongs to ClassC
> ClassC has many ClassB
>
> ClassA is not joined to ClassC
>
> My issue is, from ClassA's show view, if I iterate through all of its joined
> ClassB records, can I access ClassC properties in the same view? This is
> what I had in mind:
>
> ClassA class {
>   static hasMany = [classBProperty: ClassB]
>   def classAProperty
> }
>
> ClassB class {
>   static belongsTo = [classAProperty: ClassA, classCProperty: ClassC]
>   def classBProperty
> }
>
> ClassC class {
>   static hasMany = [classBProperty: ClassB]
>   def classCProperty
> }
>
> ClassAShow.gsp {
>   <g:if test="${ClassBInstance?.id}">
>      <g:each in="${this}>
>         <p>Data from the ClassC record that ClassB belongs to: </p>
>         *${ClassBInstance?.someWayToAccessClassCProperty}*
>      </g:each>
>   </g:if>
> }
>
> Sorry if this is confusing, please let me know if I can clarify. Any help is
> appreciated!
>
> ~Lan
>
>

The answer to your question is "yes", but the code you have in your GSP to do that is a bit funky.  Your code makes no mention of ClassA so it looks like you just want to navigate from a ClassB instance to a ClassC instance.  I don't know what you expect "this" to be in the GSP but it probably will not be what you expect it to be.  If you have a variable called classBInstance in the model, then in the GSP you can refer to classBInstance.classCProperty.



JSB

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

lpvoba
Thanks for catching that! I've edited the post, but yes, it should say "${ClassBInstance?.someWayToAccessClassAProperty}".

I had expected "this" to be the classBInstance, but maybe I should just say "classBInstance"? "this" still works, though, because I still get the properties from the classBInstance. The issue is that I cannot access the properties from the ClassA instances related to the given classBInstance.
Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

Jeff Brown-4

On Nov 5, 2012, at 10:26 AM, lpvoba <[hidden email]> wrote:

> Thanks for catching that! I've edited the post, but yes, it should say
> "${ClassBInstance?.someWayToAccessClass*A*Property}".



One of us is confused about what you are trying to do.  If you are trying to access the classAProperty on an instance of ClassB and your reference to the ClassB instance is called classBInstance, then you would refer to classBInstance.classAProperty.



JSB
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

lpvoba
I'm sorry, I may need to clarify/expand my question a little more...is it also possible to access the classCProperty from the ClassBInstance in the ClassA GSP? Maybe something like:

classAShow.gsp {
   ${ClassBInstance?.classCProperty}
}

I hope I didn't just make it more confusing...
Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

Jeff Brown-4

On Nov 5, 2012, at 10:55 AM, lpvoba <[hidden email]> wrote:

> I'm sorry, I may need to clarify/expand my question a little more...is it
> also possible to access the classCProperty from the ClassBInstance in the
> ClassA GSP? Maybe something like:
>
> classAShow.gsp {
>   ${ClassBInstance?.classCProperty}
> }
>
> I hope I didn't just make it more confusing...
>



Your question "is it also possible to access the classCProperty from the ClassBInstance in the ClassA GSP" really isn't sensical.  There is no such thing as a "ClassA GSP".  GSPs aren't really associated with a domain class.  

Your GSP is probably being rendered by some controller action.  That controller action gets to put whatever is appropriate in the model that will be made available to the GSP and the GSP can reference anything and everything that is in the model.  If your controller action has put an instance of ClassA in the model like this...

class SomeController {
    def getStuff() {
        def someClassAObject = ClassA.get(42)
        render view: 'myGsp', model: [someClassAInstance: someClassAObject]
    }
}

...and the view wants to access the ClassB instances that are associated with that ClassA, maybe you want something like this in the GSP...

<g:each var="classB" in="${classAInstance.classBProperty}">
    <!-- in here you can access ${classB.classAProperty} and ${classB.classCProperty} -->
</g:each>

It is a little funky to have a collection in ClassA called classBProperty because the name doesn't suggest a collection, it suggests a single instance.  I used classBProperty here to refer to your collection because that is what is in your original note (see the hasMany in ClassA).

Maybe if you backed away from the generic example in this thread and try to describe what you are trying to do more specifically as it relates to your example, maybe that would help.



JSB
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

lpvoba
Thank you for the suggestion. I will give it a try in my code and let you know how it goes (unfortunately I cannot post my code right now, I'm not at work).
Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

Nathan Wells

Another recommendation is to extract all this complexity into a Grails Service. Managing multiple many to many relationships with domains alone is really complex and asking for suboptimal DB queries.

On Nov 5, 2012 1:37 PM, "lpvoba" <[hidden email]> wrote:
Thank you for the suggestion. I will give it a try in my code and let you
know how it goes (unfortunately I cannot post my code right now, I'm not at
work).



--
View this message in context: http://grails.1312388.n4.nabble.com/Issues-accessing-properties-of-a-joined-class-tp4637386p4637400.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Issues accessing properties of a joined class

lpvoba
Nathan Wells wrote
Another recommendation is to extract all this complexity into a Grails Service. Managing multiple many to many relationships with domains alone is really complex and asking for suboptimal DB queries.
Thanks for the suggestion. I have not implemented a Grails Service before but will look into it.

I've still been working on this problem for the last few days with no luck, but I think that I've been able to narrow down my problem:

Essentially I have a child class with two parent classes, ParentA and ParentB. In the show.gsp view of ParentA, I iterate through all joined children. For each instance of the child class, I need to access the properties of ParentB. I'm still not sure if this is possible or not....Anyone with ideas on how to implement this?