Of course the second time it fails. I know the solution would be to do this:
$model=Model::model()->find("uniqueKeyOne='A' AND uniqueKeyTwo='B' AND uniqueKeyThree='C'");
if (empty($model)) {
$model=new Model;
}
$model->uniqueKeyOne='A';
$model->uniqueKeyTwo='B';
$model->uniqueKeyThree='C';
$model->save();
But I don’t want to do that. It would be nice if I could do something like this:
what exactly are you trying to accomplish here? create the model if it doesn’t exists, or update some fields if it does? what exactly would be the functionality of a “replace()” method?
Using a REPLACE statement does exactly that. It saves the DB call to see if a record exists first, it automatically deletes and inserts a new record if it does. http://dev.mysql.com/doc/refman/5.0/en/replace.html
The table I’m using does have a primary key, regardless of that these three fields should be a combined unique key.
The process I’m using this in can take as long as 20 minutes everytime it runs. Reducing queries speeds everything up.