Почему вылетает ошибка «колонка не найдена в таблице бд»?

Добрый день, не пойму почему выдаёт ошибку на страницах index и view, если есть связанные модели?

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'faq_id' in 'order clause'
The SQL being executed was: SELECT * FROM `faq` ORDER BY `faq_id` LIMIT 10
Error Info: Array
(
    [0] => 42S22
    [1] => 1054
    [2] => Unknown column 'faq_id' in 'order clause'
)
↵
Caused by: PDOException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'faq_id' in 'order clause'


Модель Faq:
<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "faq".
 *
 * @property int $id_faq
 * @property string $put_date
 * @property string $hide
 *
 * @property FaqLang[] $faqLangs
 */
class Faq extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'faq';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['put_date'], 'safe'],
            [['hide'], 'required'],
            [['hide'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id_faq' => Yii::t('app', 'Id Faq'),
            'put_date' => Yii::t('app', 'Put Date'),
            'hide' => Yii::t('app', 'Hide'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFaqLangs()
    {
        return $this->hasMany(FaqLang::className(), ['faq_id' => 'id_faq']);
    }
}


Модель FaqLang:
<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "faq_lang".
 *
 * @property int $id_faq
 * @property string $name
 * @property string $body
 * @property string $language
 * @property int $faq_id
 *
 * @property Faq $faq
 */
class FaqLang extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'faq_lang';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'body'], 'required'],
            [['body'], 'string'],
            [['faq_id'], 'integer'],
            [['name', 'language'], 'string', 'max' => 255],
            [['faq_id'], 'exist', 'skipOnError' => true, 'targetClass' => Faq::className(), 'targetAttribute' => ['faq_id' => 'id_faq']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id_faq' => Yii::t('app', 'Id Faq'),
            'name' => Yii::t('app', 'Name'),
            'body' => Yii::t('app', 'Body'),
            'language' => Yii::t('app', 'Language'),
            'faq_id' => Yii::t('app', 'Faq ID'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFaq()
    {
        return $this->hasOne(Faq::className(), ['id_faq' => 'faq_id']);
    }
}


Контроллер:
<?php

namespace app\modules\admin\controllers;

use Yii;
use app\modules\admin\models\SystemFaqRecord;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;

/**
 * FaqController implements the CRUD actions for SystemFaqRecord model.
 */
class FaqController extends AppAdminController
{


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

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

    /**
     * Displays a single SystemFaqRecord model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

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

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save())
                return $this->redirect(['view', 'id' => $model->id_faq]);
            else 
                throw new NotFoundHttpException("Что-то пошло не так, как ожидалось!"); 
        }

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

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

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

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

    /**
     * Deletes an existing SystemFaqRecord model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

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

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}


index:
<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

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

    <h1><?= Html::encode($this->title) ?></h1>
    <?php Pjax::begin(); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Faq'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'name',
            'body:ntext',
            'put_date',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>


view:
<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

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

    <h1><?= Html::encode($this->title) ?></h1>
    <?php Pjax::begin(); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Faq'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'name',
            'body:ntext',
            'put_date',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>


и на всякий случай update:
<?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model app\modules\admin\models\Faq */
/* @var $model app\modules\admin\models\FaqLang */

$this->title = Yii::t('app', 'Update Faq: ' . $faq->id_faq . $faqLang->id_faq, [
    'nameAttribute' => '' . $faq->id_faq . $faqLang->id_faq,
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Faqs'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $faq->id_faq, 'url' => ['view', 'id' => $faq->id_faq]];
$this->params['breadcrumbs'][] = ['label' => $faqLang->id_faq, 'url' => ['view', 'id' => $faqLang->id_faq]];
$this->params['breadcrumbs'][] = Yii::t('app', 'Update');
?>
<div class="faq-update">

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

    <?= $this->render('_form', [
        'faq' => $faq,
        'faqLang' => $faqLang,
    ]) ?>

</div>


create:
<?php

use yii\helpers\Html;


/* @var $this yii\web\View */
/* @var $model app\modules\admin\models\Faq */
/* @var $model app\modules\admin\models\FaqLang */

$this->title = Yii::t('app', 'Create Faq');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Faqs'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="faq-create">

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

    <?= $this->render('_form', [
        'faq' => $faq,
        'faqLang' => $faqLang,
    ]) ?>

</div>
  • Вопрос задан
  • 1596 просмотров
Пригласить эксперта
Ответы на вопрос 1
@k2lhu
Если у вас название первичного ключа отличается от id, то необходимо явно указать все свои ключи в классе модели:
public static function primaryKey()
{
    return ["id_faq"];
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы