SilverStripe 3 Left Join缺少参数

时间:2015-06-22 21:22:36

标签: sql left-join silverstripe

我有一个与其他一些数据对象相关的数据对象,我正在尝试为它们构建一个报告页面。

到目前为止,我已经在我的页面控制器中获得了以下代码,以显示一个表单,我将开始为报告选择过滤选项。

但是由于左连接,我收到此错误:

  

[警告]缺少SQLQuery :: addLeftJoin()

的参数2

在我调试时,似乎raw2sql正在输出:

  

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

我假设反斜杠是造成错误的原因

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;
}

1 个答案:

答案 0 :(得分:3)

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.