Skip to main content
September 11, 2013
Question

Orm get only the id from child object

  • September 11, 2013
  • 1 reply
  • 479 views

I'm new to orm, and I'm making some tests.

I've two simple object Artist and Art:

Artist.cfc

  // identifier

  property name="ArtistID" fieldtype="id" generator="native";

  // properties

  property name="Firstname";

  property name="Lastname";

  property name="Address";

  property name="City";

  property name="State";

  property name="PostalCode";

  property name="Email";

  property name="Phone";

  property name="Fax";

  property name="ThePassword";

  /* one artist can have many... */

  /* return an array of Art objects */

  property name="Art" fieldtype="one-to-many" cfc="Art" fkcolumn="ArtistID" type="array" orderby="Price Asc";

Art.cfc

  // identifier

  property name="ArtID" fieldtype="id" generator="native" ;

  // properties

  property name="ArtName" ;

  property name="Description" ;

  property name="Price" ;

  property name="LargeImage" ;

  property name="IsSold" ;

  property name="Artist" fieldtype="many-to-one" fkcolumn="ArtistID" cfc="Artist"; 

When I try to get an artist:

   artist = EntityLoadByPK("Artist", 1);

I get a response with a struct with all the artist property, and an array of child art with all properties. (I have serialized in the example)

{"ArtistID":1,"Firstname":"Leonardo","Lastname":"Da vinci","City":"NewYork","Art":[{"ArtID":1,"ArtName":"landscape","Price":"1500","IsSold":"false","Artist":null},{"ArtID":2,"ArtName":"landscape","Price":"1500","IsSold":"false","Artist":null}]

I would like to retrieve not all the properties of child art, but only the primary key. Is this possible with Orm?

This topic has been closed for replies.

1 reply

Inspiring
September 15, 2013

The obvious answer would be;

If all you want is the ID, just use normal SQL.

I realise you're trying to learn ORM - But you don't have to use ORM, even in a fully OOP application - if it doesn;t make sense to do so.

We use normal SQL for reports and large / messy recordsets all the time.

If you then need to get things back into an object you can loop through the query and SET properties on your objects.

But to be a little more helpful;

If you use:

(untested - but hopefully you get the idea...)

//Create new list

art_IDs = "";

//Retrieve art by artist PK 1

artist = EntityLoadByPK("Artist", 1);

//Loop through array of "art" objects

for (i=1;i LTE ArrayLen(artist);i=i+1) {

     //Retrieve the ID and add it to the list

     listAppend(art_IDs, artist.art.getArtId();

}