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.


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

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

Double insertions using dataobject


5 Posts   2322 Views


4 October 2007 at 12:06am (Last edited: 4 October 2007 1:19am), Community Member, 23 Posts

Hey there!

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


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



4 October 2007 at 1:46am (Last edited: 4 October 2007 1:48am), Forum Moderator, 801 Posts

"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


4 October 2007 at 2:07am Community Member, 23 Posts

too bad, that didn't do the trick

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


4 October 2007 at 2:46am Forum Moderator, 801 Posts

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?


4 October 2007 at 3:03am Community Member, 23 Posts

can't believe it!

Offcourse i had to clear the session after writing!

Stupid, stupid stupid!!

Thanks for helping me out ingo!