Reputation: 673
I am having a little problem using silverstripe elemental. I am trying to get some parent element property in a children element, but only after I save and published the children element, the parent element property becomes available.
For example, I want to get the QuizQuestion element property QuestionNumber = 2
in children element QuizAnswer, but I only get a default value QuestionNumber = 0
in the database.
When I Debug::show($this->QuizQuestion()->QuestionNumber)
, I can see it is indeed 2. How can I get the parent element property before I save and published the children element?
Upvotes: 3
Views: 379
Reputation: 318
The parent record should be available at this time. Make sure you call the parent method first.
Under the hood, elemental simply uses GridField so you can search for any GridField related documentation to understand this better.
public function onBeforeWrite()
{
parent::onBeforeWrite();
// $this->List(); // Obj(ID = 2);
}
Upvotes: 1
Reputation: 1199
I don't know now this module but normally you should be able to get the parent id out of the current url. It's very hacky but should work for you.
public function ItemByUrl() {
$uri = explode('/', $_SERVER['REQUEST_URI']);
$i = array_search('IDENTIFIER', $uri);
if(isset($uri[$i+PARTS])) {
$itemID = $uri[$i+PARTS];
$item = DATAOBJECTCLASS::get()->byID($itemID);
if($item) {
return $item;
}
}
}
So if your URL is something like that
admin/.../EditForm/field/ParentDataObject/item/67/ItemEditForm/field/ChildDataObject/item/new
And you want to get the ParentID 67
IDENTIFIER would be "ParentDataObject"
PARTS would be "2"
and DATAOBJECTCLASS would be "ChildDataObject"
Upvotes: 1