0
votes

In Silverstripe 3, I'm trying to run some custom SQL and return the result for handling in my template:

function getListings(){
        $sqlQuery = new SQLQuery();
        $sqlQuery->setFrom('ListingCategory_Listings');
        $sqlQuery->selectField('*');
        $sqlQuery->addLeftJoin('Listing', '"ListingCategory_Listings"."ListingID" = "Listing"."ID"');
        $sqlQuery->addLeftJoin('SiteTree_Live', '"Listing"."ID" = "SiteTree_Live"."ID"');
        $sqlQuery->addLeftJoin('ListingCategory', '"ListingCategory_Listings"."ListingCategoryID" = "ListingCategory"."ID"');
        $sqlQuery->addLeftJoin('File', '"ListingCategory"."IconID" = "File"."ID"');

        $result = $sqlQuery->execute();

        $dataObject = new ArrayList();
        foreach($result as $row) { 
            $dataObject->push(new ArrayData($row)); 
        }
        return $dataObject;

    }

However, this is giving me the error:

Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'ArrayList'

What am I doing wrong here and how can I get the result of this query into my template?

1

1 Answers

3
votes

I have not seen your template code, but I am assuming you are just calling $Listings in template. Which will not work because ArrayList has no forTemplate method (what forTemplate does is output the object as a appropriate string, for example a forTemplate on Form outputs a html element).

You probably want to do is loop the List and use the object:

<% loop Listings %>
    $ID
    $Something
    $Foobar
<% end_loop %> 

or, you can call forTemplate on the object if it has that method:

<% loop Listings %>
    $forTemplate
<% end_loop %>