3063 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 366 Views |
-
get parent relation ID before writting the Object

24 November 2012 at 1:23am
Hi,
Is it possible to get a Children DataObject $has_one ID before it is written?
I'll try to give an example:
A Product DataObject $has_many Items which are created through a GridField.
Each Item has a $has_one field that gets the ID of its parent Product (ProductID).
This ID is somehow automatically when the Item is created through the GridField.What am wondering is, how can I get this parent ProductID of the Item being created (from within the getCMSFields function) before it is written to the database?
Hope this makes sense.
Thanks, Thierry
-
Re: get parent relation ID before writting the Object

28 November 2012 at 2:04pm
That ParentID will be set automatically on write. If you want to see it, you could hide the Item GridField until after the Product is saved for the first time by wrapping it in if ($this->ID != 0){your grid field} the you could always also show the $this->ID in a LiteralField
I hope that's what you were asking
-
Re: get parent relation ID before writting the Object

28 November 2012 at 9:00pm
Thanks, this is actually how this ParentID is automatically set that interest me.
I tried to find out how this "magic" happens but haven't found anything yet. Since this is set automatically there must be a way to replicate the behaviour and find out what the ParentID will be even before the write function is called.I considered parsing the URL since when adding an object from within a GridField, the ParentID is left in the URL, like this:
/admin/product-admin/Product/EditForm/field/Product/item/1/ItemEditForm/field/productTranslations/item/newWhere "1" is the future ParentID of the object being created.
Just wondering is there is a clean way to do this? Or how does SilverStripe does it? I would guess through the URLHandler? -
Re: get parent relation ID before writting the Object

29 November 2012 at 1:14am
I believe the magic happens in onAfterAfterWrite on one of the prime base classes. I've overloaded this before but you need to the parent function too and I usually check if the value if a value is set or a relation exists before performing any functions to make sure it doesn't get stuck in a loop.
-
Re: get parent relation ID before writting the Object

25 April 2013 at 2:16am
Has anyone got this working? I'm trying to do the same thing.
-
Re: get parent relation ID before writting the Object

25 April 2013 at 2:23am
Well ID is autoincrement, so in theory you can guess it on the object Creation, last ID+1 save it in field, then in onAfterWrite double check and pass it to the relation if its differnt.
-
Re: get parent relation ID before writting the Object

25 April 2013 at 3:10am
Just in case it helps anyone else...
It's not my preferred solution, but for now I'm going with...
if($this->ID != 0) {
//Do stuff here after record has been saved
}
else {
//Record has never been saved...
$field = TextField::create('Field')->setTitle('Title')->setDisabled(true)->setValue('You can assign a this field once you have saved the record for the first time.');
}
| 366 Views | ||
|
Page:
1
|
Go to Top |



