2016-05-29 52 views
0

私はYii2ベースのプロジェクトに取り組んでいます。今日、私がアプリケーションをテストしていたとき、私は非常に興味深いバグに気付きました。私はモデルとコントローラとビューのProductsテーブルを持っています。 (これらはgiiで生成されます) インデックスアクション内のすべてのレコードを表示すると、正常に動作します。しかし、ここにはバグがあります。編集または表示アクションをクリックすると、データベース内の最初のレコードがレンダリングされます。私は、クエリの結果をvar_dumpして、常に、その結​​果をreturndしました。私がcreateCommandを使用したときだけ、適切な結果が得られました。Yii2の照会結果は常に同じ結果を返します

あなたはそれに問題があると思いますか?

コントローラ

<?php 

namespace backend\controllers; 

use Yii; 
use app\models\Termek; 
use yii\data\ActiveDataProvider; 
use yii\db\Query; 
use yii\web\Controller; 
use yii\web\NotFoundHttpException; 
use yii\filters\VerbFilter; 

/** 
* TermekController implements the CRUD actions for Termek model. 
*/ 
class TermekController extends Controller 
{ 
    /** 
    * @inheritdoc 
    */ 
    public function behaviors() 
    { 
     return [ 
      'verbs' => [ 
       'class' => VerbFilter::className(), 
       'actions' => [ 
        'delete' => ['POST'], 
       ], 
      ], 
     ]; 
    } 

    /** 
    * Lists all Termek models. 
    * @return mixed 
    */ 
    public function actionIndex() 
    { 
     $dataProvider = new ActiveDataProvider([ 
      'query' => Termek::find(), 
     ]); 

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

    /** 
    * Displays a single Termek model. 
    * @param integer $id 
    * @return mixed 
    */ 
    public function actionView($id) 
    { 
     $model = $this->findModel($id); 

     return $this->render('view', [ 
      'model' => $model, 
     ]); 
    } 

    /** 
    * Creates a new Termek model. 
    * If creation is successful, the browser will be redirected to the 'view' page. 
    * @return mixed 
    */ 
    public function actionCreate() 
    { 
     $model = new Termek(); 

     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
      return $this->redirect(['view', 'id' => $model->id]); 
     } else { 
      return $this->render('create', [ 
       'model' => $model, 
      ]); 
     } 
    } 

    /** 
    * Updates an existing Termek model. 
    * If update is successful, the browser will be redirected to the 'view' page. 
    * @param integer $id 
    * @return mixed 
    */ 
    public function actionUpdate($id) 
    { 
     $model = $this->findModel($id); 

     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
      return $this->redirect(['view', 'id' => $model->id]); 
     } else { 
      return $this->render('update', [ 
       'model' => $model, 
      ]); 
     } 
    } 

    /** 
    * Deletes an existing Termek model. 
    * If deletion is successful, the browser will be redirected to the 'index' page. 
    * @param integer $id 
    * @return mixed 
    */ 
    public function actionDelete($id) 
    { 
     $this->findModel($id)->delete(); 

     return $this->redirect(['index']); 
    } 

    /** 
    * Finds the Termek model based on its primary key value. 
    * If the model is not found, a 404 HTTP exception will be thrown. 
    * @param integer $id 
    * @return Termek the loaded model 
    * @throws NotFoundHttpException if the model cannot be found 
    */ 
    protected function findModel($id) 
    { 
     if (($model = Termek::findOne($id)) !== null) { 
      return $model; 
     } else { 
      throw new NotFoundHttpException('The requested page does not exist.'); 
     } 
    } 
} 

のindex.phpビューファイル

<?php 

use yii\helpers\Html; 
use yii\grid\GridView; 

/* @var $this yii\web\View */ 
/* @var $dataProvider yii\data\ActiveDataProvider */ 

$this->title = Yii::t('app', 'Termeks'); 
$this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="termek-index"> 

    <h1><?= Html::encode($this->title) ?></h1> 

    <p> 
     <?= Html::a(Yii::t('app', 'Create Termek'), ['create'], ['class' => 'btn btn-success']) ?> 
    </p> 
    <?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 

      'id', 
      'nev', 
      'szelesseg', 
      'magassag', 
      'egyeb:ntext', 
      // 'ar', 
      // 'termek_kategoria_id', 
      // 'torolt', 

      ['class' => 'yii\grid\ActionColumn'], 
     ], 
    ]); ?> 
</div> 

view.phpビューファイルの一部:

<?= DetailView::widget([ 
     'model' => $model, 
     'attributes' => [ 
      'id', 
      'nev', 
      'szelesseg', 
      'magassag', 
      'egyeb:ntext', 
      'ar', 
      'termek_kategoria_id', 
      'torolt', 
     ], 
    ]) ?> 

モデルファイル:

<?php 

namespace app\models; 

use Yii; 

/** 
* This is the model class for table "termek". 
* 
* @property integer $id 
* @property string $nev 
* @property double $szelesseg 
* @property double $magassag 
* @property string $egyeb 
* @property string $ar 
* @property integer $termek_kategoria_id 
* @property string $torolt 
*/ 
class Termek extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'termek'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['nev', 'termek_kategoria_id'], 'required'], 
      [['szelesseg', 'magassag'], 'number'], 
      [['egyeb'], 'string'], 
      [['ar', 'termek_kategoria_id'], 'integer'], 
      [['torolt'], 'safe'], 
      [['nev'], 'string', 'max' => 255], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => Yii::t('app', 'ID'), 
      'nev' => Yii::t('app', 'Nev'), 
      'szelesseg' => Yii::t('app', 'Szelesseg'), 
      'magassag' => Yii::t('app', 'Magassag'), 
      'egyeb' => Yii::t('app', 'Egyeb'), 
      'ar' => Yii::t('app', 'Ar'), 
      'termek_kategoria_id' => Yii::t('app', 'Termek Kategoria ID'), 
      'torolt' => Yii::t('app', 'Torolt'), 
     ]; 
    } 

    /** 
    * @inheritdoc 
    * @return \app\models\Query\TermekQuery the active query used by this AR class. 
    */ 
    public static function find() 
    { 
     return new \app\models\Query\TermekQuery(get_called_class()); 
    } 
} 
+0

'$ id'の値を確認しました –

+0

findModelが取得した渡されたパラメータは正しいです。しかし、それが返される結果(私がvar_dumpedした直後)は、データベースの最初のレコードです。 –

+0

の関連モデルとコントローラ/アクションコードをご覧ください。 – scaisEdge

答えて

0

(検索を使用してfindModel機能を変更してください) - >ここで、代わりのfindOne

protected function findModel($id) 
{ 
    var_dump($id); 
    $model = Termek::find()->where(['id'=> $id])->one(); 
    var_dump($model); 
    if (($model !== null) { 
     return $model; 
    } else { 
     throw new NotFoundHttpException('The requested page does not exist.'); 
    } 
} 
+0

次のメッセージで私にErrorExceptionを投げます:非オブジェクトのプロパティを取得しようとしています。これは2つのクエリが基本的に同じなので興味深いですね。 –

+0

ちょうどデバッグのためにfindModelを使用してください。アップデートで提供してください。答えはvar_dumpの結果を知ってください。 – scaisEdge

0

私は必ず理由を何とかエラーが解決しないです。私は、削除されたフィールドnullを持つすべてのレコードを照会するはずだったTermekQueryの関数を持っていました。私はそれを削除し、現在正常に動作します。

関連する問題