2016-05-29 80 views
0

我正在致力于基于Yii2的项目。今天当我测试我的应用程序时,我注意到了一个非常有趣的错误。我有一个产品表与它的模型和控制器和视图。 (这些是用gii生成的) 当我列出索引操作中的所有记录时,它工作正常。但是这里有bug。当我点击编辑或查看动作时,它会呈现数据库中的第一条记录。我是var_dump查询的结果,并总是返回提到的结果。只有当我使用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视图文件view.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> 

部分:

<?= 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

尝试修改find​​Model使用功能查找() - >其中,代替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,出现以下消息:试图获取非对象的属性。这很有趣,因为这两个查询基本相同,对吗? –

+0

只是为了调试尝试使用findModel提供更新答案让我知道var_dump的结果.. – scaisEdge

0

我不知道为什么,但不知何故错误解决。我在TermekQuery中有一个函数,它应该查询所有删除字段为空的记录。我删除它,现在工作正常。

相关问题