SOLVED: HOW to retrieve new ID (Pk value)

Hi, I have a master-detail structure.

I want to copy one master record to another, I crated the copy for the master (header) but now I need to copy the detail, from one header to another, so I need to access old header id and newly created header Id:

I have this on the createAction in the header controller




					if ($model->save()) {

						//Copia detalle

						if (isset($old_id))//Es copia

							$model->copiarDetalle($old_id, $model->id);

							

						if (Yii::app()->getRequest()->getIsAjaxRequest())

							Yii::app()->end();



the function:

$model->copiarDetalle($old_id, $model->id);

Just want to copy details from "old_id" to new "id", but $model->id is "0" after save() method !!

I look in Cmodel class, but i haven’t found any method that look for what I need, …

Best Regards.

SOLVED:

reading source " framework/db/ar/CActiveRecord.php" I found this after insert:




if(is_string($primaryKey) && $this->$primaryKey===null)

                                                $this->$primaryKey=$builder->getLastInsertID($table);



so I change one line in my code:




$model = $this->loadModel($id, 'CuotaDef');

			$old_id = $model->id;

			$model->setIsNewRecord(true); // cuando es Copia, marca como nuevo

			$model->id = null; //0  <--- change from 0 to null !




I hope this work for someone else.