does yii support $this->save() inside the model?

I never get it work

class KafIpAccess extends CActiveRecord{

private $time, $conf;





public function __construct($scenario = 'insert'){


	$this->conf = Yii::app()->params['loginSecPolicy']['loginFailedLockIp'];


	$this->time = time();





	parent::__construct($scenario);


}





public static function model($className = __CLASS__){


	return parent::model($className);


}





public function tableName(){


	return '{{ipaccess}}';


}





public function rules(){


	return array(


		array('address, logtype, expired', 'required'),


		array('expired', 'numerical'),


		array('context', 'default', 'value' => ''),


	);


}





private function _getIp(){


	return is_null($ip) ? Yii::app()->request->userHostAddress : $ip;


}





public function loginFailedPlus($ip = null){


	$this->attributes = array(


		'address' => $this->_getIp($ip),


		'logtype' => 'login_failed',


		'expired' => $this->time + $this->conf['scope'],


	);


	return $this->save();


}

}

this is part of my code, when I call $this->save(); it didn’t insert a new record for me.

You forgot to declare the formal parameter $ip in the _getIp() method.

Next, it may be a good idea (at least for now) to call $this->validate, check the return value and if true, call $this->save(false). If you added a beforeSave() method, make sure what is returned evaluates to true.

/Tommy

The miss formal param will not affect the test result in fact :)

I went through the sql logs and found that it perform a update operation where PK=null as it’s condition, so I had to add this: $this->setIsNewRecord(true); to make it work, the piece of code pasted as bellow:








	public function loginFailedPlus($ip = null){

		$this->attributes = array(

			'address' => $this->_getIp($ip),

			'logtype' => 'login_failed',

			'expired' => $this->time + $this->conf['scope'],

		);

		$this->setIsNewRecord(true);

		return $this->save();

	}




Are you trying to say there isn’t a single combination of null and Yii::app()->request->userHostAddress that makes the validation fail?

You didn’t tell how your AR instance was created. I think you can guess the state of isNewRecord if it’s newly created.

/Tommy