SilverStripe convertDataObjectSet正在剥离其他属性

SilverStripe convertDataObjectSet正在剥离其他属性,silverstripe,Silverstripe,我试图将“AbsoluteLink”属性添加到数据列表中的每个数据对象,然后使用JSONDataFormatter::convertDataObjectSet()将列表转换为JSON 我有以下功能: public function json() { $data = ResourceCentreArticlePage::get()->filter('ShowInMenus', '1')->filter('ShowInSearch', '1')->sort('Cr

我试图将“AbsoluteLink”属性添加到数据列表中的每个数据对象,然后使用
JSONDataFormatter::convertDataObjectSet()
将列表转换为JSON

我有以下功能:

public function json() {
    $data      = ResourceCentreArticlePage::get()->filter('ShowInMenus', '1')->filter('ShowInSearch', '1')->sort('Created', 'DESC');
    $pageArray = new ArrayList();

    foreach ($data as $page) {
        $page->AbsoluteLink = $page->AbsoluteLink();
        $pageArray->push($page);
    }

    // If I dump out the content of $pageArray here the object has the AbsoluteLink property  

    $jsonFormatter = new JSONDataFormatter();
    $jsonData      = $jsonFormatter->convertDataObjectSet($pageArray);

    // If I dump out the content of $jsonData here there is no AbsoluteLink property

    $this->response->addHeader("Content-type", "application/json");

    return $jsonData; 
}
问题:

public function json() {
    $data      = ResourceCentreArticlePage::get()->filter('ShowInMenus', '1')->filter('ShowInSearch', '1')->sort('Created', 'DESC');
    $pageArray = new ArrayList();

    foreach ($data as $page) {
        $page->AbsoluteLink = $page->AbsoluteLink();
        $pageArray->push($page);
    }

    // If I dump out the content of $pageArray here the object has the AbsoluteLink property  

    $jsonFormatter = new JSONDataFormatter();
    $jsonData      = $jsonFormatter->convertDataObjectSet($pageArray);

    // If I dump out the content of $jsonData here there is no AbsoluteLink property

    $this->response->addHeader("Content-type", "application/json");

    return $jsonData; 
}
AbsoluteLink属性在通过
convertDataObjectSet
方法运行
$pageArray
后被删除


我缺少什么?

使用
$jsonFormatter->setCustomAddFields()将在此处提供帮助

将以下内容添加到Page类:

public function getMyAbsoluteLink() {
    return $this->AbsoluteLink();
}
例如,Page.php:

class Page extends SiteTree {
    public function getMyAbsoluteLink() {
        return $this->AbsoluteLink();
    }
}
然后像这样使用“魔法场”:

public function json() {
    $pages = Page::get()
        ->filter('ShowInMenus', '1')
        ->filter('ShowInSearch', '1')
        ->sort('Created', 'DESC');

    $jsonFormatter = new JSONDataFormatter();
    // add your custom field
    $jsonFormatter->setCustomAddFields(["MyAbsoluteLink"]);
    $jsonData = $jsonFormatter->convertDataObjectSet(
        $pages
    );

    return $jsonData;
}
注意
$jsonFormatter->setCustomAddFields([“MyAbsoluteLink”])和我删除了数组操作


我还删除了你的数组操作。
convertDataobjectSet
函数的工作原理在运行之前,您似乎无法修改对象

这里有注释
//如果我在这里转储$pageArray的内容,这里没有绝对链接属性
,您的意思是如果转储$jsonData的注释,还是像您所说的$pageArray中的数据实际上丢失了?啊,很好,这确实是$jsonData,将立即更新。该字段参数将仅返回指定的字段。如果我将AbsoluteLink添加到此对象,则对象返回为空:$jsonFormatter->convertDataObjectSet($data,array('AbsoluteLink');是的,该函数只返回文档中指定的字段,但作为示例,它应该可以工作。Function AbsoluteLink()不遵循约定,因此它可以直接工作。您需要一个以公共函数getMyMagicMethod开头的方法。注意开头的get。如果我没记错的话。无论如何,回到我的工作电脑上,这样我可能有时间启动一个真正的工作示例。