Good morning, I have a model where there is not a specific table set. For the system generates various tables the same but with different terms. And not having to create multiple models, I’m trying to create a model and just change the name of the table creating a method __ construct. See below:
This is my model …
<?php
class Termos extends CActiveRecord
{
public $tabela;
public function __construct($tabela){
$this->tabela = $tabela;
}
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Termos 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 $this->tabela;
}
/**
* @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('rotulo_original, rotulo', 'required'),
array('rotulo_original, rotulo', 'length', 'max'=>350),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id_termo, rotulo_original, rotulo', '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(
'id_termo' => 'Id Termo',
'rotulo_original' => 'Rotulo Original',
'rotulo' => 'Rotulo',
);
}
/**
* 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()
{
/**
* This is the model class for table "lingua_portugues".
*
* The followings are the available columns in table 'lingua_portugues':
* @property integer $id_termo
* @property string $rotulo_original
* @property string $rotulo
*/ // Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->addCondition("rotulo LIKE :rotulo OR rotulo_original LIKE :rotulo");
$criteria->params = array(':rotulo'=>"%".$this->rotulo."%");
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
And that’s the call model in controller…
$model=new Termos('lingua_portugues');
But when I call the $model->search() get this error: The table "" active record class "Terms", could not be found in the database.
In other words, is not passing the table name, the name of TableA is going right, but do not know what happens when the method when it comes tableName(), it does not get the right value.
Can anyone help me?
Thank you in advance