>백엔드 개발 >PHP 튜토리얼 >Yii中为什么继承了CFormModel还是可以调用SAVE方法?

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

WBOY
WBOY원래의
2016-06-06 20:45:151154검색

<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으로 문의하세요.