Peter Harley Broom
Peter Harley Broom

Reputation: 115

SilverStripe 3 Left Join Missing argument

I have a data object related to some other data objects and I am trying to build a reporting page for them.

So far I've got the code below in my page controller to display a form where I will begin to select filtering options for the report.

However I am getting this error due to the left join:

[Warning] Missing argument 2 for SQLQuery::addLeftJoin()

It would seem that the raw2sql is outputting this when I've debugged:

\'AgeRangeData\', \'CallEvent.AgeRangeData ID=AgeRangeData.ID)\'

I'm assuming that the backslashes is what is causing the error

public function ReportingFilter(){

    $DataObjectsList = $this->dbObject('DataObjects')->enumValues();

    $fields = new FieldList(
        new DropdownField('DataObjects', 'Data Objects', $DataObjectsList)
    );

    $actions = new FieldList(
        new FormAction("FilterObjects", "Filter")
    );

    return new Form($this, "ReportingFilter", $fields, $actions);
}

public function FilterObjects($data, $form){

    $data = $_REQUEST;
    $query = new SQLQuery();

    $object = $data['DataObjects'];
    $leftJoin = Convert::raw2sql("'" . $object . "', 'CallEvent." . $object . " ID={$object}.ID)'");

    $query->selectField("CallEvent.ID", "ID");
    $query->setFrom('`CallEvent`');
    $query->setOrderBy('CallEvent.Created DESC');
    $query->addLeftJoin($leftJoin);

    return $query;
}

Upvotes: 0

Views: 215

Answers (1)

Dan Hensby
Dan Hensby

Reputation: 1143

SQLQuery::addLeftJoin() takes two arguments. The first is the table to join on and the second is the "on" clause.

You want:

$query = new SQLQuery();
$query->addLeftJoin($object, '"CallEvent"."ID" = "' . $object . '"ID"');

You'd need to escape $object appropriately, of course.


NB: Your code looks a little fragile as you're not ensuring that you $object actually has a DB table. I recommend you use ClassInfo::baseDataClass($object). This will have the added benefit that it will also sanitise your class name and ensure it's a real class.

Upvotes: 3

Related Questions