E
E
EVOSandru62016-03-04 13:32:55
Yii
EVOSandru6, 2016-03-04 13:32:55

How Yii1 correctly set the scope for the model?

Good afternoon,
There is such a scope (relationship MANY_MANY ) in the Users table:

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

There are 3 tables: m_users (id, name), m_tenants (id, name), l_users_22_tenants (id, id_1, id_2)
But when trying to display users who have a relationship with the tenants table:
$customers   = Users::model()->holder()->findAll();

Error:
m.db.CDbCommand] CDbCommand::fetchAll() failed: 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 query executed: 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"."

In this case, for scope alias ( t. ) It is not clear where to put (
Help with advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2016-03-04
@EVOSandru6

mysql swears because it doesn't know which id to use
'condition' => 'tenant.id=:tenant_id',

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question