2015-04-01 104 views
0

使用Yii 2.0我想從我的視圖中獲取控制器中的一些$ _POST值,但無法執行此操作。我會告訴你我的代碼,並在下面通過它進行討論。

型號/ CaseSearch.php

<?php 

namespace app\models; 

use Yii; 
use yii\base\Model; 
use yii\data\ActiveDataProvider; 
use app\models\Cases; 

/** 
* CaseSearch represents the model behind the search form about `app\models\Cases`. 
*/ 

class CaseSearch extends Cases 
{ 

public $category; 
public $subcategory; 
public $childcategory; 
public $newcategory; 
/** 
* @inheritdoc 
*/ 
public function rules() 
{ 
    return [ 
     [['case_id', 'year'], 'integer'], 
     [['name', 'judgement_date', 'neutral_citation', 'all_ER', 'building_law_R', 'const_law_R', 'const_law_J', 'CILL', 'adj_LR'], 'safe'], 
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public function scenarios() 
{ 
    // bypass scenarios() implementation in the parent class 
    return Model::scenarios(); 
} 

/** 
* Creates data provider instance with search query applied 
* 
* @param array $params 
* 
* @return ActiveDataProvider 
*/ 
public function search($params) 
{ 
    $query = Cases::find(); 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 

    $this->load($params); 

    if (!$this->validate()) { 
     // uncomment the following line if you do not want to any records when validation fails 
     // $query->where('0=1'); 
     return $dataProvider; 
    } 

    $query->andFilterWhere([ 

     'case_id' => $this->case_id, 
     'judgement_date' => $this->judgement_date, 
     'year' => $this->year, 
    ]); 

    $query->andFilterWhere(['like', 'name', $this->name]) 
     ->andFilterWhere(['like', 'neutral_citation', $this->neutral_citation]) 
     ->andFilterWhere(['like', 'all_ER', $this->all_ER]) 
     ->andFilterWhere(['like', 'building_law_R', $this->building_law_R]) 
     ->andFilterWhere(['like', 'const_law_R', $this->const_law_R]) 
     ->andFilterWhere(['like', 'const_law_J', $this->const_law_J]) 
     ->andFilterWhere(['like', 'CILL', $this->CILL]) 
     ->andFilterWhere(['like', 'adj_LR', $this->adj_LR]); 







    return $dataProvider; 
} 

public function searchByCategory($category){ 
    $query = Cases::find(); 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 



    if (!$this->validate()) { 
     // uncomment the following line if you do not want to any records when validation fails 
     // $query->where('0=1'); 
     return $dataProvider; 
    } 

    $query->andFilterWhere([ 

     'category_id' => $category 

    ]); 

    return $dataProvider; 
} 
} 

好了,所以現在是我的看法:

<?php 


    $form = ActiveForm::begin(); 

    echo $form->field($searchModel, 'category') 
       ->dropDownList(
        ArrayHelper::map($allCategory, 'id', 'name'), 
        [ 
         'onchange'=>'getSubcategory()', 
        ] 
    ); 

    //To stop errors, if first category not chosen make subcategory and empty drop down. 
    $subcategory = array(
     "empty" => "" 

    ); 

    echo $form->field($searchModel, 'subcategory') 
       ->dropDownList(
        ArrayHelper::map($subcategory, 'id', 'name'), 
        [ 
         'onchange'=>'getChildcategory()', 
        ] 
    ); 
    //To stop errors, if second category not chosen make childcategory and empty drop down. 
    $childcategory = array(
     "empty" => "" 
    ); 
    echo $form->field($searchModel, 'childcategory') 
       ->dropDownList(
        ArrayHelper::map($childcategory, 'id', 'name'), 
        [ 
         //'onchange'=>'getChildCategory()', 
         'onchange'=>'submitNow()', 
        ] 
    ); 



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

好了,當我點擊提交按鈕,我想捕捉值我的控制器,以便我可以使用它來改變gridview中給出的結果。

當我檢查下拉列表中的元素名稱很奇怪,所以我不知道這是不是一個不同的。例如,對於子類別的名稱實際上是:CaseSearch [子類別]

現在對於我的控制器:

public function actionIndex() 
{ 
// 
// This is for the first render of index 
// 
    $model = new Cases; 
    $searchModel = new CaseSearch(); 
    $allCategory = Category::find()->all(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);  
    // 
    // This is for when i click the submit button on the view. I want it to submit and then grab the subcategory in variable $subtest. I make $subtest = 1 so that when i first render the page it doesn't throw an error and when submitted it should change to the post value 
    // 

    $subtest = 1; 
    if($searchModel->load(Yii::$app->request->post())){ 
     $subtest = $_POST['subcategory']; 

    } 

    return $this->render('index', [ 
     'searchModel' => $searchModel, 
     'dataProvider' => $dataProvider, 
     'allCategory' => $allCategory, 
     'model' => $model, 
     'subtest' => $subtest 
    ]); 
} 

但是當我嘗試的print_r()變量$分測驗我認爲我得到的錯誤:

Undefined index: CaseSearch[subcategory] 

及其行:

$subtest = $_POST['CaseSearch[subcategory]']; 

在我的控制器中。

任何人都可以請建議,因爲我不明白爲什麼?

回答

0

我覺得你的應用程序的設計是可改進的。閱讀Yii指南。 但是我認爲解決問題的辦法是:

$subtest = $_POST['CaseSearch']['subcategory]; 
+0

這對我非常感謝!我知道它可以得到改善,我已經在Yii學習了一個星期了,所以我仍然在處理它所提供的一切。 – 2015-04-02 08:30:23

0

請勿直接在您的控制器中使用$_GET$_POST有一些抽象層過濾該輸入。

$get = $request->get(); 
// equivalent to: $get = $_GET; 

$post = $request->post(); 
// equivalent to: $post = $_POST; 

您應該使用該類和方法來獲取您的參數。

http://www.yiiframework.com/doc-2.0/guide-runtime-requests.html

+0

是的,我知道,但它仍然如果我使用的Yii :: $ APP->請求 - >後() – 2015-04-01 13:45:37

+0

然後,你做了錯事不工作。也許你不會將數據作爲POST發送? – Stony 2015-04-01 13:48:12

+0

但它應該自動執行,不應該嗎?如果你看我的問題,當點擊提交按鈕時,它應該將該請求作爲發送請求發送給控制器是否正確? – 2015-04-01 13:50:38