Boa dia, tenho uma model onde não tem uma tabela específica definida. Pois o sistema gerará várias tabelas iguais mas com termos diferentes. E para não ter que criar várias models, estou tentando criar uma model e apenas alterar o nome da tabela criando um método __construct. Vejam abaixo:
Essa é minha 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,
));
}
}
E essa a chamada da model na controller…
$model=new Termos('lingua_portugues');
Porém, quando eu chamo o método $model->search() aparece este erro: A tabela "" da classe de active record "Termos", não pôde ser encontrada no banco de dados.
Ou seja, não está passando o nome da tabela, o nome da tablea está passando direitinho, mas não sei o que acontece quando quando chega no método tableName(), ele não recebe o valor certo.
Alguém pode me ajudar??
Agradeço desde já