Silverstripe:得到many_many关系自定义上传变种(Silverstripe: ge

2019-10-22 13:07发布

我想知道如何定制这个uploadfield从$ belongs_many_many法表来获得。

代码的一部分....

<? 

class MultipleBlock extends DataObject {

    private static $db = array(
        'Act' => 'Varchar',


    private static $many_many = array(
        'MultipleBlockColumns' => 'MultipleBlockColumn'
    );


}

class MultipleBlockColumn extends DataObject {

    static $belongs_many_many = array(
        'MultipleBlocks' => 'MultipleBlock'
    );
    private static $has_one = array(
        'Image' => 'Image',
    );

    $fields->addFieldToTab('Root.Image',$uploadField = new UploadField(
                $name = 'Image',
                $title = 'Image'
            )    
        );

    enter code here
    $uploadField->setFolderName('Uploads/Subsite'.Subsite::currentSubsiteID().'/images/'.$var);

}

我曾尝试这些方法来获得法案变量,但没有出现:

$var=$this->Parent->MultipleBlocks->Act;
$var=$this->many_many(MultipleBlocks)->Act;
$var=$this->Parent->Act;

任何想法? 提前致谢!

Answer 1:

如果我正确地读你的代码,为什么你不能够访问的原因Act变量是由于这样的事实,你没有通过迭代MultipleBlocks关系。 如何获得样本Act变量的相关MultipleBlocks将如下所示(模板/布局文件中):

<% loop $MultipleBlocks %>
$Act
<% end_loop %>

在PHP这将是沿着线的东西:

$multipleBlocks = $this->MultipleBlocks();
foreach($multipleBlocks as $block){
    $act = $block->Act;
}

当您引用MultipleBlocks ,你在做$this->MultipleBlocks->Act将无法正常工作MultipleBlocks是一个一对多的关系所以它实际上是一个DataList ,而不是一个单独的对象。

Update

这里是的has_many / HAS_ONE例如:

class MultipleBlock extends DataObject {

    private static $db = array(
        'Act' => 'Varchar',
    );


    private static $has_many = array(
        'MultipleBlockColumns' => 'MultipleBlockColumn'
    );


}

class MultipleBlockColumn extends DataObject {

    private static $has_one = array(
        'MultipleBlock' => 'MultipleBlock'
    );
    private static $has_one = array(
        'Image' => 'Image',
    );

    public function getBlockAct(){
        return $this->MultipleBlock()->Act;
    }

}


Answer 2:

哇......过了些日子,学习Silverstripe多一点,我发现我的解决方案。 为了实现从多块列多嵌段是这样的:

parent::MultipleBlocks();

为了使我保存到文件夹定制,有我的代码......它工作得很好。 该MultiBlockColumns的ID,必须先创建。

$parent = parent::MultipleBlocks();
            $uploadField->setFolderName('Uploads/Subsite'.Subsite::currentSubsiteID().'/images/'.$parent['MultipleBlock']->Act);

而已! 感谢您提交的想法。



文章来源: Silverstripe: get var from many_many relation custom upload