Answer the question
In order to leave comments, you need to log in
How to collect condition Many-to-Many in yii?
Good afternoon,
There are 3 tables:
m_users
-----------
id
name
mc_photo
-----------
id
name
l_users_22_photo
-----------
id
id_1
id_2
I want to display all user avatars in CActiveDataProvider
,
I tried this:
class Users extends ActiveRecord
{
public $pictures;
...
public function relations()
{
return
[
...
'images'=>[self::MANY_MANY, 'Photo','l_users_22_photo(id_1, id_2)'],
...
]
}
}
class LUsers22Photo extends CActiveRecord
{
public function relations()
{
return
[
'user' => array( self::BELONGS_TO, 'Users', 'id_1' ),
'photo' => array( self::BELONGS_TO, 'Photo', 'id_2' ),
]
}
}
class Photo extends ActiveRecord
{
public function relations()
{
return
[
'users'=>array(self::MANY_MANY, 'Users', 'l_users_22_photo(id_1, id_2)'),
'picture_users'=>array(self::HAS_MANY, 'LUsers22Photo', 'id_2'),
]
}
}
$path = $model->getFolderPath();
$criteria = new CDbCriteria();
$criteria->with =
[
'picture_users' =>
[
'condition' => 'id_1 = '. $model->id,
'group' => 'id_2',
'together'=>true
//'having' => 'COUNT(*) = ' . count( $tags ),
]
];
$criteria->condition = 't.exist = 1';
$images = new CActiveDataProvider('Photo',
[
'criteria' => $criteria,
'pagination' =>
[
'pageSize' => 20,
],
]);
$this->widget('zii.widgets.grid.CGridView',
[
'id' => 'image-grid',
'dataProvider' => $images,
'template' => "{items}",
'columns' =>
[
[
'header' => 'Изображение',
'type' => 'raw',
'value' => function ($data) use ($model) {
//return CHtml::image($data->getFolderPath(), $data->NAME,
return CHtml::image('/images/users/'.$model->id.'/pictures/'.$data->name, '',
['style' => 'height: 100px;']);
}
],
[
'header' => 'Название',
'filter' => false,
'value' => function ($data) {
return $data->name;
}
],
[
'class' => 'CButtonColumn',
'template' => '{delete}',
'deleteButtonUrl' => 'Yii::app()->createUrl("/cabinet/users/deletePicture/", ["id" => $data->id))',
],
],
]);
$this->renderPartial('render_imageGrid');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question