Hi all, I’m still fairly new to the Yii framework but already love the way Yii works.
Wonder if anyone can help me with a recent issue. I generated all my models and controllers using the Code Generator (gii) and have had no problem until now.
I have a table called ‘tutor_schedule’ in my database with the model name ‘TutorSchedule’. However when I tried to load up anything to do with this model, I get the following error:
Fatal error: Call to undefined method TutorSchedule::tableName() in ‘C:\xampp\yii\framework\db\ar\CActiveRecord.php on line 2310’
I’ve tried ‘index’ and ‘view’ (eg /index.php/tutorschedule/index) and none of them work. I haven’t changed the autogenerated code or made any changes to the DB tables so I’m confused by the error especially as all my other Controllers are working.
<?php
/**
* This is the model class for table "tutor_schedule".
*
* The followings are the available columns in table 'tutor_schedule':
* @property integer $sched_id
* @property integer $tutor_id
* @property string $date
* @property integer $day_id
* @property string $time
*/
class TutorSchedule extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return TutorSchedule the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tutor_schedule';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('tutor_id, day_id', 'numerical', 'integerOnly'=>true),
array('date, time', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('sched_id, tutor_id, date, day_id, time', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'sched_id' => 'Sched',
'tutor_id' => 'Tutor',
'date' => 'Date',
'day_id' => 'Day',
'time' => 'Time',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('sched_id',$this->sched_id);
$criteria->compare('tutor_id',$this->tutor_id);
$criteria->compare('date',$this->date,true);
$criteria->compare('day_id',$this->day_id);
$criteria->compare('time',$this->time,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
The only thing I am using is the standard Controller for this model which was autogenerated:
<?php
class TutorScheduleController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new TutorSchedule;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['TutorSchedule']))
{
$model->attributes=$_POST['TutorSchedule'];
if($model->save())
$this->redirect(array('view','id'=>$model->sched_id));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['TutorSchedule']))
{
$model->attributes=$_POST['TutorSchedule'];
if($model->save())
$this->redirect(array('view','id'=>$model->sched_id));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('TutorSchedule');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new TutorSchedule('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['TutorSchedule']))
$model->attributes=$_GET['TutorSchedule'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=TutorSchedule::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='tutor-schedule-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
According to this comment, try adding an empty tablePrefix to the config OR add a prefix to all tables and use the tablePrefix property - this is the recommended way BTW.
Also, in your models, wrap the table name in two thingy-magingies {{table_name}}
public function tableName()
{
return '{{inventory}}';
}
'components' => array(
// uncomment the following to use a MySQL database
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=xxx',
'emulatePrepare' => true,
'username' => 'xxx',
'password' => 'xxx',
'charset' => 'utf8',
'tablePrefix' => '', // HERE!!!!
'enableProfiling' => false,
'schemaCachingDuration' => false
),
ORey, you were right. It was giving me the error because I had added a class under the components folder with the same name. Didn’t know that would cause issues. Many thanks again.