0

我按照指南使用两个模型(用户,用户信息)制作注册表单,但是当我查看注册表单时出现警告(包括(data.php)[function.include]:无法打开流:没有这样的文件或目录)弹出,我真的不明白是什么导致了这个警告。这是我的代码:

1. Register action:

 public function actionRegister(){

           $user=new User;
           $user_info=new UserInfo;

            if(isset($_POST['User'],$_POST['UserInfo'])){


             $user->attributes=$_POST['User'];
             $user_info->attributes=$_POST['UserInfo'];


              $valid=$user->validate();

              $valid=$user_info->validate() && $valid;

               if($valid){

                   $user->save(false);
                   $user_info->save(false);

                   $this->redirect(array('index'));


               }


           }

         $this->render('register', array('user'=>$user,'user_info'=>$user_info));

        }





    2. Register view:



         <?php $form=$this->beginWidget('CActiveForm', array(

            'action'=>$this->createUrl('Site/Register'), 

        'enableClientValidation'=>true,

        'enableAjaxValidation'=>false,

        'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),


           'htmlOptions'=>array('enctype'=>'multipart/form-data')
    )); ?>



        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary(array($user,$user_info)); ?>

        <div class="row">
            <?php echo $form->labelEx($user,'username'); ?>
            <?php echo $form->textField($user,'username',array('size'=>60,'maxlength'=>300)); ?>
            <?php echo $form->error($user,'username'); ?>
        </div>




        3.PHP Warning: 


        include(data.php) [<a href='function.include'>function.include</a>]: failed
        to open stream: No such file or directory
        (C:\wamp\www\Yii-new\framework\YiiBase.php:427)
        Stack trace:
        #0 C:\wamp\www\Yii-new\framework\validators\CValidator.php(186): import()
        #1 C:\wamp\www\Yii-new\framework\base\CModel.php(287): createValidator()
        #2 C:\wamp\www\Yii-new\framework\base\CModel.php(260):
        UserInfo->createValidators()
        #3 C:\wamp\www\Yii-new\framework\web\widgets\CActiveForm.php(506):
        UserInfo->getValidators()
        #4 C:\wamp\www\Yii-new\project2\protected\views\site\register.php(50):
        CActiveForm->error()
        #5 C:\wamp\www\Yii-new\framework\web\CBaseController.php(126): require()
        #6 C:\wamp\www\Yii-new\framework\web\CBaseController.php(95):
        SiteController->renderInternal()
        #7 C:\wamp\www\Yii-new\framework\web\CController.php(869):
        SiteController->renderFile()
        #8 C:\wamp\www\Yii-new\framework\web\CController.php(782):
        SiteController->renderPartial()
        #9
        C:\wamp\www\Yii-new\project2\protected\controllers\SiteController.php(109):
        SiteController->render()
        #10 C:\wamp\www\Yii-new\framework\web\actions\CInlineAction.php(49):
        SiteController->actionRegister()
        #11 C:\wamp\www\Yii-new\framework\web\CController.php(308):
        CInlineAction->runWithParams()
        #12 C:\wamp\www\Yii-new\framework\web\CController.php(286):
        SiteController->runAction()
        #13 C:\wamp\www\Yii-new\framework\web\CController.php(265):
        SiteController->runActionWithFilters()
        #14 C:\wamp\www\Yii-new\framework\web\CWebApplication.php(282):
        SiteController->run()
        #15 C:\wamp\www\Yii-new\framework\web\CWebApplication.php(141):
        CWebApplication->runController()
        #16 C:\wamp\www\Yii-new\framework\base\CApplication.php(180):
        CWebApplication->processRequest()
        #17 C:\wamp\www\Yii-new\project2\index.php(13): CWebApplication->run()
        REQUEST_URI=/Yii-new/project2/site/register
        in C:\wamp\www\Yii-new\project2\protected\views\site\register.php (50)
        in C:\wamp\www\Yii-new\project2\protected\controllers\SiteController.php
        (109)
        in C:\wamp\www\Yii-new\project2\index.php (13)

          [1]: http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/
4

3 回答 3

0

您没有为 User 或 UserInfo 或两者创建模型。所以,请检查 protected/models 文件夹,有可用的 User.php 和 UserInfo.php。

于 2013-11-04T20:22:16.310 回答
0

这是正确的做法;

如果您需要更多数据,并且需要混合模型,请选择 1 个模型作为主要模型;

在该模型中,声明一些变量;

现在,当您初始化模型类的新实例时,默认属性以及新添加的变量都会在那里;

对于新变量,使用新的模型实例初始化它们;

例如:

您在 A 类中,您需要为 B 类和 C 类存储数据以备后用;

你初始化 A 类和所有属性;

你也使用extended_var1 = new Model();extended_var2 = new Model2();

您将此数据传递给表单;

你像往常一样处理标准变量,作为标准的 yii 形式,当扩展变量出现时,请记住它们是对象或数组,但它们就在那里;

在这个例如你在UrlController

public function actionUpdate($id, $type = 'update') {
    $model = $this->loadModel($id);

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if (isset($_POST['Url'])) {
        $model->attributes = $_POST['Url'];
        $model->website = $_POST['Website'];
        if ($model->save())
            if ($type == 'update')
                $this->redirect(array('view', 'id' => $model->id));
            else
                $this->redirect(array('/admin/url/approvePublicLink'));
    }

    $model_website = Website::model()->findByAttributes(array('id'=>$model->website_id));

    $this->render('update', array(
        'model' => $model,
        'model_website' => $model_website,
    ));
}

你的主要模型类是Url

快速例如在Url模型中:

class Url extends CActiveRecord {

    public $website = array();

...

    protected function afterSave() {
        $w = null;
        $w = Website::model()->findByAttributes(array('id' => $this->website_id));
        if($w)
        {
        $w->link = $this->website['link'];
        $w->domain = $this->website['domain'];
        $w->description = $this->website['description'];
        $w->save();
        }
        return parent::afterSave();
    }

}

这是我的观点:

<div class="form">

<?php

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'url-form',
    'enableAjaxValidation'=>false,
));
?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

<div style="position:relative;float:left;">

    <div style="float:left;padding:20px 20px;">

    <h1>URL</h1>

    <div class="row">
        <?php echo $form->labelEx($model,'link'); ?>
        <?php echo $form->textField($model,'link',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'link'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'title'); ?>
        <?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'title'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'description'); ?>
        <?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'description'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'important'); ?>
        <?php echo $form->dropDownList($model,'important',array(0=>'Normal',1=>'Important')); ?>
        <?php echo $form->error($model,'important'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'views'); ?>
        <?php echo $form->textField($model,'views'); ?>
        <?php echo $form->error($model,'views'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'created'); ?>
        <?php echo $model->created; ?>
        <?php echo $form->error($model,'created'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'updated'); ?>
        <?php echo $model->updated; ?>
        <?php echo $form->error($model,'updated'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'will_expire'); ?>
        <?php echo $form->dropDownList($model,'will_expire',array(0=>'No',1=>'Yes')); ?>
        <?php echo $form->error($model,'will_expire'); ?>
    </div>

    </div>

    <div style="float:left;padding:20px 20px;">

    <h1>WEBSITE</h1>

    <div class="row">
        <?php echo $form->labelEx($model,'website_id'); ?>
        <?php echo CHtml::link($model->relation_website->domain,$model->relation_website->link,array('class'=>'avia','target'=>'_blank')); ?>
        <?php echo $form->error($model,'website_id'); ?>
    </div>  

    <div class="row">
        <?php echo $form->labelEx($model_website,'link'); ?>
        <?php echo $form->textField($model_website,'link'); ?> ( ex: http://www.linkbook.co/ )
        <?php echo $form->error($model_website,'link'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model_website,'domain'); ?>
        <?php echo $form->textField($model_website,'domain'); ?> ( ex: linkbook.co )
        <?php echo $form->error($model_website,'domain'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model_website,'description'); ?>
        <?php echo $form->textField($model_website,'description'); ?>
        <?php echo $form->error($model_website,'description'); ?>
    </div>

    </div>

    </div>

    <div style="clear:both;"></div>

    <div class="row">
        <?php echo $form->labelEx($model,'status'); ?>
        <?php echo $form->dropDownList($model,'status',array(-1=>'Banned',0=>'Normal',1=>'Active')); ?>
        <?php echo $form->error($model,'status'); ?>
    </div>  

    <div class="row buttons">
        <?php echo CHtml::submitButton('Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->
于 2013-11-05T07:50:21.470 回答
0

问题与 UserInfo 模型有关。我已经删除了 UserInfo(模型、控制器、视图)并使用 gii 再次创建它,现在它工作正常。

于 2013-11-05T07:48:02.113 回答