使用CTimestampBehavior出现了Trying to get property of non-object


我使用CTimestampBehavior为表新增一个叫做registerDate的属性。在User表中新增一个registerDate字段,我的CTimestampBehavior是这样写的:


 <?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-29
 * Time: 上午9:54
 *   */
class User extends CActiveRecord
{

    public static function  model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function  tableName()
    {

        return '{{user}}';
    }

    public function attributeLabels()
    {
        return array(
            'userId'=>'用户的漫游号',
            'username'=>'用户昵称',
            'homeland'=>'故乡',
            'birthday'=>'生日',
           'email'=>'用户邮箱',

        );

    }

 public function rules()
    {
        return array(
            array('email', 'required','message' => '密码不能为空'),
            array('email', 'email', 'message' => 'email的格式不合法'),
            array('password', 'required','message' => '密码不能为空')

        );
    }

 public function behaviors(){
    return array(
        'CTimestampBehavior' => array(
            'class' => 'zii.behaviors.CTimestampBehavior',
            'createAttribute' => 'registerDate'

        )
    );
}
}
```
我的UserController是这样写的:
```php
<?php
class UserController extends  Controller{
public function   actionRegister(){
   $userModel= new User();
    if(isset($_POST['User'])){
         $userModel->attributes=$_POST['User'];
             if ($userModel->validate()&&$userModel->save()) {
            echo '保存成功';
   }
    }
   $this->render('register',array('registerForm'=>$userModel));
} 
}

?>

sql是这样的:


 CREATE TABLE user(
  userId     INT UNSIGNED  PRIMARY KEY  NOT NULL  AUTO_INCREMENT ,
  password VARCHAR(32) NOT NULL ,
  email VARCHAR(128) UNIQUE NOT NULL 
  )

没有的字段:
registerDate DATE NOT NULL COMMENT ' 注册日期',

但是却出现了如下的错误:
请输入图片描述

我把 'createAttribute' => 'registerDate'这句话删了还是出现这样的错误!
求大神解答这到底是咋会时啊!!!!!!!!

yii php

招兵买马酱 10 years, 4 months ago

我已经解决了,是我自己对这个类理解错误了

hea7ven answered 10 years, 4 months ago

Your Answer