@EVOSandru6

Как в yii собрать condition Много ко Многим?

Добрый день,

Есть 3 таблицы:

m_users
-----------
id
name


mc_photo
-----------
id
name


l_users_22_photo
-----------
id
id_1
id_2


Хочу вывести в CActiveDataProvider все аватарки пользователя,

Пробовал так:

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


_imageGrid.php

$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))',
        ],
    ],
]);


Модель точно рабочая, внешний _form.php работает без $this->renderPartial('render_imageGrid');

Формируется такой запрос:

SELECT "t"."name" AS "t0_c1",
"t"."id" AS "t0_c0",
"picture_product"."id" AS "t1_c0",
"picture_product"."id_1" AS "t1_c1",
"picture_product"."id_2" AS "t1_c2",
"picture_product"."sys_date" AS "t1_c3",
"picture_product"."sys_date_update" AS "t1_c4",
"picture_product"."sys_user" AS "t1_c5",
"picture_product"."exist" AS "t1_c6"
FROM "mc_photo" "t"
LEFT OUTER JOIN "l_products_22_photo" "picture_product"
ON ("picture_product"."id_2"="t"."id")
WHERE (id_1 = 22)
GROUP BY id_2

Ловлю такую ошибку:

DbCommand не удалось исполнить SQL-запрос: SQLSTATE[42803]: Grouping error: 7 ERROR: column "t.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "t"."id" AS "t0_c0", "t"."name" AS "t0_c1", "t"."meta...
^. The SQL statement executed was: SELECT "t"."id" AS "t0_c0", "t"."name" AS "t0_c1", "t"."meta_d" AS "t0_c2", "t"."meta_k" AS "t0_c3", "t"."order_sort" AS "t0_c4", "t"."sys_date" AS "t0_c5", "t"."sys_date_update" AS "t0_c6", "t"."sys_user" AS "t0_c7", "t"."exist" AS "t0_c8", "picture_users"."id" AS "t1_c0", "picture_users"."id_1" AS "t1_c1", "picture_users"."id_2" AS "t1_c2", "picture_users"."sys_date" AS "t1_c3", "picture_users"."sys_date_update" AS "t1_c4", "picture_users"."sys_user" AS "t1_c5", "picture_users"."exist" AS "t1_c6" FROM "mc_photo" "t" LEFT OUTER JOIN "l_users_22_photo" "picture_users" ON ("picture_users"."id_2"="t"."id") WHERE (t.exist = 1) AND (id_1 = 10) GROUP BY id_2 LIMIT 20


Или в postgres:

ERROR: column "t.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "t"."name" AS "t0_c1", "t"."id" AS "t0_c0",


Что я не так делаю?
  • Вопрос задан
  • 179 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы