help with update action

EDIT: I was running into problems until post#9&10 that was preventing getting anyway with what I want. With that now fixed I still want to be able to set an attribute’s (activated) date on an update action only when the status changes from ‘I’ to ‘A’. So I need to be able to get the current value in the db and compare it with the form value and combine that into my if statement within the controller. Any ideas?


I am trying to set an attribute’s (activated) date on an update action only when it changes from ‘I’ to ‘A’. Below is my current action that is not working. Any help with getting this right?


public function actionUpdate($id) {

	$model = $this->loadModel($id, 'Member');


	if (isset($_POST['Member'])) {

		$model->setAttributes($_POST['Member']);


		if ($model->activated == 'A') $model->activated = date('Y-m-d');


		if ($model->save()) {

			$this->redirect(array('view', 'id' => $model->Member));

		}

	}


	$this->render('update', array(

			'model' => $model,

));

}



You probably forgot to change Organization to Member




  if (isset($_POST['Member'])) {



/Tommy

Thanks Tommy, that is one of the problems but it still did not do the trick. Corrected original question.

Does the model have any errors? You could check by doing this:




if ($model->hasErrors()) {


   $errors = $model->getErrors();


   //then loop through the errors to see them...


}



Not coming up with any errors either, in my logs I can see that the sql being executed for activated is ‘0000-00-00’.

I supposed, within your logs you could see an UPDATE SQL command was dumped by yii? Copy and Paste it to phpmyadmin or chive or anything, then try to execute it, see if it is working manually to make sure…

Yeah, please paste the generated SQL query here. Is Y-m-d the format Mysql is expecting?

Also, why setAttributes and not just:

$model->attributes = $_POST[‘Member’]

Not sure it matters just curious :)

After you assign the date to the model field conditionally what does this show:

print_r($model);

Have you checked the value for $model->activated? It’s possible that it’s not getting set and failing the if statement.

Are you storing both the date Y-m-d and a text value ‘A’ in the same model field?

Are you sure you don’t have a separate status field and activated date field?

Thanks for the help everyone, found the stupid mistake…

I should have had [note the status == ‘A’]


if ($model->status == 'A') $model->activated = date('Y-m-d')

Thanks, I found this at the same time as you I guess, I was typing up a reply with the SQL generated with and without the if clause and finally found it. I appreciate the help.

and BTW the setAttributes is from giix generated code rather than gii