当前位置:Gxlcms > PHP教程 > Yii中为什么继承了CFormModel还是可以调用SAVE方法?

Yii中为什么继承了CFormModel还是可以调用SAVE方法?

时间:2021-07-01 10:21:17 帮助过:29人阅读

    public function  actionRegister()
    {
        $registerForm = new RegisterForm();
        if (isset($_POST['RegisterForm'])) {
            $registerForm->attributes = $_POST['RegisterForm'];
            $registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
            if ($registerForm->avatar) {
                $preRand = time() . mt_rand(0, 99999);
                $imageName='img_big'.$preRand.$registerForm->avatar->extensionName;
                $registerForm->avatar->saveAs('uploads/' . $imageName);
                $registerForm->avatar = $imageName;
            }
            $path = dirname(Yii::app()->BasePath) . '/uploads/';
            $thumb = Yii::app()->thumb; //与 $thumb=new Cthumb()有什么区别?
            $thumb->image = $path . 'img_small' . $preRand . $registerForm->avatar->extensionName;
            $thumb->width = 130;
            $thumb->height = 95;
            $thumb->mode = 4;
            $thumb->directory = $path;
            $thumb->defaultName = $preRand;
            $thumb->createThumb();
            $thumb->save();
            $registerForm->thumb = $thumb->image;
            $registerForm->registerDate = time();
//save方法会自动验证
            if ($registerForm->save() && $registerForm->login()) {
                Yii::app()->db->getLastInsertID(); //取得插入的Id。但是
            }

        }

这个RegisterForm是继承的CFormModel,怎么还可以调用save方法?
而且我看了源代码CFormModel跟CActiveRecord没有关系啊。。

回复内容:

    public function  actionRegister()
    {
        $registerForm = new RegisterForm();
        if (isset($_POST['RegisterForm'])) {
            $registerForm->attributes = $_POST['RegisterForm'];
            $registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
            if ($registerForm->avatar) {
                $preRand = time() . mt_rand(0, 99999);
                $imageName='img_big'.$preRand.$registerForm->avatar->extensionName;
                $registerForm->avatar->saveAs('uploads/' . $imageName);
                $registerForm->avatar = $imageName;
            }
            $path = dirname(Yii::app()->BasePath) . '/uploads/';
            $thumb = Yii::app()->thumb; //与 $thumb=new Cthumb()有什么区别?
            $thumb->image = $path . 'img_small' . $preRand . $registerForm->avatar->extensionName;
            $thumb->width = 130;
            $thumb->height = 95;
            $thumb->mode = 4;
            $thumb->directory = $path;
            $thumb->defaultName = $preRand;
            $thumb->createThumb();
            $thumb->save();
            $registerForm->thumb = $thumb->image;
            $registerForm->registerDate = time();
//save方法会自动验证
            if ($registerForm->save() && $registerForm->login()) {
                Yii::app()->db->getLastInsertID(); //取得插入的Id。但是
            }

        }

这个RegisterForm是继承的CFormModel,怎么还可以调用save方法?
而且我看了源代码CFormModel跟CActiveRecord没有关系啊。。

CFormModel并没有实现save方法,见文档:http://www.yiiframework.com/doc/api/1.1/CFormModel

人气教程排行