Greetings to all.
I’m new here and this is my first post.
My problem is:
- I have a MySQl table called Books with the following fields:
-
IdBook (PK)
-
IdAuthor (FK)
-
Title
- I have a MySQl table called Authors with the followings fields:
-
IdAuthor (PK)
-
Name
I generated by Gii the models and CRUD operations.
- Books model:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('IdBook, IdAuthor, Title', 'required'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('IdAuthor, Title', 'safe', 'on'=>'search'),
);
}
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(
'idAuthor' => array(self::BELONGS_TO, 'Authors', 'IdAuthor'),
);
}
public function attributeLabels()
{
return array(
'IdBook' => 'Id Book',
'IdAuthor' => 'Id Author',
'Detalle' => 'Title',
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('IdAuthor',$this->IdAuthor,true);
$criteria->compare('Title',$this->Title,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
- Books view:
*** index.php
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'summaryText'=>'Páxina {page} de {pages} ( {count} resultados totales ).',
'emptyText'=>'Non se atoparon resultados.',
'itemView'=>'_view',
)); ?>
*** _view.php
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('IdBook')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->IdBook), array('view', 'id'=>$data->IdBook)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('IdAuthor')); ?>:</b>
<?php echo CHtml::encode($data->IdAuthor); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('Title')); ?>:</b>
<?php echo CHtml::encode($data->Title); ?>
<br />
</div>
What I want is to replace IdAuthor for Name (Authors table). I have read dozens of post and i can’t get it.
I normally get the error “Books.Name doesn’t exist in the model” or something.
I make searchRel method in the model and then using:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=> Books::model()->searchRel,
'summaryText'=>'Páxina {page} de {pages} ( {count} resultados totales ).',
'emptyText'=>'Non se atoparon resultados.',
'itemView'=>'_view',
));
and it didn’t work.
I need help, please!!!