Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

write() - Method on Field of Subclass of $has_many - DataObject


Go to End


2 Posts   1423 Views

Avatar
mtz

Community Member, 17 Posts

17 December 2011 at 5:41am

Hello,

i got that following setup:

Page-Class width 2 $has_many relations, to: lets say "Customer"- and "CustomerPremium"- DataObjects.
My CustomerPremuim - Class is a direct Subclass of Customer.

Now i want an db-write - operation (update a Field) on each related CustomerPremium DataObject in that onAfterWrite() - Method on the Page-Class.

If i target the Parent-Class "Customer" with "$this->Customer()" the write() Method works, but not on the Subclass.
Even since the Subclass has its own $has_many relation in Page.

Any Idea why is that? :(

if i print out the result of the iteration i get my regular DataObjects in any case. So i'm wondering that i cant write to it just because
its an Subclass.

my code is:
class Page extends .... {

$has_many {
"Customers" => "Customer",
"CustomersPremium" => "CustomerPremium"
}

function onAfterWrite(){
foreach($this->CustomerPremium() as $i) {
$i->Format = "482 x 288";
$i->write();
}

parent::onAfterWrite();

.....

Thanks!

Avatar
mtz

Community Member, 17 Posts

19 December 2011 at 3:58am

Ok, my Problem seems to be solved.
After min. 5 Hours walking down the wrong way I figured out that my Eventhandler onAfterWrite has to be changed in onBeforeWrite...

;-s