easy-swoole / orm

31 stars 27 forks source link

[1.1.1升级到1.1.3] 报错: Undefined variable: targetTableAlias at file:/easyswoole/orm/src/AbstractModel.php line:875 #39

Closed getchu closed 4 years ago

getchu commented 4 years ago

[1.1.1升级到1.1.3] 报错: Undefined variable: targetTableAlias at file:/easyswoole/orm/src/AbstractModel.php line:875

详见图:

联表的字段名相同, 却不懂得用callable $where, 就要大幅度修改抽象类的hasMany方法? 这会导致性能降低和没必要的bug,

抽象Model 的 hasMany 原来设计得挺好的, 建议回滚至 1.1.1 版本, 关于字段名相同的问题, 可以这么写代码:

假设: component表 有主键 componentId, hasMany componentImg 表记录, 外键是 componentId, 既然字段名相同, 可以让开发者自己决定 要输出 哪一张表的哪一个同名字段, 即 使用 callable $where 传参, 而不是修改 hasMany的"便捷传参"方式

`

//模型关联,一对一,一对多
public function componentImg() {

    $componentId = $this->getAttr('componentId');
    return $this->hasMany(\App\Model\ComponentImg::class, function(\EasySwoole\Mysqli\QueryBuilder $builder) use($componentId) {

        $targetTable = 'component_img';//要查询的数据表
        $joinPk = 'componentId';

        //自定义部分
        $builder->where("{$targetTable}.{$joinPk}", $componentId)
            ->where("{$targetTable}.deleteAt", null, 'IS')
            ->orderBy($targetTable.'.section', 'ASC')
            ->orderBy($targetTable.'.sort', 'ASC')
            ->orderBy($targetTable.'.imgId', 'ASC');

        return $builder;
    });
}

`

xuanyanwow commented 4 years ago

自动别名是有必要的,很多刚入门用户都是选择自动关联,不自定义where,性能损耗也不多,表结构是有缓存的,只执行了拼接。如tp-orm等 也都是自动别名的。

getchu commented 4 years ago

做了fix, 兼容自动别名