yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Bug in ActiveRelationTrait - prefixKeyColumns #7210

Closed dynasource closed 8 years ago

dynasource commented 9 years ago

I am getting an error in a gridview. The error arises when adding a JOIN to the query (having a link. )

The error is generated because of a wrong table alias used in the where condition (done automatically by Yii). It uses the full 'from' statement in the Query in which a space of the alias is used like in http://www.yiiframework.com/doc-2.0/yii-db-query.html#from()-detail

The error:

Column not found: 1054 Unknown column 'identity_x_usergroup Members2.right_id' in 'where clause'

SELECT COUNT(*) FROM `identity_x_usergroup` `Members2` LEFT JOIN `identity` ON `left_id`='id' WHERE (identity.status>0) AND (`identity_x_usergroup Members2`.`right_id`=2)
$query = $model->getMembers2();
$query->join('LEFT JOIN','identity','left_id=id');
$query->where('identity.status>0');

[sql] => 
    [on] => 
    [joinWith] => 
    [select] => Array
        (
            [0] => COUNT(*)
        )

    [selectOption] => 
    [distinct] => 
    [from] => Array
        (
            [0] => identity_x_usergroup Members2
        )

    [groupBy] => 
    [join] => Array
        (
            [0] => Array
                (
                    [0] => LEFT JOIN
                    [1] => identity
                    [2] => Array
                        (
                            [left_id] => id
                        )

                )

        )

    [having] => 
    [union] => 
    [params] => Array
        (
        )

    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => 
    [where] => Array
        (
            [0] => and
            [1] => identity.status>0
            [2] => Array
                (
                    [0] => in
                    [1] => Array
                        (
                            [0] => identity_x_usergroup Members2.right_id
                        )

                    [2] => Array
                        (
                            [0] => 2
                        )

                )

        )

    [limit] => 
    [offset] => 
    [orderBy] => Array
        (
        )

    [indexBy] => 
    [modelClass] => dmn\yii2\modules\usergroup\models\UsergroupIdentity
    [with] => 
    [asArray] => 
    [multiple] => 1
    [primaryModel] => dmn\yii2\modules\usergroup\models\Usergroup Object{ RECURSION}
  [link] => Array
        (
            [right_id] => id
        )

    [via] => 
    [inverseOf] => 

The error is created in the following line: https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveRelationTrait.php#L437

The table alias does not seem to be processed right.

thanks for looking at it.

cebe commented 9 years ago

Confirmed, thanks for reporting. As far as I see from a quick look, the fix should be done in Query::from() to turn string alias into array syntax.

coderlight commented 9 years ago

$query = $model->getMembers2(); $query->join('LEFT JOIN','identity','identity_x_usergroup.left_id=idenitty.id'); $query->where('identity.status>0');

cebe commented 8 years ago

@dynasource why did you close it?

dynasource commented 8 years ago

because its 2 years ago and I am not able to reproduce it nor support it with the current state of my software.