php - Yii how to use model update even if the record is new -


i have system important close realtime possible. reason when i'm fetching data external source want use $model->update instead of executing 2 queries :

$model->find() if(new)     $model->save else     $model->update 

this time consuming...can use $model->update , if record new create it?

i looked @ code update, i'm not sure how override it.

public function update($attributes=null) {     if($this->getisnewrecord())         throw new cdbexception(yii::t('yii','the active record cannot updated because new.'));     if($this->beforesave())     {         yii::trace(get_class($this).'.update()','system.db.ar.cactiverecord');         if($this->_pk===null)             $this->_pk=$this->getprimarykey();         $this->updatebypk($this->getoldprimarykey(),$this->getattributes($attributes));         $this->_pk=$this->getprimarykey();         $this->aftersave();         return true;     }     else         return false; } 

$model->save 

works same way want. if model new insert , if model exist update. http://www.yiiframework.com/doc/guide/1.1/en/database.ar#updating-record

as can see, use same save() method perform insertion , updating operations. if ar instance created using new operator, calling save() insert new row database table; if ar instance result of find or findall method call, calling save() update existing row in table.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -