Php Yii 2.0将数据从窗体传递到控制器

Php Yii 2.0将数据从窗体传递到控制器,php,yii2,Php,Yii2,所以最近,我从CodeIgniter迁移到了Yi2.0 我有一个视图表单,用户可以查看和更新数据 我的表单如下所示: <form role="form" name="damnedForm" action=""> <div class="form-group"> <label>SKU Code</label> <input class="form-control" name="pSku" value="&l

所以最近,我从CodeIgniter迁移到了Yi2.0

我有一个视图表单,用户可以查看和更新数据 我的表单如下所示:

<form role="form" name="damnedForm" action="">
   <div class="form-group">
        <label>SKU Code</label>
        <input class="form-control" name="pSku" value="<?= $model->sku ?>">
   </div>

   <div class="form-group">
         <label>Name</label>
         <input class="form-control" name="pName" value="<?= $model->name ?>">
   </div>

   <div>
         <button type="submit" class="btn btn-danger">Submit me</button>
   </div>
</form>
我的问题是:


如何将表单中的所有数据传递给控制器?我是否必须制作一个手动post/get方法并在控制器上捕获它?或者我可以使用ActiveForm类吗?

如果您还不知道,Yii2附带了一个很棒的gode生成器工具,名为Gii。只要您在
dev
环境中,就可以使用
index.php?r=gii
访问它

如果使用此工具为模型创建CRUD,则可以在代码中查看表单是如何在视图和控制器中编写和收集的

我推荐这种方法,因为它是做表单的“yii方式”


欢迎来到Yii

如果您还不知道,Yii2附带了一个很棒的gode生成器工具,名为Gii。只要您在
dev
环境中,就可以使用
index.php?r=gii
访问它

如果使用此工具为模型创建CRUD,则可以在代码中查看表单是如何在视图和控制器中编写和收集的

我推荐这种方法,因为它是做表单的“yii方式”


欢迎来到Yii

如果您还不知道,Yii2附带了一个很棒的gode生成器工具,名为Gii。只要您在
dev
环境中,就可以使用
index.php?r=gii
访问它

如果使用此工具为模型创建CRUD,则可以在代码中查看表单是如何在视图和控制器中编写和收集的

我推荐这种方法,因为它是做表单的“yii方式”


欢迎来到Yii

如果您还不知道,Yii2附带了一个很棒的gode生成器工具,名为Gii。只要您在
dev
环境中,就可以使用
index.php?r=gii
访问它

如果使用此工具为模型创建CRUD,则可以在代码中查看表单是如何在视图和控制器中编写和收集的

我推荐这种方法,因为它是做表单的“yii方式”


欢迎来到Yii

//表单的视图应该是这样的

<div class="row">
    <div class="col-sm-6">
        <div class="the-box">
            <h4 class="small-title">Create New Department</h4>
            <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'department-Department-form',
                'enableClientValidation' => true,
                'enableAjaxValidation' => false,
            ));
            ?>

            <!--<form role="form">-->
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_name'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_name', array(
                    'id' => 'dept_name',
                    'class' => 'form-control',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_name'); ?>
                </small>

            </div>
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_contact'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_contact', array(
                    'id' => 'dept_contact',
                    'class' => 'form-control',
//                        'placeholder' => 'ID Number',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_contact'); ?>
                </small>
            </div>




            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_hod'); ?>
                <?php
                echo $form->dropDownList($modelDepartment, 'dept_hod', $employeeList, $htmlOptions = array(
                    'class' => 'form-control chosen-select'
                ));
                ?>

                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_hod'); ?>
                </small>
            </div>
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_email'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_email', array(
                    'id' => 'dept_email',
                    'class' => 'form-control',
//                        'placeholder' => 'ID Number',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_email'); ?>
                </small>
            </div>

            <button type="submit" class="btn btn-success"><i class="fa fa-sign-in"></i> Create</button>
            <?php $this->endWidget(); ?>
            <!--</form>-->
        </div><!-- /.the-box -->
    </div>
    <div class="col-sm-6">
        <div class="the-box">
            <?php
            echo '<pre>';
            print_r($dtData);
            ?>
        </div>
    </div>
</div>
//Your Model should be something like this format
class DepartmentForm extends CFormModel {

    public $dept_name;
    public $dept_contact;
    public $dept_hod;
    public $dept_email;

    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('dept_name, dept_contact, dept_hod, dept_email', 'required'),
            array('dept_hod', 'numerical', 'integerOnly' => true),
            array('dept_name, dept_contact', 'length', 'max' => 255),
            array('dept_email', 'length', 'max' => 150),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('dept_id, dept_name, dept_contact, dept_hod, dept_email', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels() {
        return array(
            'dept_id' => 'Dept',
            'dept_name' => 'Department Name',
            'dept_contact' => 'Department Contact',
            'dept_hod' => 'Department HOD',
            'dept_email' => 'Department Email',
        );
    }

}
//Your Controller should be something like this
public function actionManageDepartments() {

        $modelDept = new DepartmentForm(); //Initialize the model above
        $sql = new TSqlResource();


        //Handles the Work Profile
        if (Yii::app()->request->getPost('DepartmentForm')) {
            $modelDept->attributes = Yii::app()->request->getPost('DepartmentForm');
            if ($modelDept->validate()) {
                $data = array(
//                    dept_name, dept_contact, dept_hod, dept_email
                    'dept_name' => $modelDept->attributes ['dept_name'],
                    'dept_contact' => $modelDept->attributes ['dept_contact'],
                    'dept_hod' => $modelDept->attributes ['dept_hod'],
                    'dept_email' => $modelDept->attributes ['dept_email'],
                    'created_by' => Yii::app()->session['user']['profile_id'],
                );
                //insert into database
                $dataSql = $sql->postDepartment($data);

                if ($dataSql == true) {
                    YII::app()->user->setFlash('alert alert-success', ' Successfully Created <strong>' . $data['dept_name'] . '</strong>  Department. ');
                } else {
                    YII::app()->user->setFlash('alert alert-danger', 'Sorry  an Error Occured while adding <strong>' . $data['dept_name'] . '</strong> Department. Contact Admin for assistance ');
                }
            }
        }
//        #end work profile post
        $this->render('manageDepartments', array(
            'modelDepartment' => $modelDept,
            'dtData' => $dtData,
                )
        );
    }

创建新部门
创造
//您的模型应该类似于此格式
类DepartmentForm扩展了CFormModel{
公共部门名称;
公共部门联络人;
公共部门;;
公开$dept_电子邮件;
公共职能规则(){
//注意:您应该只为以下属性定义规则:
//将接收用户输入。
返回数组(
数组('dept\u name,dept\u contact,dept\u hod,dept\u email','required'),
数组('dept_hod','numeric','integerOnly'=>true),
数组('dept\u name,dept\u contact','length','max'=>255),
数组('dept_email','length','max'=>150),
//搜索()使用以下规则。
//@todo请删除不应搜索的属性。
数组('dept\u id,dept\u name,dept\u contact,dept\hod,dept\u email','safe','on'=>'search'),
);
}
公共函数attributeLabels(){
返回数组(
“部门id”=>“部门”,
“部门名称”=>“部门名称”,
“部门联系人”=>“部门联系人”,
“部门hod”=>“部门hod”,
“部门电子邮件”=>“部门电子邮件”,
);
}
}
//你的控制器应该是这样的
公共职能部门{
$modelDept=newdepartmentform();//初始化上面的模型
$sql=新的TSqlResource();
//处理工作配置文件
if(Yii::app()->request->getPost('DepartmentForm')){
$modelDept->attributes=Yii::app()->request->getPost('DepartmentForm');
如果($modelDept->validate()){
$data=数组(
//部门名称、部门联系人、部门方法、部门电子邮件
'dept_name'=>$modelDept->attributes['dept_name'],
“部门联系人”=>$modelDept->attributes[“部门联系人”],
'dept\u hod'=>$modelDept->attributes['dept\u hod'],
'dept_email'=>$modelDept->attributes['dept_email'],
'创建人'=>Yii::app()->会话['user']['profile\u id'],
);
//插入数据库
$dataSql=$sql->postDepartment($data);
如果($dataSql==true){
YII::app()->user->setFlash('alert-alert-success','Successfully Created。$data['dept\u-name'].Department');
}否则{
YII::app()->user->setFlash('alert-alert-danger','Sorry在添加'时发生错误。$data['dept\u-name'].Department.联系管理员寻求帮助”);
}
}
}
//#结束工作剖面柱
$this->render('manageDepartments',数组(
“模型部门”=>$modelDept,
“dtData”=>$dtData,
)
);
}

//表单的视图应该是这样的

<div class="row">
    <div class="col-sm-6">
        <div class="the-box">
            <h4 class="small-title">Create New Department</h4>
            <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'department-Department-form',
                'enableClientValidation' => true,
                'enableAjaxValidation' => false,
            ));
            ?>

            <!--<form role="form">-->
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_name'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_name', array(
                    'id' => 'dept_name',
                    'class' => 'form-control',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_name'); ?>
                </small>

            </div>
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_contact'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_contact', array(
                    'id' => 'dept_contact',
                    'class' => 'form-control',
//                        'placeholder' => 'ID Number',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_contact'); ?>
                </small>
            </div>




            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_hod'); ?>
                <?php
                echo $form->dropDownList($modelDepartment, 'dept_hod', $employeeList, $htmlOptions = array(
                    'class' => 'form-control chosen-select'
                ));
                ?>

                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_hod'); ?>
                </small>
            </div>
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_email'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_email', array(
                    'id' => 'dept_email',
                    'class' => 'form-control',
//                        'placeholder' => 'ID Number',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_email'); ?>
                </small>
            </div>

            <button type="submit" class="btn btn-success"><i class="fa fa-sign-in"></i> Create</button>
            <?php $this->endWidget(); ?>
            <!--</form>-->
        </div><!-- /.the-box -->
    </div>
    <div class="col-sm-6">
        <div class="the-box">
            <?php
            echo '<pre>';
            print_r($dtData);
            ?>
        </div>
    </div>
</div>
//Your Model should be something like this format
class DepartmentForm extends CFormModel {

    public $dept_name;
    public $dept_contact;
    public $dept_hod;
    public $dept_email;

    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('dept_name, dept_contact, dept_hod, dept_email', 'required'),
            array('dept_hod', 'numerical', 'integerOnly' => true),
            array('dept_name, dept_contact', 'length', 'max' => 255),
            array('dept_email', 'length', 'max' => 150),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('dept_id, dept_name, dept_contact, dept_hod, dept_email', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels() {
        return array(
            'dept_id' => 'Dept',
            'dept_name' => 'Department Name',
            'dept_contact' => 'Department Contact',
            'dept_hod' => 'Department HOD',
            'dept_email' => 'Department Email',
        );
    }

}
//Your Controller should be something like this
public function actionManageDepartments() {

        $modelDept = new DepartmentForm(); //Initialize the model above
        $sql = new TSqlResource();


        //Handles the Work Profile
        if (Yii::app()->request->getPost('DepartmentForm')) {
            $modelDept->attributes = Yii::app()->request->getPost('DepartmentForm');
            if ($modelDept->validate()) {
                $data = array(
//                    dept_name, dept_contact, dept_hod, dept_email
                    'dept_name' => $modelDept->attributes ['dept_name'],
                    'dept_contact' => $modelDept->attributes ['dept_contact'],
                    'dept_hod' => $modelDept->attributes ['dept_hod'],
                    'dept_email' => $modelDept->attributes ['dept_email'],
                    'created_by' => Yii::app()->session['user']['profile_id'],
                );
                //insert into database
                $dataSql = $sql->postDepartment($data);

                if ($dataSql == true) {
                    YII::app()->user->setFlash('alert alert-success', ' Successfully Created <strong>' . $data['dept_name'] . '</strong>  Department. ');
                } else {
                    YII::app()->user->setFlash('alert alert-danger', 'Sorry  an Error Occured while adding <strong>' . $data['dept_name'] . '</strong> Department. Contact Admin for assistance ');
                }
            }
        }
//        #end work profile post
        $this->render('manageDepartments', array(
            'modelDepartment' => $modelDept,
            'dtData' => $dtData,
                )
        );
    }

创建新部门