beforesave() does not work

Not sure whats wrong in my code below! beforesave() is called but the columns are not updated e.g slug column below is not updated,





public  function beforeSave()

	{

		// return false;

		

		if ($this->isNewRecord)

		{


			// $this->CreateDate  = date('Y-m-d H:i:s');

			$this->CreateDate  = new CDbExpression('NOW()');

			$this->User_Userid=Yii::app()->user->getId();

		}

		else

		{

			// return false;

			$this->slug="Hello";

			// $this->UpdateDate= new CDbExpression('NOW()');

			// $this->UpdateDate  = date('Y-m-d H:i:s');




		}




		

   

   return parent::beforeSave();


	}



Since ‘slug’ should be set if model is not a newRecord, does this mean that when model is newRecord beforeSave works ?

yep!

I tried to return false on both the cases and it definitely enters the loop… however, the update is also not working … not sure whats wrong!!?

This code seems ok, U can try print_r($this->attributes) before U return parent::beforeSave()…just to be sure that value is assigned.

But I think that U have some code after beforeSave() that is making confusion (giving another values to ‘slug’)

Check if parent::beforeSave(); returns true

try taking out the condition and see if that works the code looks good to me

before your code ! like say pavel.rybakov




protected function beforeSave()

{

    if(parent::beforeSave()) // <=== check this line !!!

	{

    	if ($this->isNewRecord)

    	{

    		// place your code here

    		// return true

    	}

    	else

    	{ 

    		$this->slug="Hello";

    		// $this->UpdateDate= new CDbExpression('NOW()');

    		// $this->UpdateDate  = date('Y-m-d H:i:s');

        	// return true !!!


    	}

    }

    else

    {

    	//return false here because you check before if BeforeSave::parent was valid

    }

}



beforeSave documentation : http://www.yiiframew…foreSave-detail