Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Double insertions using dataobject

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2266 Views
  • Mednezz
    Avatar
    Community Member
    23 Posts

    Double insertions using dataobject Link to this post

    Hey there!

    Somehow when i insert stuff inside the DB i get it twice! Here's my model :

    [code

    class MyAccountCredits extends DataObject {
       static $db = array (
          "MemberID" => "Int",
          "ProductID" => "Varchar(200)",
          "Aantal" => "Int",
          "Geldigtot" => "Date",
          "Credits" => "Int"
       );   
    }

    and here i put it inside the database :

    $producten = unserialize(Session::get('producten'));
    $sMail = '';
    $member = $this->Member();
    $nextyear = date('Y-m-d',mktime(0, 0, 0, date("m"), date("d"), date("Y")+1));
    foreach ($producten as $product) {
       $sMail .= $product->product_aantal. ' x ' . $product->product.' totaal = '. $product->prijs_totaal.' <br>';
       $oCredits = Object::create('MyAccountCredits');
       $oCredits->MemberID = $member->ID;
       $oCredits->ProductID = $product->ProductID;
       $oCredits->Aantal = $product->product_aantal;
       $oCredits->Geldigtot = $nextyear;
       $oCredits->Credits = $product->credits;
       $oCredits->write();
    }
    print $sMail;

    when i submit 3 products, the string $sMail contains the 3 products. but in the db i get them twice. but strange enough like

    ID Name
    1 product 1
    2 product 2
    3 product 3
    4 product 1
    5 product 2
    6 product 3

    Why does the dataobject wants to insert the hole operation 2 times?

    When i use ?previewwrite=1 i also see just 3 insert queries, but when i remove it, it inserts 6 times!

    And when i use showqueries=1 is only see the 3 expected queries....

    Cheers!

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Double insertions using dataobject Link to this post

    "MemberID" => "Int",
    "ProductID" => "Varchar(200)",

    this is the silverstripe-notation for a has-one/has-many relation, whereas those columns are automatically generated and don't need to be included in $db.

    if your columns don't signify silverstripe-relations, consider renaming them to some other convention, e.g. "Member_ID" .

    not sure if thats the problem, but it might confuse the database-layer

  • Mednezz
    Avatar
    Community Member
    23 Posts

    Re: Double insertions using dataobject Link to this post

    too bad, that didn't do the trick

    I really need this to be fixed, any ideas how to debug this?

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Double insertions using dataobject Link to this post

    very strange...

    do you have some redirect after the saving?
    do you clear the session after saving?
    is this saving taking place in the cms?
    do you have any onBeforeWrite()-methods defined?
    which version of silverstripe are you working on?

  • Mednezz
    Avatar
    Community Member
    23 Posts

    Re: Double insertions using dataobject Link to this post

    can't believe it!

    Offcourse i had to clear the session after writing!

    Stupid, stupid stupid!!

    Thanks for helping me out ingo!

    2266 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.