How do I properly save data to the database?
31,279
If you want to save new data, just use Model::save()
:
$data = array(
'ModelName' => array(
'foo' => $foo
)
)
// prepare the model for adding a new entry
$this->ModelName->create();
// save the data
$this->ModelName->save($data);
If you want to update your data just use the same method without calling Model::create()
$data = array(
'ModelName' => array(
'id' => $id
'foo' => $foo
)
)
$this->ModelName->save($data);
Edit:
I guess this is what you're looking for:
$this->ModelName->id = $id;
if (!$this->ModelName->exists()) {
$this->ModelName->create();
}
$this->ModelName->save($data);
Author by
trante
Updated on July 09, 2022Comments
-
trante almost 2 years
I normally save new data to the database like this:
$this->MyTable->set(array( 'id' => $id, 'code' => $temp_code, 'status' => $status, 'age' => $age, 'location' => $location, 'money' => $money )); $this->MyTable->save();
If the ID already exists in the database I update its corresponding fields like this:
$this->Adtweet->id = $id; $this->Adtweet->saveField('code', $temp_code); $this->Adtweet->saveField('status', $status);
Is there a better or 'proper' way to do this?
When I attempt to enter an ID that already exists and I use the
set
function, I get the following SQL integrity error:(Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '150245925' for key 'PRIMARY')
How can I write a function that cleanly handles duplicate entries without returning an error?
-
trante almost 12 yearsThank you for reply. To use "create" I think I should make something like this: Because before checking the model, I don't know even that ID exists in database or not..
$isIdExists = $this->ModelName->find('first', array( 'conditions' => array('ModelName.id' => $id)
---if ($isIdExists) { $this->ModelName->create(); }
-
trante almost 12 yearsThank you for update. When i tried
$this->ModelNAme->create();
, a row with ID zero is created. But I have a proper ID of user. So i changed it like this$this->ModelName->create( array ('id' => $id));
This created proper row. But after that!$this->ModelName->exists()
still gives true.. I suppose I should work more and more.. -
Julian Hollmann almost 12 yearsMaybe you forgot to set the ID column to
auto_increment
? -
trante almost 12 yearsWell, I use this model for the member data. So ID column is twitter ID of user. Every user has unique twitter ID and it doesn't start from zero. So I create new record with ID parameter, if it doesnt exist.
-
trante almost 12 yearsNow I'm dealing with this:
$this->ModelName->twitter_user_id = $twitter_user_id;
$isIdExists = $this->ModelName->exists();
in this case$isIdExists
returns false. But from phpmyadmin I can see that, the record is there -
Julian Hollmann almost 12 yearsThe main problem here is, that cakephp expects the primary key is called 'id', if this isn't the case you need to tell this your model. So if your primary key field is called "twitter_user_id" you have to set
public $primaryKey = 'twitter_user_id';
in your model. -
trante almost 12 yearsprimaryKey info helped me very much thank you. Sorry for disturbing you. Is it possible to stop active record in a for loop? In the code below, "arrayOfIds" has 4 elements. When I run it, in first loop ($arrayOfIds[0])
$this->ModelName->exists()
comes false, and new row is created. Then in next loops, for example $arrayOfIds[1],$this->ModelName->exists()
comes true, although the Id of them are different and not exist in table. First loop result is OK, in next loop ti lacks. -
trante almost 12 years
foreach ($arrayOfIds as $twitter_user_id) { $this->ModelName->twitter_user_id = $twitter_user_id; if ($this->ModelName->exists()) { echo "$twitter_user_id exists<br>"; } else { echo "$twitter_user_id not exists<br>"; $this->ModelName->create( array ('twitter_user_id' => $twitter_user_id)); } $data = array('ModelName' => array('age' => 15, 'last_satatus' => 'online' )); $this->ModelName->save($data); }
-
trante almost 12 yearsI suppose create with parameter false resets current active record. book.cakephp.org/2.0/en/models/…
-
Julian Hollmann almost 12 yearsFor further questions please close this one and open a new one.