简体   繁体   English

使用Yii2 activeform为枚举字段的每个项目插入值

[英]inserting values for each of items of an enum field using Yii2 activeform

I have a user model and an attachment model. 我有一个user模型和一个attachment模型。

User : 用户名

id    name     email         password_hash
--------------------------------------
1     Reza     reza@web.af   xxxxxxx

Attachment : 附件

user_id    type(enum)   name
--------------------------------------
1          resume       fk4k34kdfmkg3.pdf
1          photo        59rg3kerfn3ju.jpg
1          nid          34kf2wkefclh0.jpg

I need to create the activeform for it and I am wondering what is the appropriate (best) way to create model fields for them. 我需要为其创建活动表单,我想知道为它们创建模型字段的适当(最佳)方法是什么。 each time a new user is registered three rows of attachments will be inserted to the attachments table too. 每次注册新用户时,附件表中也会插入三行附件。 Of course the following activeform is not doing what i want. 当然,以下activeform不能满足我的要求。 please help. 请帮忙。

<?php $form = ActiveForm::begin(); ?>

<?=$form->field($user, 'name')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'email')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'password_hash')->passwordInput(['maxlength' => true]) ?>

<?= $form->field($attachment, 'type')->fileInput()->label('photo') ?>
<?= $form->field($attachment, 'type')->fileInput()->label('resume') ?>
<?= $form->field($attachment, 'type')->fileInput()->label('nid') ?>
..
..

In your model define three public variable like 模型中定义三个公共变量,例如

public $typePhoto;
public $typeResume;
public $typeNid;

then define rules like 然后定义规则

public function rules()
{
    return [
        [['typePhoto', 'typeResume', 'typeNid'], 'required'],
    ];
}

and then create ActiveForm like this 然后像这样创建ActiveForm

<?php $form = ActiveForm::begin(); ?>

<?=$form->field($user, 'name')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'email')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'password_hash')->passwordInput(['maxlength' => true]) ?>

<?= $form->field($attachment, 'typePhoto')->fileInput()->label('photo') ?>
<?= $form->field($attachment, 'typeResume')->fileInput()->label('resume') ?>
<?= $form->field($attachment, 'typeNid')->fileInput()->label('nid') ?>
..
..

Refer Creating Forms 请参阅创建表格

I think for that purpose you should use yii model form extended from yii\\base\\Model. 我认为出于这个目的,您应该使用从yii \\ base \\ Model扩展的yii模型形式。 It can provide columns different from database ones. 它可以提供与数据库不同的列。

Here is the simplified example of form model that you need: 这是您需要的表单模型的简化示例:

class LoginForm extends \yii\base\Model
{
    public $name;
    public $email;
    public $password_hash;
    public $type_resume;
    public $type_photo;
    public $type_nid;

    public function rules()
    {
        return [
        // define validation rules here
        ];
    }

    public function saveData() 
    {
         $userModel = new User();
         $userModel->name = $this->name;
         //and so on, save all user properties
         $userModel->save();

         $attachModel = new Attachment();
         $attachModel->type = 'resume'; //it better to use constant
         $attachModel->user_id = $userModel->id;
         $attachModel->name = ' '; //set the necessary value
         $attachModel->save();

         //and so on, save your attachment models for all types
    } 
}

So your ActiveForm should be something like this: 因此,您的ActiveForm应该是这样的:

<?php $form = ActiveForm::begin(); ?>

<?=$form->field($loginForm, 'name')->textInput(['maxlength' => true]) ?>
<?=$form->field($loginForm, 'email')->textInput(['maxlength' => true]) ?>
<?=$form->field($loginForm, 'password_hash')->passwordInput(['maxlength' => true]) ?>

<?= $form->field($loginForm, 'type_photo')->fileInput()->label('photo') ?>
<?= $form->field($loginForm, 'type_resume')->fileInput()->label('resume') ?>
<?= $form->field($loginForm, 'type_nid')->fileInput()->label('nid') ?>
..

In code above the $loginForm is object of LoginForm class. 在上面的代码中,$ loginForm是LoginForm类的对象。 Use saveData method of LoginForm to save the data properly. 使用LoginForm的saveData方法正确保存数据。

For more information about yii form models visit http://www.yiiframework.com/doc-2.0/guide-input-forms.html 有关yii表单模型的更多信息,请访问http://www.yiiframework.com/doc-2.0/guide-input-forms.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM