Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM 1.8 cant update existing object
#1

[eluser]hagbard Celine[/eluser]
Hi,

this is my code. (really simple)
Code:
$bps = new Blog_post();
                $bps->where('TitleUrl', $id)->get();

                //var_dump($bps);die();
                $bps->TitleUrl = ParseTitle($this->input->post('btitle'));
                $bps->Title = $this->input->post('btitle');
                $bps->Message = $this->input->post('bentry');
                $bps->CategoryID = $catname;
                $bps->save();

Error-Message:

Duplicate entry '2' for key 'PRIMARY'

INSERT INTO `blog_posts` ....... blah.

i though if i select an existing object by the "get" method DataMapper will automatically do an update instead of an insert?
what i do wrong?

thanks for help
#2

[eluser]WanWizard[/eluser]
If you use the save() method, UPDATE is used if
- the object property id exists and has a value
- you didn't call save_as_new()

In all other cases Datamapper uses INSERT.

Given the code example, my guess is that you didn't follow the design requirements as documented, which clearly states that the primary key of every datamapper table MUST be called 'id'.
#3

[eluser]denis747[/eluser]
I had this problem as well so I chose to use "update where" instead which works. On my table the primary key is named "ID" in capital letters, could this be the reason for the error when calling save() on an existing object?
#4

[eluser]WanWizard[/eluser]
Yes, it must be called 'id'. 'id' != 'ID'.

And column names can be case sensitive on some database platforms...
#5

[eluser]denis747[/eluser]
Thanks I edited my table and changed the primary key name from 'ID' to 'id'. Save() on an existing object is now working perfectly, it is updating only the changed fields. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB