Hi all,
I have been working on my yii project with model relations and i can create new items and check that the foreign keys are set but when i use the link view, the relations do not appear.
I have checked the agile book and the blog example as everybody told me in the live chat but i can not make it work.
this is the model
<?php
/**
* This is the model class for table "encimera".
*
* The followings are the available columns in table 'encimera':
* @property integer $IdEncimera
* @property string $Observaciones
* @property integer $FKMedidaEncimera
* @property integer $FKColorEncimera
* @property integer $FKTipoEncimera
*/
class Encimera extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Encimera 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 'encimera';
}
/**
* @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('FKMedidaEncimera, FKColorEncimera, FKTipoEncimera', 'numerical', 'integerOnly'=>true),
array('Observaciones', 'length', 'max'=>400),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('IdEncimera, Observaciones, FKMedidaEncimera, FKColorEncimera, FKTipoEncimera', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'color' => array(self::HAS_ONE, 'ColorEncimera', 'IdColorEncimera'),
'medida' => array(self::HAS_ONE, 'MedidaEncimera', 'IdMedidaEncimera'),
'tipo' => array(self::HAS_ONE, 'TipoEncimera', 'IdTipoEncimera')
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'IdEncimera' => 'Id Encimera',
'Observaciones' => 'Observaciones',
'FKMedidaEncimera' => 'Medida',
'FKColorEncimera' => 'Color',
'FKTipoEncimera' => 'Tipo',
);
}
/**
* 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('IdEncimera',$this->IdEncimera);
$criteria->compare('Observaciones',$this->Observaciones,true);
$criteria->compare('color',$this->color->Color);
$criteria->compare('tipo',$this->tipo->Tipo);
$criteria->compare('medida',$this->medida);
$criteria->compare('FKMedidaEncimera',$this->FKMedidaEncimera);
//$criteria->compare('FKColorEncimera',Colorencimera::model()->findByPk($this->FKColorEncimera)->Color);
$criteria->compare('FKColorEncimera',$this->FKColorEncimera);
$criteria->compare('FKTipoEncimera',$this->FKTipoEncimera);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/*public function getColorTexto()
{
return Colorencimera::model()->findByPk($this->FKColorEncimera)->Color;
}
public function getTipoTexto()
{
return Tipoencimera::model()->findByPk($this->FKTipoEncimera)->Tipo;
}
public function getMedidaTexto()
{
return Medidaencimera::model()->findByPk($this->FKMedidaEncimera)->getConcatened();
}*/
}
And this is the admin.php where i can not see the relations in the item list:
<?php
$this->breadcrumbs=array(
'Encimeras'=>array('index'),
'Administrar',
);
$this->menu=array(
array('label'=>'Listar Encimera', 'url'=>array('index')),
array('label'=>'Crear Encimera', '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('encimera-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Administrar Encimeras</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('Búsqueda avanzada','#',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'=>'encimera-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'IdEncimera',
/*array(
'name'=>'medida',
'value'=>$dataProvider->medida->concatened,
),*/
array(
'name'=>'color',
'value'=>'CHtml::encode([b]$data->color->Color[/b])',
),
/*array(
'name'=>'tipo',
'value'=>'CHtml::encode($model->tipoTexto)',
),
'Observaciones',
array(
'class'=>'CButtonColumn',
),*/
),
)); ?>
$data->color->Color
The color model has a field Color with the text.
Any ideas would be great i really don’t know if starts my project with the blog code because i was all the afternoon trying this…
thanks in advance,
Hibernator,