Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Advanced query using DataObject::get()


Reply


3 Posts   1889 Views

Avatar
SSadmin

Community Member, 90 Posts

7 February 2010 at 11:10am

Hey,folks.
I am building a website using silverstripe at this moment. But i am still a newbie :(
I met the problem with Dataobject::get() again.

The data table relationship lists below:

one ListingPage has many ListingImages

static $has_many=array(
   'ListingImages'=>'ListingImage'
   );

one ListingImage has one Attachment(image)

static $has_one=array(
   //parent
   'ParentListingPage'=>'ListingPage',
   'Attachment'=>'MyImage');

MyImage extends Image Type.

OKay. the things i want is to select all the ListingImages where ListingPage id=current Listing page id. and furthermore, I want to access the URL of each Attachment. So, i set of URL of the Images' Attachment of the current Listing page is what i want by doing the Dataobject query.

function PictureData(){
      
      $id=$this->ID;

      $data=DataObject::get('ListingImage',"ParentListingPageID=$id);
      
            }
i can select all the ListingImage using the function above, but not sure how to further query the content in ListingImage and grab the URL back.

If i could be able to get the ListingImages' URL, i think i could use the foreach loop through the URL sets and output as what i want.

Any Suggestions, really appreciate.
            

Avatar
Willr

Forum Moderator, 5513 Posts

7 February 2010 at 1:57pm

ListingPage id=current Listing page id

If I'm correct in reading your code you're trying to get the ListingImages from the current page - instead of using $this->ID and another query you can use the ORM in SS to return the sets for you. Since a Listing Page has many Listing Images you can do $this->ListingImages() to return all the images attached to a page.

Using $this->ListingImages() on your ListingPage will return a set of ListingImage objects - and since its an object you can call methods on individual images in the set using a foreach loop.

if($images = $this->ListingImages()) {
foreach($images as $image) {
// you can call various methods here
// $image->Attachment()->URL
// $image->FieldName
//....
}
}

Even though you said you only need the URL of each attachment I would pass the whole set of images back and then do the iteration in the template.

Avatar
SSadmin

Community Member, 90 Posts

8 February 2010 at 10:25am

Hey, Thanks Willr. Really thx for your timely and generous reply, really appericate.
Yeah,you r right with reading my code and understand my purpose.
The ORM is great useful. i have tried the method you mentioned that works great.
From my understanding, the $this->ListingImages() is grabbing all ListingImage items within current ListingPage.
functionally, It works similar to the template way: <% control ListImages %> Looping <% end_control %>.

Anyway. thanks so much for help, all the things i have done above (get the image URLs) is trying to get rid of the last comma in Javascript Array for solving the IE capatiable problem.

If you leave the comma there at a array,[{},{},{},] ,IE will treat differently compared with FF. So, i have to using backend php function to generate a json object and substr_replace it and output to frontend.
Code:
   function getJsonData(){
      
      $json="[";
      if($images=$this->ListingImages()){
         foreach ($images as $image)
            {
               $json.="{
            Thumb:'{$image->Attachment()->ListingThumb()->URL}',
            Full:'{$image->Attachment()->SingleListing()->URL}'
               },";
            }
         $json = substr_replace($json, "];", -1);
            Debug::show($json);
            return $json;
      }
   }

Lol, Was a good lession to learn. Will to talk with you soon.