[SOLVED] AR issues

Hello,

i found strange issue with AR. First what i did i selected my table with relations checked if it exists. Second made insert into database after inserting i need to update that record. Issue is that i need to make new selection of active record with resetScope.





if ($this->getIsNewRecord()) {

 		$model = self::model()->resetScope()->findByPk($this->id);

 		$model->invoice_number = 'BCR-' . $this->id;

 		$model->save();

}



how to make only:







if ($this->getIsNewRecord()) {

        $this->invoice_number = 'BCR-' . $this->id;

        $this->save();

}



You probably face the same problem i had. Do you use afterSave() ? Then isNewRecord still is true. If you are careful, you can change it to false (that’s what would happen internally in AR after afterSave() was called anyway). I did it like this:




if ($this->isNewRecord) {

    $this->isNewRecord=false;

    $this->saveAttributes(array(

        'invoice_number'=>'BCR-'.$this->id,

    ));

}

Just make sure, you put this after the last check for isNewRecord in your code, of course.

How can you find a record in database if is new?

yes i used afterSave() thanks this issue solved.

Answer to this issue was:





protected function afterSave() {

		if ($this->getIsNewRecord()) {

			$this->isNewRecord=false;

			$this->saveAttributes(array(

				'invoice_number'=>'BCR-'.$this->id,

			));

		}

	}