My code is below. I have an array, and by using this array I fill the database table. If some id exists in table, I update its row, if id doesn’t exist than I create row and write data to row.
I empty my table and then run this code:
$arrayOfIds=array('1234', '5678');
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_status' => 'online'
));
$this->ModelName->save($data);
}
Result is echoed to screen:
1234 not exists
5678 exists
When I check table, 2 rows (twitter_user_id =1234 and twitter_user_id =5678) was added to table.
When I re-run my code with two entries in table, I also get this response:
1234 not exists
5678 exists
Do I miss something?
i think that exists() function is working not like you are thinking it must be 🙂
i would change this line:
to something like this:
and if its not working as needed, add some condition to find function.. about your twitter_user_id