在 Silverstripe 上,我如何检索孙对象的数据列表,包括它们的额外字段?

On Silverstripe, how do I retrieve a DataList of grandchild objects including their extrafields?

我的设置类似于下面列出的设置。

class A extends DataObject{
    private static $many_many = array(
        'Bs' => 'B'
    );

    public function Grandchildren(){
        // return grandchildren, including their many_many_extraFields data (ExtraData)
    }
}

class B extends DataObject{
    private static $many_many = array(
        'Cs' => 'C'
    );

    private static $belongs_many_many = array(
        'As' => 'A'
    );

    private static $many_many_extraFields = array(
        'As' => array(
            'ExtraData' => 'Int'
        )
    );
}

class C extends DataObject{
    private static $db = array(
        'Name' => Varchar(255)
    );

    private static $belongs_many_many = array(
        'Bs' => 'B'
    );
}

我希望从 A[= 上的函数中检索所有 B 对象的所有 C 对象29=](这里叫孙子())。

我无法开始工作的两个潜在解决方案:

1 -

public function Grandchildren(){
    $grandchildren = DataList::create();
    foreach($this->Bs() as $b){
        $Cs = $b->Cs();
        // How would I then merge these into one single DataList ($grandchildren)?
    }
    return $grandchildren;
}

2 -

public function Grandchildren(){
    return C::get()->leftJoin('B_Cs', 'B_Cs.CID = C.ID')->where('B_Cs.AID = ' . $this->ID);
    // This works but doesn't contain the needed ExtraData.
}

非常感谢您的帮助。

啊抱歉。答案比我意识到的要简单得多。刚刚再次检查了 DataList 文档中的每个方法。

public function Grandchildren(){
    return $this->Bs()->relation('Cs');
}

在这里留下问题和答案,以帮助任何遇到与我相同情况的人(因为我之前已经多次遇到这个问题)。