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.

Archive /

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

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

Double insertions using dataobject


Go to End


5 Posts   3176 Views

Avatar
Mednezz

Community Member, 23 Posts

4 October 2007 at 12:06am

Edited: 04/10/2007 1:19am

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!

Avatar
Ingo

Forum Moderator, 801 Posts

4 October 2007 at 1:46am

Edited: 04/10/2007 1:48am

"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

Avatar
Mednezz

Community Member, 23 Posts

4 October 2007 at 2:07am

too bad, that didn't do the trick

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

Avatar
Ingo

Forum Moderator, 801 Posts

4 October 2007 at 2:46am

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?

Avatar
Mednezz

Community Member, 23 Posts

4 October 2007 at 3:03am

can't believe it!

Offcourse i had to clear the session after writing!

Stupid, stupid stupid!!

Thanks for helping me out ingo!