无法使用单个表单来收集yii2中两个模型的数据

时间:2017-06-11 11:28:13

标签: php forms model yii2 relationship

我正在尝试使用单个表单来收集在yii2 basic中共享相同关系的两个模型(StaffmodelGarantormodel)的信息,但我一直收到错误

  

调用未定义的方法app \ models \ StaffGarantor :: find()

提交表单时。

我的代码如下。

//custom model called StaffGarantor

<?php

namespace app\models;

use app\models\Staff;
use app\models\Garantor;
use yii\db\ActiveQuery;
use base\InvalidConfigException;


/**
 * Description of RegisterStudent
 *
 */
class StaffGarantor extends \yii\base\Model {
//class StaffGarantor extends \yii\db\ActiveRecord{
    //put your code here

//Staff Property
    public  $Fname;
    public  $mname;
    public  $others;
    public  $dob;
    public  $Gender;
    public  $state_of_origin;
    public  $lga_of_origin;
    public  $p_address;
    public  $c_address;
    public  $mobile;
    public  $qualification;
    public  $passport;
    public  $staff_id;
    public  $created_by;
    public  $date_created;

    public  $vErrors; //Error reporting

//Garantor property
    public  $first_name;
    public  $others_name;
    public  $g_dob;
    public  $g_gender;
    public  $state_origin;
    public  $lga_origin;
    public  $permenent_address;
    public  $con_address;
    public  $email;
    public  $home_line;
    public  $phone;
    public  $occupation;
    public  $employee_name;
    public  $employee_address;
    public  $photo;
    public  $relationship;

     public function rules(){

       return[
            [['Fname', 'mname', 'others', 'dob', 'Gender', 
'state_of_origin', 'lga_of_origin', 'c_address', 'mobile', 'qualification', 
'staff_id', 'created_by', 'date_created'], 'required'],
            [['Fname', 'mname', 'others'], 'string', 'max' => 30],
            [['dob'], 'string', 'max' => 11],
            [['Gender'], 'string', 'max' => 10],
            [['state_of_origin', 'lga_of_origin','created_by'], 'string', 'max' => 50],
            [['p_address', 'c_address'], 'string', 'max' => 100],
            [['mobile'], 'string', 'max' => 13],
            [['qualification', 'staff_id'], 'string', 'max' => 20],
            [['passport'], 'file', 'extensions' => ['png','jpg','jpeg'], 
'maxSize' => 500 * 1024, 
                'message' => 'Passoport cannot be more than 500KB.'],
            [['staff_id'], 'unique', 'targetClass' => Staff::className(), 
'targetAttribute' => 'staff_id'],
            [['date_created'], 'string', 'max' => 15],
            [['date_created'],'date','format'=>'yyyy-mm-dd'],
            [['g_dob'],'date','format'=>'yyyy-mm-dd'],
            [['staff_id'], 'unique'],
           //Garantor Info
            [['first_name', 'others_name', 'g_dob', 'g_gender', 
'state_origin', 'lga_origin', 'permenent_address', 'con_address', 'phone', 
'occupation', 'photo','relationship'], 'required'],
            [['first_name', 'others_name', 'state_origin', 'lga_origin'], 
'string', 'max' => 50],
            [['g_dob'], 'string', 'max' => 13],
            [['g_gender'], 'string', 'max' => 20],
            [['permenent_address', 'con_address', 'email', 'employee_name', 
'employee_address'], 'string', 'max' => 100],
            [['home_line', 'phone'], 'string', 'max' => 15],
//           [['id','exist']],
            [['g_dob'],'date','format'=>'yyyy-mm-dd'],
            [['occupation', 'relationship'], 'string', 'max' => 30],
            [['photo'], 'file', 'extensions' => ['png','jpg','jpeg'], 
'maxSize' => 500 * 1024, 
                'message' => 'Passoport cannot be more than 500KB.'], 
           [['staff_id'], 'exist', 'skipOnError' => true, 'targetClass' => 
Staff::className(), 'targetAttribute' => ['staff_id' => 'id']],
        ];
    }
    public function register()
    {
        if (!$this->validate()){

            $this->vErrors = $this->errors;
            return  $this;
        }

        $staff = new Staff();

        $staff->Fname                  = $this->Fname;
        $staff->mname                  = $this->mname;
        $staff->others                 = $this->others;
        $staff->dob                    = $this->dob;
        $staff->Gender                 = $this->Gender;
        $staff->state_of_origin        = $this->state_of_origin;
        $staff->lga_of_origin          = $this->lga_of_origin;
        $staff->p_address              = $this->p_address;
        $staff->c_address              = $this->c_address;
        $staff->mobile                 = $this->mobile;
        $staff->passport               = $this->passport;
        $staff->qualification          = $this->qualification;
        $staff->staff_id               = $this->staff_id;
        $staff->created_by             = $this->created_by;
        $staff->date_created           = $this->date_created;
         $staff->passport       =  $staff->myUploader($this->passport);


        $staff->generateAuthKey();

        if ($staff->validate()){

            $garantor    = new Garantor();
        $garantor->first_name                  = $this->first_name;
        $garantor->others_name                      = $this->gothers_name;
        $garantor->g_dob                         = $this->g_dob;
        $garantor->g_gender                      = $this->g_gender;
        $garantor->state_origin                = $this->state_origin;
        $garantor->lga_origin                  = $this->lga_origin;
        $garantor->permenent_address           = $this->permenent_address;
        $garantor->con_address                 = $this->con_address;
        $garantor->home_line                   = $this->home_line;
        $garantor->phone                       = $this->phone;
        $garantor->email                       = $this->email;
        $garantor->occupation                  = $this->occupation;
        $garantor->employee_name               = $this->employee_name;
        $garantor->employee_address            = $this->employee_address;
        $garantor->photo                       = $this->photo;
        $garantor->relationship                = $this->relationship;
        $garantor->date_created                = $this->date_created;
        $garantor->created_by                  = $this->created_by;

        $garantor->photo       =  $garantor->myUploader($this->photo);

        if ($garantor->validate()){
                    $this->vErrors = $garantor->errors;
                    return  $this;
                }else { 
                    $staff->save(); 
                    $garantor->staff_id    =   $staff->id;
                    $garantor->save();  
                    return $staff;
                }
             }else{
//            echo '<pre>'; var_dump($member);echo '</pre>';die();

            $this->vErrors = $staff->errors;
            return  $this;
        }


    }//End Register method
} // End Class

//人员控制中的动作呈现表单

public function actionRegister()
    {
      $model = new StaffGarantor();

        if ($model->load(Yii::$app->request->post())) { 

            $model->passport = UploadedFile::getInstance($model, 'photo');
            $model->photo = UploadedFile::getInstance($model, 'photo');
            $model = $model->register();

            if(!$model->vErrors){

                return $this->redirect(['view', 'id' => $model->id]);

            }else{
                return $this->render('add', [
                    'model' => $model,
                ]);            
            }

        } else {
            return $this->render('add', [
                'model' => $model,
            ]);
        }
    }

//自定义表单收集数据

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Staff;
use app\models\Garantor;
use yii\helpers\ArrayHelper;
use kartik\widgets\DatePicker;
use kartik\file\FileInput;
?>
<div class="staff-add ">


       <?php $form = ActiveForm::begin(); ?>
       <div class="box box-info">
            <div class="box-header with-border">
                   <h3 class="box-title">Enter Staff's Personal Details.
</h3>
            </div>

         <div class="box-body">  
            <?= $form->field($model, 'staff_id')->label('Staff ID No') ?>            
            <?= $form->field($model, 'Fname') ?>
            <?= $form->field($model, 'mname') ?>
            <?= $form->field($model, 'others') ?>   
            <?= $form->field($model, 'dob') ?>
            <?= $form->field($model, 'Gender')-
>dropDownList(['Male'=>'Male', 'Female'=>'Female'],
                    ['prompt'=>'-Please, select Gender']) ?>
            <?= $form->field($model, 'state_of_origin') ?>   
            <?= $form->field($model, 'lga_of_origin') ?>
            <?= $form->field($model, 'p_address') ?>
            <?= $form->field($model, 'c_address') ?>   
            <?= $form->field($model, 'mobile') ?>   
            <?= $form->field($model, 'qualification') ?>
            <?= $form->field($model, 'created_by') ?>
            <?= $form->field($model, 'date_created') ?>   

            <br><hr>
            <?= $form->field($model, 'passport')->fileInput()->label('Add a 
passport photograph<hr>')?> 
         </div>
        </div>


    <div class="box box-info">
        <div class="box-header with-border">
              <h3 class="box-title">Staff's Guarantors Details</h3>
       </div>

        <div class="box-body">         

            <?= $form->field($model, 'first_name') ?>
            <?= $form->field($model, 'others_name') ?>
            <?= $form->field($model, 'g_dob') ?>
            <?= $form->field($model, 'g_gender') ?>
            <?= $form->field($model, 'state_origin') ?>
            <?= $form->field($model, 'lga_origin') ?>
            <?= $form->field($model, 'permenent_address') ?>
            <?= $form->field($model, 'con_address') ?>
            <?= $form->field($model, 'home_line') ?>
            <?= $form->field($model, 'phone') ?>
            <?= $form->field($model, 'email') ?>
            <?= $form->field($model, 'occupation') ?>
            <?= $form->field($model, 'employee_name') ?>
            <?= $form->field($model, 'employee_address') ?>
            <?= $form->field($model, 'relationship') ?>
            <br><hr>
             <?= $form->field($model, 'photo')->fileInput()->label('Add a 
passport photograph<hr>')?>
        </div>
        </div> 

    <div class="form-group">
            <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn 
btn-primary']) ?>
        </div>
    <?php ActiveForm::end(); ?>

</div><!-- members-register -->

//实际的StaffModel

<?php

namespace app\models;

use Yii;
use app\models\Garantor;

/**
 * This is the model class for table "staff".
 *
 * @property integer $id
 * @property string $Fname
 * @property string $mname
 * @property string $others
 * @property string $dob
 * @property string $Gender
 * @property string $state_of_origin
 * @property string $lga_of_origin
 * @property string $p_address
 * @property string $c_address
 * @property string $mobile
 * @property string $qualification
 * @property string $passport
 * @property string $staff_id
 * @property string $created_by
 * @property string $date_created
 *
 * @property Garantor[] $garantors
 */
class Staff extends \yii\db\ActiveRecord implements 
\yii\web\IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'staff';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Fname', 'mname', 'others', 'dob', 'Gender', 
'state_of_origin', 'lga_of_origin', 'c_address', 'mobile', 'qualification', 
'staff_id', 'created_by', 'date_created'], 'required'],
            [['Fname', 'mname', 'others'], 'string', 'max' => 30],
            [['dob'], 'string', 'max' => 11],
            [['Gender'], 'string', 'max' => 10],
            [['state_of_origin', 'lga_of_origin', 'passport', 'created_by'], 
'string', 'max' => 50],
            [['p_address', 'c_address'], 'string', 'max' => 100],
            [['mobile'], 'string', 'max' => 13],
            [['qualification', 'staff_id'], 'string', 'max' => 20],
            [['date_created'], 'string', 'max' => 15],
            [['staff_id'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'Fname' => 'Fname',
            'mname' => 'Mname',
            'others' => 'Others',
            'dob' => 'Dob',
            'Gender' => 'Gender',
            'state_of_origin' => 'State Of Origin',
            'lga_of_origin' => 'Lga Of Origin',
            'p_address' => 'P Address',
            'c_address' => 'C Address',
            'mobile' => 'Mobile',
            'qualification' => 'Qualification',
            'passport' => 'Passport',
            'staff_id' => 'Staff ID',
            'created_by' => 'Created By',
            'date_created' => 'Date Created',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
     public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    public static function findIdentity($id) {

    }

     public static function findIdentityByAccessToken($token, $type = null) 
{

    }

    public function getGarantors()
    {
        return $this->hasMany(Garantor::className(), ['staff_id' => 'id']);
    }
// Student Picture Uploader
     public function myUploader($file){

        if($file){

            $tempName = Yii::$app->security-
>generateRandomString().time().'.'.$file->extension;

            $file->saveAs('uploads/'.$tempName);

            return $tempName;
        }else{
            return "No passport photographe";
        }       
    }

     // Parents or Gaurdian Picture Uploader
     public function pUploader($pfile){

        if($pfile){

            $ptempName = Yii::$app->security-
>generateRandomString().time().'.'.$pfile->extension;

            $pfile->saveAs('uploads/'.$ptempName);

            return $ptempName;
        }else{
            return "No passport photographe";
        }       
      }

    }

2 个答案:

答案 0 :(得分:0)

您的StaffGarantor课程来自yii\base\Model。要使用ActiveRecord方法find(),您的课程需要延长yii\db\ActiveRecord

class StaffGarantor extends \yii\db\ActiveRecord {}

答案 1 :(得分:0)

我建议你不要为两个模型收集数据的单独模型。

您可以在一个活动表单中使用两个模型