dony
(Voffka1990)
September 29, 2012, 9:03pm
1
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
softark
(Softark)
September 30, 2012, 1:15am
2
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.
dony
(Voffka1990)
September 30, 2012, 7:17am
3
it is an idea. I will try to
and why in the demo blog did not do as you said?
softark
(Softark)
September 30, 2012, 7:50am
4
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.
dony
(Voffka1990)
September 30, 2012, 8:45am
5
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
softark
(Softark)
September 30, 2012, 10:12am
6
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
dony
(Voffka1990)
September 30, 2012, 10:41am
7
softark, thanks.
array(
'name' => 'gallery_id',
'value'=>'$data->albums->title',
'filter'=>'$data->albums->title',
),
why field filter not working?
abennouna
(Abennouna)
September 30, 2012, 11:15am
8
softark
(Softark)
September 30, 2012, 11:23am
9
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
dony
(Voffka1990)
October 1, 2012, 7:16am
10
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'),
),
softark
(Softark)
October 1, 2012, 9:47am
11
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'),
),
dony
(Voffka1990)
October 1, 2012, 10:23am
12
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.
softark
(Softark)
October 1, 2012, 10:56am
13
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.