Sachy
(Sachin Mandalia)
October 27, 2014, 5:28pm
1
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 ?
Sachy
(Sachin Mandalia)
October 27, 2014, 7:25pm
3
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
alirz23
(Alirz23)
October 28, 2014, 7:43am
6
try taking out the condition and see if that works the code looks good to me
Garfield
(Guillaumeasfar)
October 28, 2014, 9:48am
7
Sachy:
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();
}
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