E
E
EVOSandru62015-08-11 07:30:38
Yii
EVOSandru6, 2015-08-11 07:30:38

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

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

The model is definitely working, external _form.php works without $this->renderPartial('render_imageGrid');
The following query is formed:
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
I'm getting this error:
DbCommand failed to execute SQL query: 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."


LINE 1: SELECT "t"."name" AS "t0_c1", "t"."id" AS "t0_c0",

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question