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.

Data Model Questions

How to get an array containing only data from one column


Reply

2 Posts   253 Views

Avatar
s_a_m_y

29 April 2014 at 7:03am Community Member, 25 Posts

I've been going over tons of posts and the data model documentation. Maybe I am just blind for the obvious by now but I only find options, that retrieve ALL columns from data objects (which would equal SELECT * in SQL). What I need is to get only the data from one specific column that matches the criteria (SELECT Filename FROM ... WHERE ParentID=85). Neither the filter nor sort options will provide this as far as I can tell. Here is my set-up:

I have a list of images saved to a specific folder with a fixed ID. I need to pass the "Filenames" dynamically as an array to a customScript in the init(); within the Page_Controller. If I would need to access this on the template, I'd do:

function GetImages(){
   return DataObject::get("Image", "ParentID = 85");
}

and call the method in the template but limit it to the wanted data:

<% control GetImages %>
   $Filename
<% end_control %>

How do I get only the data from the column "Filename" into an array, that I then can use in my customScript also within the init() of the controller:

Requirements::customScript("
         jQuery(document).ready(function(){
            $.backstretch([
                "file1.jpg", "file2.jpg", "file3.jpg" //THIS IS WERE THE ARRAY WOULD GO
               ], {
               fade: 750,
               duration: 4000
               });
           });
       ");

Thankful for any hint!
Sam

Avatar
swaiba

29 April 2014 at 8:15am Forum Moderator, 1796 Posts

http://api.silverstripe.org/3.1/class-DataList.html#_column
http://api.silverstripe.org/2.4/class-DataObjectSet.html#_column