@EVOSandru6

Как Yii1 правильно выставить scope для модели?

Добрый день,

Есть такой scope (отношения MANY_MANY) в таблице Users:

public function scopes()  {
        return array_merge (
            parent::scopes(), [
                'holder'    => [
                    'with'  => [
                        'tenant' => [
                            'condition'         => 'id=:tenant_id',
                            'params'            => [
                                ':tenant_id'    =>  Tenants::getTenantID()
                            ]
                        ]
                    ]
                ],
            ]
        );
    }

public function relations() {
		return [
                     'tenant'        =>      [self::MANY_MANY,   'Tenants',  'l_users_22_tenants(id_1, id_2)'],
]
}


Есть 3 таблицы: m_users (id, name), m_tenants (id, name), l_users_22_tenants (id, id_1, id_2)

Но при попытке вывести пользователей, у которых есть связь с таблицей арендаторов:

$customers   = Users::model()->holder()->findAll();


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

m.db.CDbCommand] Не удалось выполнить CDbCommand::fetchAll(): SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "id" is ambiguous
LINE 1: ...") WHERE ((t.exist = 1) AND ("t"."role_id"=$1)) AND (id=$2)
^. Выполнявшийся SQL-запрос: SELECT "t"."id" AS "t0_c0", "t"."role_id" AS "t0_c1", "t"."name" AS "t0_c2", "t"."name_kz" AS "t0_c3", "t"."name_en" AS "t0_c4", "t"."name_alias" AS "t0_c5", "t"."photo" AS "t0_c6", "t"."order_sort" AS "t0_c7", "t"."sys_date" AS "t0_c8", "t"."sys_date_update" AS "t0_c9", "t"."sys_user" AS "t0_c10", "t"."exist" AS "t0_c11", "t"."email" AS "t0_c12", "t"."password" AS "t0_c13", "t"."login" AS "t0_c14", "t"."status_id" AS "t0_c15", "t"."telephone" AS "t0_c16", "t"."photo_id" AS "t0_c17", "t"."place_id" AS "t0_c18", "t"."sex_id" AS "t0_c19", "t"."company" AS "t0_c20", "t"."site" AS "t0_c21", "t"."confirm" AS "t0_c22", "t"."hash" AS "t0_c23", "t"."fax" AS "t0_c24", "tenant"."id" AS "t1_c0", "tenant"."name" AS "t1_c1", "tenant"."name_kz" AS "t1_c2", "tenant"."name_en" AS "t1_c3", "tenant"."name_alias" AS "t1_c4", "tenant"."description" AS "t1_c5", "tenant"."description_kz" AS "t1_c6", "tenant"."description_en" AS "t1_c7", "tenant"."order_sort" AS "t1_c8", "tenant"."sys_date" AS "t1_c9", "tenant"."sys_date_update" AS "t1_c10", "tenant"."sys_user" AS "t1_c11", "tenant"."exist" AS "t1_c12", "tenant"."director" AS "t1_c13", "tenant"."manager" AS "t1_c14", "tenant"."photo" AS "t1_c15" FROM "m_users" "t" LEFT OUTER JOIN "l_users_22_tenants" "tenant_tenant" ON ("t"."id"="tenant_tenant"."id_1") LEFT OUTER JOIN "m_tenants" "tenant" ON ("tenant"."id"="tenant_tenant"."id_2") WHERE ((t.exist = 1) AND ("t"."role_id"=:yp0)) AND (id=:tenant_id).


В данном случае для scope alias (t.) Не понятно куда ставить(

Подсобите советом.
  • Вопрос задан
  • 260 просмотров
Решения вопроса 1
EnChikiben
@EnChikiben
mysql ругается потому что не знает какой id использовать
'condition' => 'tenant.id=:tenant_id',
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

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