首頁 >後端開發 >php教程 >Yii中为什么继承了CFormModel还是可以调用SAVE方法?

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

WBOY
WBOY原創
2016-06-06 20:45:151149瀏覽

<code class="lang-php">    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。但是
            }

        }
</code>

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

回复内容:

<code class="lang-php">    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。但是
            }

        }
</code>

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

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn