Relations items

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>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</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&uacute;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,

any ideas?

maybe i should use $data or $model???

thanks




public function relations()

{

  return array(

	'color' => array(self::BELONGS_TO, 'ColorEncimera', 'FkColorEncimera'),

	'medida' => array(self::BELONGS_TO, 'MedidaEncimera', 'FkMedidaEncimera'),

	'tipo' => array(self::BELONGS_TO, 'TipoEncimera', 'FkTipoEncimera'),

  );

}



  • Be aware of the difference between HAS_ONE and BELONGS_TO.

  • Always use the foreign key column name in relationship definitions.

Thanks!

now the relations are working :)


	public function relations()

	{

		return array(

			'color' => array(self::BELONGS_TO, 'ColorEncimera', 'FKColorEncimera'),

			'medida' => array(self::BELONGS_TO, 'MedidaEncimera', 'FKMedidaEncimera'),

			'tipo' => array(self::BELONGS_TO, 'TipoEncimera', 'FKTipoEncimera')

		);

	}

My problem now is that i can not set the relations when a create a new item. I have


CHtml::activeDropDownList($model,'tipo',CHtml::listData( Tipoencimera::model()->findAll(), 'idTipoEncimera', 'Tipo' ), array('prompt'=>'Selecciona Tipo...'));

And my controller in the method create has:




public function actionCreate()

	{

		$model=new Encimera;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Encimera']))

		{

			$model->attributes=$_POST['Encimera'];

			$model->attributes['FKTipoEncimera']= $_POST['Encimera']['tipo'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->IdEncimera));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}



But now the fk field is null…model->attributes has the fields FKTipoEncimera,…but not the relation one (tipo) maybe this is the problem…

I think that the problem is in attribute rules


        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'),

                );



i can not test it right now…i’ll let you know in case anybody had the same problem…

thanks in advance!

in my form i have to put the foreign keys with the original name : FKTipoEncimera




	<div class="row">

		<?php echo $form->labelEx($model,'tipo'); ?>

		<?php echo CHtml::activeDropDownList($model,'FKTipoEncimera',CHtml::listData( Tipoencimera::model()->findAll(), 'idTipoEncimera', 'Tipo' ), array('prompt'=>'Selecciona Tipo...')); ?>

		<?php echo $form->error($model,'tipo'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'medida'); ?>

		<?php echo CHtml::activeDropDownList($model,'FKMedidaEncimera',CHtml::listData( Medidaencimera::model()->findAll(), 'IdMedidaEncimera', 'concatened' ), array('prompt'=>'Selecciona Medida...')); ?>

		<?php echo $form->error($model,'medida'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($model,'color'); ?>

		<?php echo CHtml::activeDropDownList($model,'FKColorEncimera',CHtml::listData( Colorencimera::model()->findAll(), 'IdColorEncimera', 'Color' ), array('prompt'=>'Selecciona Color...')); ?>

		<?php echo $form->error($model,'color'); ?>

	</div>




now it is working! thanks