[英]YII2 - How to get a field value from form for search
我是Yii2和MVC的新手,所以認為問題的答案很簡單,但沒有從文檔和教程中找到答案。 所以,我需要從數據庫表中選擇一個具有相應id的記錄。 所以,這里是我遵循的步驟:
1)我為相應的表創建了一個帶有CRUD生成器的模型(CustomerReport),並將其添加到視圖中:
use app\models\CustomerReport;
2)在視圖頁面上我測試了它
$customer_data = CustomerReport::findone(['id' => 2]);
這對我來說很好
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(); ?>
但我無法通過表單找到通過id執行搜索的方法。 據我了解,我需要提供搜索字段值
$customer_data = CustomerReport::findone(['id' => VALUE FROM ACTIVE FORM]);
Controller知道視圖搜索表單返回的對象中的內容
$customer_data = CustomerReport::findone(['id' => $model->id]);
在你的文件中這些表單獲得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.