简体   繁体   English

YII2 - 如何从表单中获取字段值以进行搜索

[英]YII2 - How to get a field value from form for search

I am a new in Yii2 and in MVC, so think that answer to question is simple, but did not found the answer from documentation and tutorials. 我是Yii2和MVC的新手,所以认为问题的答案很简单,但没有从文档和教程中找到答案。 So, I need to select a record with corresponding id from the database table. 所以,我需要从数据库表中选择一个具有相应id的记录。 So, here the steps that I am following: 所以,这里是我遵循的步骤:

1) I created a model (CustomerReport) with CRUD generator for corresponding table and added it on the view where : 1)我为相应的表创建了一个带有CRUD生成器的模型(CustomerReport),并将其添加到视图中:

use app\models\CustomerReport;

2) On the view page I had tested it with 2)在视图页面上我测试了它

$customer_data = CustomerReport::findone(['id' => 2]);

This works OK for me 这对我来说很好

3) I added a Search form on the view: 3)我在视图上添加了一个搜索表单:

<?php $form = ActiveForm::begin(['id' => 'customer-report']); ?>
                <?= $form->field($model, 'id') ?>

                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
                </div>

            <?php ActiveForm::end(); ?>

But I could not find the way to perform a search by id via form. 但我无法通过表单找到通过id执行搜索的方法。 As I understand, I need to provide search field value into 据我了解,我需要提供搜索字段值

$customer_data = CustomerReport::findone(['id' => VALUE FROM ACTIVE FORM]);

Controller知道视图搜索表单返回的对象中的内容

$customer_data = CustomerReport::findone(['id' => $model->id]);

In your file where these form get POSTed (probable XyzController.php) 在你的文件中这些表单获得POSTed(可能是XyzController.php)

if(isset($_POST['CustomerReport']['id'])  // after form get POSTed
    $customer_data = CustomerReport::findone(['id' => $_POST['CustomerReport']['id']]);

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

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