Once you have loaded a component are you then able to access properties of that object with set conditions? For instance, if you have a one-to-many relationship between people and pets, you load people specifying a particular person, you then want to pull all said persons pets where the pets are of a particular species. cats vs dogs for instance.
<cfset person=EntityLoad("person", {name="#URL.name#"})>
<cfset pets=person[1].getPets()>
is there anyway to call getPets where type=’dog’ or something?
Or would I have to loop through the pets creating structures for each type and deal with them that way?
You should not loop through the pets if at all possible: distinguishing between types of pets is best left to the object that has a relationship with them or to a service that works with those objects.
One approach is to add a method to
Personthat will return a type of Pet. We’re using something similar in our current project.You might also be able to make it more generic:
Another approach is to use the
whereattribute in relationships in thePersonobject when retrieving pets. We use something like this as well:Note that these examples make several assumptions about the structure of your app and database: there is a Pets object in the
modelfolder that maps to a table in your db; this table has a PET_TYPE column (that maps to a petType property) that contains pet types in lower case; thePersonobject maps to a table with a primary key calledPERSON_ID, which is also a foreign key in thePetstable.Also, when considering these and other approaches, you should think about how you intend to access pets. Turning off lazy loading can create performance hits if you have a significant number of Pets, particularly if there are several types you want to store (so you’re making several passes through the Pets table when initializing a Person). You may not need separate properties if you don’t access Pets often; it may be enough simply to have a getPets() function.