Post模型里有字段title,content,visits,在规则里定义三者都不能为空
$post=Post::model()->findByPk(10); $post->visits=$post->visits+1; $post->save();
这样会更新不了,因为title和contetn没有赋值,我怎样才能只更新visits这个字段呢?
Post模型里有字段title,content,visits,在规则里定义三者都不能为空
$post=Post::model()->findByPk(10); $post->visits=$post->visits+1; $post->save();
这样会更新不了,因为title和contetn没有赋值,我怎样才能只更新visits这个字段呢?
Post::model()->updateCounters(10, array('visits'=>1));
你这里是用程序来更新某些字段,所以不需要验证。
除了用updateCounters()来更新计数字段外,你还可以用updateByPk()来指定更新特定的字段。另外save()函数的第二个参数也允许你指定保存特定的字段。
Post::model()->updateCounters(10, array('visits'=>1));
好像有问题,应该是
Post::model()->updateCounters(array('visits'=>1),'id=:id', array(':id'=>10));
另外,因为我在afterSave()里增加了更新关联表的操作,所以即使在save()中指定第二个参数的话也还是不行,afterSave()里的操作有冲突
最后还是决定用updateCounters,方便
多谢强