Reputation: 4383
I wanna update a field in inserted raw by afterSave
method,so I develop following code in Driver Model
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord) {
$newid = self::newid($this->id);
$this->updateByPk($this->id, new CDbCriteria(array('condition'=>'driverid = :driverid', 'params'=>array('driverid'=> $newid))));
}
}
and in Controller the method which crud made:
public function actionCreate()
{
$model=new Driver;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Driver']))
{
$model->attributes=$_POST['Driver'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
edit:
private static function CheckNumber($number) {
$array = array(10);
for ($i = 0; $i < 10; $i++)
$array[$i] = 0;
do {
$array[$number % 10]++;
$number /= 10;
} while ((int) ($number / 10 ) != 0);
$array[$number]++;
for ($i = 0; $i < 10; $i++)
if ($array[$i] > 1)
return false;
return true;
}
public static function newid($id) {
while (self::CheckNumber($id) == FALSE) {
$id++;
}
return $id;
}
Upvotes: 3
Views: 13219
Reputation: 1268
public function afterSave(){
if($this->isNewRecord) {
$transaction_entry = new TransactionEntry;
$transaction_entry->branch_id = 1;
$transaction_entry->date_of_transaction = $this->bill_date;
$transaction_entry->account_category_id = 1;
$transaction_entry->description = $this->student->admission_no ."-".$this->course->course_name."-".$this->fee_month."/".$this->fee_year;
$transaction_entry->amount = $this->total_amount;
$transaction_entry->save();
}
Upvotes: 0
Reputation: 120
The insert method in "CActiveRecord" sets the "isNewRecord" property as "false" if the insertion is done successfully.
Therefore the "if ($this->isNewRecord)" condition in your afterSave method is always false and the program will never execute the updateByPk method inside this condition. That's why your afterSave won't work.
Upvotes: 0
Reputation: 17478
As rightly pointed out by Rajat, your updateByPk();
is incorrect. You can instead use this line, to update the driverid
field of the newly created record:
$this->updateByPk($this->id,array('driverid'=>$newid));
So your entire afterSave()
becomes this:
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord) {
$newid = self::newid($this->id);
$this->updateByPk($this->id,array('driverid'=>$newid));
}
}
saveAttributes()
ultimately calls updateByPk()
.
Upvotes: 1
Reputation: 11254
Your updateByPk is messed up..
As its not clear what you want to do..So I am presuming you want to update driver_id value by result of self::newid($this->id);
of just inserted row..
what you should do is:
protected function afterSave() {
parent::afterSave();
if ($this->isNewRecord) {
$newid = self::newid($this->id);
$this->driverid = $new_id;
$this->isNewRecord = false;
$this->saveAttributes(array('driverid'));
}
}
And yeah you are right about usage of afterSave()
isNewRecord is true even after new record is created, but after first time, it will be false always..
From Yii Forum said By Qiang Himself..
Upvotes: 5