问题
I would like to know how customize this uploadfield to get from $belongs_many_many Act table.
A part of the code....
<?
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);
}
I have tried these methods to get Act variable but nothing appear:
$var=$this->Parent->MultipleBlocks->Act;
$var=$this->many_many(MultipleBlocks)->Act;
$var=$this->Parent->Act;
Any idea? Thanks in advance!
回答1:
If I'm reading your code correctly, the reason why you aren't able to access the Act
variable is due to the fact that you're not iterating through the MultipleBlocks
relation. A sample of how to get the Act
variable for a the related MultipleBlocks
would be as follows (within your template/layout file):
<% loop $MultipleBlocks %>
$Act
<% end_loop %>
In php it would be something along the lines of:
$multipleBlocks = $this->MultipleBlocks();
foreach($multipleBlocks as $block){
$act = $block->Act;
}
When you reference MultipleBlocks
as you did in the $this->MultipleBlocks->Act
it won't work as MultipleBlocks
is a many relation so it's actually a DataList
rather than an individual object.
Here is the has_many/has_one example:
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;
}
}
回答2:
Wow... after some days, and learning Silverstripe little more, I have found my solution. To achieve MultiBlocks from MultiBlock column is this :
parent::MultipleBlocks();
To make my save to folder customization, there is my code... It works perfectly well. The ID of the MultiBlockColumns must be create first.
$parent = parent::MultipleBlocks();
$uploadField->setFolderName('Uploads/Subsite'.Subsite::currentSubsiteID().'/images/'.$parent['MultipleBlock']->Act);
That's it! Thanks for submitting idea.
来源:https://stackoverflow.com/questions/29475944/silverstripe-get-var-from-many-many-relation-custom-upload