Representing Status In Text

Hello. In demo blog this problem solved as http://www.yiiframework.com/doc/blog/1.1/en/post.model#representing-status-in-text and in view files:


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		array(

			'name'=>'title',

			'type'=>'raw',

			'value'=>'CHtml::link(CHtml::encode($data->title), $data->url)'

		),

		array(

			'name'=>'status',

			'value'=>'Lookup::item("PostStatus",$data->status)',

			'filter'=>Lookup::items('PostStatus'),

		),

		array(

			'name'=>'create_time',

			'type'=>'datetime',

			'filter'=>false,

		),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

In database i have some tables


CREATE TABLE `photo` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(256) NOT NULL,

  `albums_id` int(11) NOT NULL,

  `description` text NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `albums_id` (`albums_id`)

 ADD CONSTRAINT `photo_ibfk_1` FOREIGN KEY (`albums_id`) REFERENCES `albums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


CREATE TABLE `albums` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(256) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

How me write that in table displayed text values instead of a numeric value. for example link on demo blog http://www.yiiframework.com/demos/blog/index.php/post/admin

Have you already established a relation between your Photo and Album?

If yes, then using album "name" (instead of album id) in Photo model is fairly easy.

it is an idea. I will try to

and why in the demo blog did not do as you said?

In blog tutorial, the statuses of post are constants. And there’s no related object regarding the status. So we use look up table to define their human readable labels.

But Photo has a relation to Album, and it already has the title of album.

you can read more? where to write? something like it?


$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        array(

            'name' => 'name',

            'type' => 'image',

        ),

        array(

            'name' => 'albums_id',

            'value'=>'Albums::name',

            'filter'=>true,

        ),

        array(

            'class' => 'CButtonColumn',

        ),

    ),

));

but its not working. i not understand, how can i get value of the field another table ‘value’=>‘Albums::name’? sorry for my english

Something like this should work.




        array(

            'name' => 'albums_id',

            'value'=>'$data->albums->title',

        ),



You have to replace ‘albums’ and ‘title’ according to your code.

Customizing relations() Method

http://www.yiiframework.com/doc/blog/1.1/en/post.model#customizing-x-16x-method

Relational Active Record

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

CDataColumn::value

http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail

softark, thanks.


        array(

            'name' => 'gallery_id',

            'value'=>'$data->albums->title',

            'filter'=>'$data->albums->title',

        ),

why field filter not working?

For field filters, please look at

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

If you want to use a drop down list for the filter, you may do it like this:




array(

    'name' => 'gallery_id',

    'value'=>'$data->albums->title',

    'filter'=>CHtml::listData(Albums::model()->findAll(), 'id', 'title'),

),



http://www.yiiframework.com/doc/api/1.1/CDataColumn#filter-detail

If you want to use a text field for the filter, as bennouna suggests, you should read this wiki:

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview

How can i get a value in filter fieald albums.title in model Photo file admin.php


array(

    'name' => 'gallery.albums_id',

    'value' => '$data->gallery->albums->title',

    'filter' => CHtml::listData(Albums::model()->findAll(), 'id', 'title'),

),

Follow the wiki, and introduce a virtual attribute "albums_id" in Photo model.

This is for searching albums_id of the related model of Gallery.

And modify Photo::search() method using "albums_id".

And in "admin.php" view of Photo:




array(

    'name' => 'albums_id',  // the name of the introduced virtual attribute

    'value' => '$data->gallery->albums->title',

    'filter' => CHtml::listData(Albums::model()->findAll(), 'id', 'title'),

),



something like there?


$criteria->compare('albums_id', $this->gallery->albums_id, true);

In wiki written:


public function getFullName()

   {

      return $this->firstname . " " . $this->lastname;

   }

How can I employ Getter-function in my situation?


 //Photo.php

public function getAlbums() {

        return $this->albums->title;

    }

I see it as but this not working.

Sorry, but I mean this wiki:

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview




class Photo extends CActiveRecord {

...

  public $albums_id;

...

  public function rules() {

    return array(

      ...

      array( 'xxx,yyy,albums_id', 'safe', 'on'=>'search' ),

    );

  }

...

  function relations() {

      return array(

          'gallery'=>array( self::BELONGS_TO, 'Gallery', 'gallery_id' ),

      );

  }

...

  function search() {

    $criteria = new CDbCriteria;

    $criteria->with = array( 'gallery' );

    ...

    $criteria->compare( 'gallery.albums_id', $this->albums_id );

    ...

  }

...

}



The wiki mentioned is truly worth reading. You should definitely read it.