Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » write() - Method on Field of Subclass of $has_many - DataObject

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

Page: 1
Go to End
Author Topic: 587 Views
  • mtz
    Avatar
    Community Member
    17 Posts

    write() - Method on Field of Subclass of $has_many - DataObject Link to this post

    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!

  • mtz
    Avatar
    Community Member
    17 Posts

    Re: write() - Method on Field of Subclass of $has_many - DataObject Link to this post

    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

    587 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.