按页面ID SilvertStripe3过滤数据对象

时间:2018-06-03 18:36:49

标签: php oop silverstripe

我有一个页面类型“类扩展页面”,它有很多'TestBlocks'=> 'TestBlock',然后CMS用户将新数据输入CMS后端的字段,我需要的是从“类TestBlock扩展DataObject”中获取这些值并创建一个形成与他们。基本上数据会成为问题,但我不确定如何以正确的方式将其放入我的Test控制器中。当我尝试使用dataobect的get方法时,它返回来自pagetype的所有页面的所有值,而不是下面的特定页面代码:

class Test_Controller extends Page_Controller
{
private static $allowed_actions = array(
    'TestForm',

);

public function TestForm()
{

    $players = MultiQuestion::get();
    echo $players->QuestionText;
    $array = [];
    foreach ($players as $player) {
        $array[] = TextField::create($player->QuestionText)->setFieldHolderTemplate('Field_Holder');
    }

    $fieldGroup1 = CompositeField::create(
        $array

    )->setName('FieldGroup1');

    $fields = FieldList::create($fieldGroup1);
    $actions = FieldList::create(
        FormAction::create('submit', 'Get Results')->addExtraClass('get_results')
    );
    $form = Form::create($this, __function__, $fields, $actions);

    return $form;
}
}

1 个答案:

答案 0 :(得分:2)

假设您已正确设置关系,则可以使用$testBlocks = $this->TestBlocks()

<强> TestBlock.php

class TestBlock extends DataObject {
  static $has_one = array(
    'Test' => 'Test'
  );
}

<强> test.php的

class Test extends Page {
  static $has_many = array(
    'TestBlocks' => 'TestBlock'
  );
}

class Test_Controller extends Page_Controller {

  public function TestForm() {
    $testBlocks = $this->TestBlocks();
    //other code
  }
}