Beginner lost with Active Record


(Dion) #1

This is my first try at Yii, so please bear with me.

As a test, I try to find the number of records in a table.

I started with the basic Yii template, used Gii to make a model of a table (see code at the end), and wrote the following code in site/index.php to find the number of records in a table.
<?php $test = LimeSurvey349943::find()->count(); ?>

This results in the error ‘Class ‘LimeSurvey349943’ not found’.

It’s probably something simple, but I’m stuck. How to correct this?

The code Gii generated in models/LimeSurvey349943.php:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "lime_survey_349943".
 *
 * @property int $id
 * @property string $token
 * @property string $submitdate
 * @property int $lastpage
 * @property string $startlanguage
 * @property string $seed
 * @property string $startdate
 * @property string $datestamp
 * @property string $349943X1X2
 * @property string $349943X1X1
 */
class LimeSurvey349943 extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    { 
       return 'lime_survey_349943';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['submitdate', 'startdate', 'datestamp'], 'safe'],
            [['lastpage'], 'integer'],
            [['startlanguage', 'startdate', 'datestamp'], 'required'],
            [['349943X1X1'], 'number'],
            [['token'], 'string', 'max' => 35],
            [['startlanguage'], 'string', 'max' => 20],
            [['seed'], 'string', 'max' => 31],
            [['349943X1X2'], 'string', 'max' => 5],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'token' => 'Token',
            'submitdate' => 'Submitdate',
            'lastpage' => 'Lastpage',
            'startlanguage' => 'Startlanguage',
            'seed' => 'Seed',
            'startdate' => 'Startdate',
            'datestamp' => 'Datestamp',
            '349943X1X2' => '349943 X1 X2',
            '349943X1X1' => '349943 X1 X1',
        ];
    }
}

(Alexander Makarov) #2

Have you imported class from namespace?


(demonking) #3

You need this line, at the start of your file (but after “namespace”)

use app\models\LimeSurvey349943;