iae galera blz.
Eu tenho o seguinte problema, estou construindo um mini projeto onde tenho um relacionamento de tabelas no banco de dados, fiz o crud da tabelas e até ai tudo bem, mas eu preciso construir uma gridview que apresente os dados das duas tabelas do banco de dados, na parte de apresentação de dados está tudo ok o que não estou sabendo como fazer é o filtro de pesquisa da gridview que apresenta os dados do relacionamento:
ocorre o seguinte erro:
Error 500: <h1>CDbException</h1>
<p>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘tb_telefones.telefone’ in ‘order clause’. The SQL statement executed was: SELECT t
.id
AS t0_c0
, t
.nome
AS t0_c1
FROM tb_agenda
t
ORDER BY tb_telefones.telefone LIMIT 10 (C:\wamp\www\yii\framework\db\CDbCommand.php:528)</p><pre>#0 C:\wamp…
isso porque a consulta está pesquisando os campos da tabela do modelo e não da sua tabela relacionada alguém sabe como eu poderia resolver isso.
Segue abaixo o codigo do meu modelo e do admin.php
agenda.php
<?php
/**
* This is the model class for table "tb_agenda".
*
* The followings are the available columns in table 'tb_agenda':
* @property integer $id
* @property string $nome
*/
class agenda extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return agenda the static model class
*/
public $tb_telefone;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tb_agenda';
}
/**
* @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('nome', 'required'),
array('nome', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, nome, tb_telefone', '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(
'tb_telefones' => array(self::HAS_MANY, 'telefone', 'fk_nome'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels(){
return array(
'id' => 'Id',
'nome' => 'Nome',
'telefone' => 'Telefone',
);
}
/*Função para retornar o numero telefonico de acordo com o usuario corrente.*/
public function getTelefone(){
$telefone = Yii::app()->db->createCommand()->select('telefone')->from('tb_telefone')->where('fk_nome=:fk_nome', array(':fk_nome'=>$this->id))->queryScalar();
return $telefone;
}
/**
* 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('id',$this->id);
$criteria->compare('nome',$this->nome,true);
$criteria->with = array('tb_telefones');
$criteria->compare('tb_telefones.telefone', $this->tb_telefone, true );
return new CActiveDataProvider('agenda',array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'tb_telefone'=>array(
'asc'=>'tb_telefones.telefone',
'desc'=>'tb_telefones.telefone DESC',
),
'*',)
)
));
}
}
agenda/admin.php
<?php
$this->breadcrumbs=array(
'Agendas'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List agenda', 'url'=>array('index')),
array('label'=>'Create agenda', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('agenda-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Agendas</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'agenda-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'nome',
array(
'name'=>'tb_telefone',
'value'=>'$data->telefone',
),
array(
'class'=>'CButtonColumn',
),
),
));
?>