Problem assigning a safe attibutes

I’m a bit of a noob to the framework so forgive me if I am doing something fundamentally wrong, anyway I have been trying to set this attribute but it won’t set unless I do some hacky stuff. Anyway in my FinanccialReturnController I do this.




public function actionCreate()

{

        $model = new FinancialReturn;

        if( Yii::app()->request->isPostRequest )

        {

            $model->setAttributes( $_POST ,true );

            $this->_processModel( $model , $_POST );

        }



I have an attribute called financial_year_end that I want saved to the DB but it’s not being set. To get to the heart of the matter I went into the CModel code and added some log entries in setAttributes




public function setAttributes($values,$safeOnly=true)

{

	if(!is_array($values))

		return;

	$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());

	foreach($values as $name=>$value)

	{		

		if(isset($attributes[$name]))

		{

			Yii::log("BEFORE: $name=$value ");

			$this->$name=$value;

			Yii::log("AFTER: $name=".$this->$name);

		}

		else if($safeOnly)

			$this->onUnsafeAttribute($name,$value);

	}

}



And it reports in the log

BEFORE: financial_year_end=1143806400

AFTER: financial_year_end=

my rules in the FinancialReturn model are


    

    public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array_merge(array(

            array('financial_year_end', 'required'),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('financial_year_end', 'safe'),

            array('id', 'safe', 'on'=>'search'),

        ),parent::rules());

    }



Anyway I’m pretty confused as to why financial_year_end is not getting set, any help would be very much appreciated:)

If financial_year_end is in your form and the user entered it, then it should be validated. Your validation at this point is only that it is a required field, which is technically OK. Because it is being validated in some way it is considered "safe". If it is supplied from some other source than user input (and therefore does not go through validation) then it is not considered "safe" unless you explicitly say it is. So you do not need to have it validated AND designate it as "safe". But that is probably not your problem.

It seems like the magic __set is not being invoked on your FinancialReturn object. Does it extend CActiveRecord? Was it generated by Gii?

Yeah I though I didn’t need to mark the column as safe if it is marked required, I put it in just in case my understanding was wrong.

Actually I just had a look and for some reason it’s saving now, I’ll look at the change log and try to figure out what changed. I hadn’t really changed that much. Other than before it extended from TimeStampedActiveRecord(extended from CActiveRecord) which set the default values for the time stamping columns. Then I just put another class FinancialReturnItem in between to just add a couple of common function to a group of Models and extended from that. That is the only thing that relates to the FinancailReturn I can think of that has changed, I’ll take a look at my change log when I get a chance today to see if I can identify anything else that may have effected it.