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!
gandaliter
(Tobycmiller)
August 25, 2011, 2:16pm
2
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?
mdomba
(Maurizio Domba Cerin)
August 25, 2011, 7:53pm
5
NOTE: merged duplicate thread
sakaw
(Sakaw 03)
August 26, 2011, 2:56am
6
$last = Yii::app()->db->getLastInsertID();
Thank you all,I’ve solved this problem using gandaliter’s method! Simple and useful.