Silverstripe:从 many_many 关系自定义上传中获取变量
Silverstripe: get var from many_many relation custom upload
我想知道如何自定义此上传字段以从 $belongs_many_many 法案 table.
获取
部分代码....
<?
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);
}
我尝试了这些方法来获取 Act 变量,但没有出现:
$var=$this->Parent->MultipleBlocks->Act;
$var=$this->many_many(MultipleBlocks)->Act;
$var=$this->Parent->Act;
有什么想法吗?
提前致谢!
如果我没看错你的代码,你无法访问 Act
变量的原因是你没有遍历 MultipleBlocks
关系.如何获取相关 MultipleBlocks
的 Act
变量的示例如下(在您的 template/layout 文件中):
<% loop $MultipleBlocks %>
$Act
<% end_loop %>
在 php 中,它类似于:
$multipleBlocks = $this->MultipleBlocks();
foreach($multipleBlocks as $block){
$act = $block->Act;
}
当您像在 $this->MultipleBlocks->Act
中那样引用 MultipleBlocks
时,它将不起作用,因为 MultipleBlocks
是一个多关系,所以它实际上是一个 DataList
而不是一个单个对象。
更新
这是 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;
}
}
哇...几天后,对 Silverstripe 的了解又多了一点,我找到了解决方案。从 MultiBlock 列实现 MultiBlocks 是这样的:
parent::MultipleBlocks();
为了让我保存到文件夹自定义,有我的代码...它工作得很好。必须先创建 MultiBlockColumns 的 ID。
$parent = parent::MultipleBlocks();
$uploadField->setFolderName('Uploads/Subsite'.Subsite::currentSubsiteID().'/images/'.$parent['MultipleBlock']->Act);
就是这样!感谢您提交想法。
我想知道如何自定义此上传字段以从 $belongs_many_many 法案 table.
获取部分代码....
<?
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);
}
我尝试了这些方法来获取 Act 变量,但没有出现:
$var=$this->Parent->MultipleBlocks->Act;
$var=$this->many_many(MultipleBlocks)->Act;
$var=$this->Parent->Act;
有什么想法吗? 提前致谢!
如果我没看错你的代码,你无法访问 Act
变量的原因是你没有遍历 MultipleBlocks
关系.如何获取相关 MultipleBlocks
的 Act
变量的示例如下(在您的 template/layout 文件中):
<% loop $MultipleBlocks %>
$Act
<% end_loop %>
在 php 中,它类似于:
$multipleBlocks = $this->MultipleBlocks();
foreach($multipleBlocks as $block){
$act = $block->Act;
}
当您像在 $this->MultipleBlocks->Act
中那样引用 MultipleBlocks
时,它将不起作用,因为 MultipleBlocks
是一个多关系,所以它实际上是一个 DataList
而不是一个单个对象。
这是 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;
}
}
哇...几天后,对 Silverstripe 的了解又多了一点,我找到了解决方案。从 MultiBlock 列实现 MultiBlocks 是这样的:
parent::MultipleBlocks();
为了让我保存到文件夹自定义,有我的代码...它工作得很好。必须先创建 MultiBlockColumns 的 ID。
$parent = parent::MultipleBlocks();
$uploadField->setFolderName('Uploads/Subsite'.Subsite::currentSubsiteID().'/images/'.$parent['MultipleBlock']->Act);
就是这样!感谢您提交想法。