Can I get the primary key of the row i just save()?

Since the id of the record is Auto_Increment, I don’t need to write a line like $data->id=…;and save it.

But the problem is I have the other table which is working together with this table and use primary key of that table as a foreign key.

How do i do that?Thanks!

If the save is successful it will load the primary key into the model, so in your case you just use $data->id after saving it.

have u ever realize in the blog postController, actionCreate look like this

if($model->save())

$this->redirect(array('view','id'=>$model->id));

when it saved, view will redirect into new id?

say you have a newly created model in $model




<?php

echo $model->id; // nothing NULL


$model->save(); // AR saves the model to DB and fills in the blanks with defaults


echo $model->id; // INT (the id columns value)

?>



Is that what you where looking for?

NOTE: merged duplicate thread

$last = Yii::app()->db->getLastInsertID();

Thank you all,I’ve solved this problem using gandaliter’s method! Simple and useful.