在Silverstripe上,我如何检索孙对象的数据列表,包括其外域?

问题描述 投票:1回答:1

我有一个与以下概述类似的设置。

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上的函数(这里称为Grandchildren())上检索所有B对象的所有C对象。

我无法使用的两个潜在解决方案:

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

非常感谢您的帮助。

php silverstripe silverstripe-4
1个回答
1
投票

对不起。答案比我意识到的要简单得多。只是再次遍历了DataList文档上的所有方法。

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

在这里留下问题和答案,以帮助那些与我处境相同的人(因为我以前一直被这个问题困扰很多次。]

© www.soinside.com 2019 - 2024. All rights reserved.