Yii Framework Forum: Activerecord::beforesave Doesn't Allow To Change Isnewrecord - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Activerecord::beforesave Doesn't Allow To Change Isnewrecord Rate Topic: -----

#1 User is offline   cnlevy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 14-October 12

Posted 06 January 2013 - 04:31 AM

This is my code:
public function beforeSave() {
	if ($this->isNewRecord) { // check if value already exists in the db
		$t=$this->findByAttributes($this->getAttributes(array('column','table','value')));
		if ($t) { // update only
			$this->id=$t->id;
			$this->setIsNewRecord(false);
		}
	}
	return parent::beforeSave();
}


The record still gets inserted in the database, because beforeSave() is called from the insert() method !

So, should this validation be done in beforeValidate, or the CActiveRecord implementation should be modified to call beforeSave() inside the save() method ?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users