Yii Framework Forum: Update model acting weird - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Update model acting weird Rate Topic: -----

#1 User is offline   fennellgb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 02-April 10

Posted 12 May 2010 - 03:58 PM

Hi guys, I'm not quite sure why this isn't working. The code makes logical sense to me, but for some reason the save is throwing an error. Can someone help enlighten me on why this is failing?

What I am trying to do:
Check the "opened" column to see if it is null. If it is, update that column upon initial view and then reload the model.
$model = $this->loadModel();

            if($model->opened === NULL)
            {
                $model->opened = new CDbExpression('NOW()');
                
                if($model->save())
                {
                    $model = $this->loadModel();
                }
                else
                {
                    //throw new CHttpException(404,'The message could not be updated.');
                }
            }

0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,522
  • Joined: 12-October 09
  • Location:Croatia

Posted 12 May 2010 - 04:06 PM

And what is the error?
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   fennellgb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 02-April 10

Posted 12 May 2010 - 04:10 PM

View Postmdomba, on 12 May 2010 - 04:06 PM, said:

And what is the error?

I'm not seeing an error logged, which is why I am completely confused. I have logging on for LogError and LogWarning, and I see nothing.
0

#4 User is offline   Theill11 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 03-March 10
  • Location:@ localhost

Posted 12 May 2010 - 04:36 PM

There is no need to reload the model, because it contains the new values.

But if you want to do it anyway, you should use
$model->refresh();

0

#5 User is offline   fennellgb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 02-April 10

Posted 12 May 2010 - 04:40 PM

View PostKomodo, on 12 May 2010 - 04:36 PM, said:

There is no need to reload the model, because it contains the new values.

But if you want to do it anyway, you should use
$model->refresh();


Thanks, I will do that, but the reason for the reload is that "opened" would have "NOW()" in it since it's the expressions being handed over. I will use the refresh though, thanks for info.

And the error is on the $model-save(), I'm not even getting to this part yet... =(
0

#6 User is offline   fennellgb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 02-April 10

Posted 12 May 2010 - 04:52 PM

Found the error. I put <?php echo CHtml::errorSummary($model); ?> as the throw statement and found that a field was sending a buggy info. Sorry for the bother guys... =D
0

#7 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 12 May 2010 - 06:06 PM

how about using saveAttributes instead of save?

$model = $this->loadModel();
if($model->opened === NULL)
{
	if($model->saveAttributes(array('opened'=>new CDbExpression('NOW()'))))
	{
		$model->refresh();
	}else{
		//throw new CHException('The message could not be updated.');
	}
}

php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users