dongying195959 2013-08-24 19:39
浏览 23
已采纳

如何在一个动作中填充2个模型

hello guys, i have 2 models Users and Communities, i want to fill some data into the model Communities by using the actionCreate of the model Users, but there is nothing saved in the model communities, here is my code :

public function actionCreatec()
    {
            if($this->actionlogged()==0||$this->actionisadmin()==1)
            {
        $model=new Users;
                $model2=new Communities; 

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        if(isset($_POST['Users']))
        {

                    $model->attributes=$_POST['Users'];
                    $model2->idUser=$model->idUser;
                    $model2->admin=1;
                    $model2->save();
                    $model->userType='c';
                    $model->gender='n';
                    $model->firstName='null';
                    $model->lastName='null';
                    $model->birthDate='null';
                    $model->active=1;
                    $model->admin=0;

                    if(isset($_POST['profile'])==0)
                        $model->profilePhoto='default.jpg';
                    elseif(isset($_POST['profile'])==1)
                        $model->profilePhoto=CUploadedFile::getInstance($model,                'profilePhoto');

                    if($model->save()){
                        $this->actionfiledir();
                        if(isset($_POST['profile'])==1)
                            $model->profilePhoto->saveAs($this->actionphotodir()."/".$model->profilePhoto);
                        Yii::app()->session['userId']=$model->idUser;
                        $this->redirect(array('view','id'=>$model->idUser));
                    }
                }

        $this->render('createc',array(
            'model'=>$model,
        ));
            }
            else
                $this->redirect(array('site/index'));
    }
  • 写回答

1条回答 默认 最新

  • doushijiao0679 2013-08-25 09:35
    关注

    $model2 probably can't pass validation. You can check if $model2->save(); returns false, and $model2->getErrors() (do it after save method) to see validation errors.

    To skip validation:

    $model2->save(false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line