replace into

is there a way to use replace instead of insert with $model->save() ?

I think the Yii’s $model->save is work like REPLACE already. For example you are opening an record with id value is 8.

If there really exists a record with id 8, then it will be a UPDATE scenario (which similar with REPLACE which is DELETE and then INSERT)

well, i want to use only one query for adding the row

and this can be done with replace or insert into … on duplicate key

but i can`t figure out how to use this with AR or DAO

i tried to overwriting the insert function but i get this error

Exception ‘CException’ with message 'Property “CDbCommand.execute” is not defined

!note name column has unique index

public function insert($attributes=null) {

	$sql = "INSERT INTO authors

				(name, description, piclink)


				(:name, :description, :piclink) ON DUPLICATE KEY UPDATE count = count + 1";

	$command = Yii::app()->db->createCommand($sql);

	$command->bindValue(":name", $this->name, PDO::PARAM_STR);

	$command->bindValue(":description", $this->description, PDO::PARAM_STR);

	$command->bindValue(":piclink", $this->piclink, PDO::PARAM_STR);

	return $command->execute;


i can use the update method like you say, but i have to make an extra query to mysql to see if the author exists:(

You can user $model->save() two ways. First one is to insert a new record and second is to update an existing record. I sense that what you need is to update an existing record. Yii decides which way to go depending on how you declare your $model. If you declare $model = new Model(); than $model->save() will enter a brand new record into your database. If you use $model = $this->loadModel(); … or you load an existing row from you db than when you call $model->save() Yii will update your existing row loaded via $this->loadModel();

hope this helps.

thank for the quick answer:)

i want to add a new author, but if exists only to update the count field with + 1

i can to this be searching, if exists update else insert

but there is a more quick and elegant way…the way i wrote in my last post the insert function

my bad, the problem was that i forgot the () at the line return $command->execute;

but i can`t figure how to do this only with ar:)

I can’t think of how you can do this in one query with AR. Other than that you can use the AR exists() method to check if record exists and based on this you could either ->save() the new record or if exists use updateCounters() to update +1. If anybody has a better solution with AR I will be happy to see it.

I was looking for a REPLACE INTO syntax too, and I wanted to say that your code works except for a syntax error on the last line. It should be the following (note the brackets) because execute is a method and not a property.


Coincident I encounter this similar problem. I am using yii v1.1.8, and I found that we can change from insert to update by changing the param scenario, I takes few days to find it, so would like to share with yii developer. My eg below:

Assume I wish to check whether puser_id is exist, as puser_id is primary index. If exist, then update will be performed. But before that you need to set the ID first. Try and catch is check whether error 2300 (integrity) is occured.

public function actionCreate()


$model=new PureyUser;

// Uncomment the following line if AJAX validation is needed

// $this->performAjaxValidation($model);










	catch(CDbException $e)


		if(trim($e->getCode()) == "23000")


			$userid = $model->puser_id;




			$model->scenario = "update";










I’ve stumbled upon the same short coming and judgign by the responses to this post, I suppose you can’t!

Another frustrating issue is how it appears you must have a primary key on your table, even if it is completely unnecessary.

Yet another example of frameworks forcing you to deviate from best practices.