E
E
EVOSandru62015-06-23 09:01:50
Yii
EVOSandru6, 2015-06-23 09:01:50

Why doesn't the model find the table?

Hey!
I followed the steps of the YII2 guide : guide.yii2.org-info.by/guide-en-start-gii.html ,
After generating the CountrySearch model ( extends Country) and the CountryController controller, an error appeared when switching to country/index :
The table does not exist: {{%country_search}} The advanced
version is used . The CountrySearch class uses the following namespaces:
namespace app\models;

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

Tried to fix to:
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Country;

The error changes to:
Unable to find 'frontend\models\Country' in file: S:\DEVELOPER\openserver\OpenServer\domains\yii2/frontend/models/Country.php. Namespace missing?
Here are the files (all in frontend ): Country
model :
namespace app\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord
{
}

Country Search Model :
namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Country;

class CountrySearch extends Country
{

    public function rules()
    {
        return [
            [['code', 'name'], 'safe'],
            [['population'], 'integer'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $query = Country::find();

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

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'population' => $this->population,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name', $this->name]);

        return $dataProvider;
    }
}

Controller CountrController :
namespace frontend\controllers;

use Yii;
use app\models\Country;
use app\models\CountrySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

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

    /**
     * Lists all Country models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new CountrySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single Country model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

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

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

    /**
     * Updates an existing Country model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $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->code]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

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

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

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

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
matperez, 2015-06-23
@EVOSandru6

The table name is taken from the tableName() static method . If it is not defined in the model, Yii tries to generate the table name on its own, which is what happens to you.
It is strange that your Country model turned out to be empty. Add the table name and everything will work.

/**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'country';
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question