Yii2查询结果总是返回相同的

时间:2016-05-29 10:41:51

标签: php mysql database activerecord yii2

我正在开发一个基于Yii2的项目。今天,当我测试我的应用程序时,我注意到它中有一个非常有趣的错误。我有一个带有它的产品表,模型和控制器和视图。 (这些是用gii生成的) 当我列出索引操作中的所有记录时,它工作正常。但是这里出现了错误。当我点击编辑或查看操作时,它会渲染数据库中的第一条记录。我是var_dump查询的结果,总是返回提到的结果。只有当我使用createCommand得到了正确的结果。

你认为男人的问题是什么?

控制器

<?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());
    }
}

2 个答案:

答案 0 :(得分:1)

尝试使用find() - &gt;修改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.');
    }
}

答案 1 :(得分:0)

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