Cześć,
mam takie tabele: portfolio (id, name, is_visible, thumb) oraz portfolioImages(id, file, is_visible). Obie są powiazane tabelą portfoliosImages (portfolio_id, portfolioImage_id) w modelu mam zdeklarowane relacje Many_Many
public function relations()
{
return array(
'portfolios' => array(self::MANY_MANY, 'Portfolio', 'portfolio_images_tags(portfolio_image_id, portfolio_id)'),
);
}
Udało mi się w końcu w CGridView wyświetlić zdjęcia wraz z portfolio do których należą o tak:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'portfolio-image-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'header' => Yii::t('app', 'Thumbnail'),
'name' => 'file',
'type' => 'html',
'value' => 'CHtml::image("/uploads/portfolio/$data->file", $data->file, array("class" => "admin-thumb"))',
),
array(
'name' => 'is_visible',
'value' => 'Yii::t("app", Functions::yesOrNo($data->is_visible))',
),
array(
'name' => 'relatedPortfolios',
'filter' => CHtml::listData(Portfolio::model()->findAll(array('order' => 'tag ASC')), 'id', 'tag'),
'type' => 'html',
'value'=>'$data->relatedPortfolios',
),
array(
'class' => 'CButtonColumn',
),
),
));
Mam jednak problem, ponieważ nie wiem jak filtrować zdjęcia po portfolio (kategoriach, tagach czy jak tam zwał). Próbowałem i kombinowałem z tym co znalazłem w sieci, ale za cieńki jestem. Czy możecie mi pomóc odpalić filtry?
Model zdjęć:
<?php
/**
* This is the model base class for the table "portfolio_image".
* DO NOT MODIFY THIS FILE! It is automatically generated by giix.
* If any changes are necessary, you must set or override the required
* property or method in class "PortfolioImage".
*
* Columns in table "portfolio_image" available as properties of the model,
* followed by relations of table "portfolio_image" available as properties of the model.
*
* @property string $id
* @property string $file
* @property integer $is_visible
*
* @property Portfolio[] $portfolios
*/
abstract class BasePortfolioImage extends GxActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'portfolio_image';
}
public static function label($n = 1)
{
return Yii::t('app', 'Portfolio Image|Portfolio Images', $n);
}
public static function representingColumn()
{
return 'file';
}
public function rules()
{
return array(
array('is_visible', 'numerical', 'integerOnly' => true),
array('file', 'length', 'max' => 127),
array('file, is_visible', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, file, is_visible', 'safe', 'on' => 'search'),
);
}
public function relations()
{
return array(
'portfolios' => array(self::MANY_MANY, 'Portfolio', 'portfolio_images_tags(portfolio_image_id, portfolio_id)'),
);
}
public function pivotModels()
{
return array(
'portfolios' => 'PortfolioImagesTags',
);
}
public function attributeLabels()
{
return array(
'id' => Yii::t('app', 'ID'),
'file' => Yii::t('app', 'File'),
'is_visible' => Yii::t('app', 'Is Visible'),
'portfolios' => Yii::t('app', 'Categories'),
'relatedPortfolios' => Yii::t('app', 'Categories'),
);
}
public function getRelatedPortfolios ()
{
$out=CHtml::listData($this->portfolios,'id','tag');
return implode(',<br />', $out);
}
public function search()
{
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('file', $this->file, true);
$criteria->compare('is_visible', $this->is_visible);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
Model portfolio (kategorii czy też tagów)
<?php
/**
* This is the model base class for the table "portfolio_tag".
* DO NOT MODIFY THIS FILE! It is automatically generated by giix.
* If any changes are necessary, you must set or override the required
* property or method in class "PortfolioTag".
*
* Columns in table "portfolio_tag" available as properties of the model,
* followed by relations of table "portfolio_tag" available as properties of the model.
*
* @property string $id
* @property string $tag
* @property integer $is_visible
* @property string $thumb
*
* @property PortfolioImage[] $portfolioImages
*/
abstract class BasePortfolio extends GxActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'portfolio';
}
public static function label($n = 1)
{
return Yii::t('app', 'Portfolio|Portfolios', $n);
}
public static function representingColumn()
{
return 'tag';
}
public function rules()
{
return array(
array('is_visible', 'numerical', 'integerOnly' => true),
array('tag, thumb', 'length', 'max' => 127),
array('tag', 'required'),
array('is_visible, thumb', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, tag, is_visible, thumb', 'safe', 'on' => 'search'),
);
}
public function relations()
{
return array(
'portfolioImages' => array(self::MANY_MANY, 'PortfolioImage', 'portfolio_images_tags(portfolio_image_id, portfolio_id)'),
);
}
public function pivotModels()
{
return array(
'portfolioImages' => 'PortfolioImagesTags',
);
}
public function attributeLabels()
{
return array(
'id' => Yii::t('app', 'ID'),
'tag' => Yii::t('app', 'Name'),
'is_visible' => Yii::t('app', 'Is Visible'),
'thumb' => Yii::t('app', 'Thumbnail'),
'portfolioImages' => Yii::t('app', 'Images'),
);
}
public function search()
{
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('tag', $this->tag, true);
$criteria->compare('is_visible', $this->is_visible);
$criteria->compare('thumb', $this->thumb);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
Dziękuję.