think-orm icon indicating copy to clipboard operation
think-orm copied to clipboard

hasOneThrough不支持bind,以及直接bindAttr的name字段居然是显示模型的表名$name字段的值

Open moobing opened this issue 3 years ago • 1 comments

1、hasOneThrough使用bind后,报错 代码片段1:


    public function hasOneThroughGoodsDetail()
    {
        return $this->hasOneThrough(KypGoodsDetail::class,KypGoods::class,'id','id','goods_id','goods_warehouse_id')
        		->bind([
	                'goods_detail_id' =>'goods_warehouse_id',
	                'goods_name'=>'name',
	                'goods_cover_pic'=>'cover_pic',
            	]);
    }
image

2、直接在控制器使用bindAttr,name字段没有调用正确的数据,而是显示对应模型的表名$name字段的值 代码片段2:


        $goodsList = BlindboxGoodsModel::with(['hasOneGoods','hasOneThroughGoodsDetail'])
        			->where('blindbox_id',$params['id'])
        			->find()
        			->bindAttr('hasOneThroughGoodsDetail',['goods_name' =>'name']);
image 代码片段3:GoodsDetail模型代码
namespace app\model;
use think\Model;

class KypGoodsDetail extends Model
{
    protected $name = 'goods_warehouse';//指定表名
}

moobing avatar Apr 26 '22 10:04 moobing

刚刚研究出来一个临时解决办法,可以达到预期效果。代码如下,给大家避坑 希望官方及时修复bindAttr的模型$name变量污染问题,并尽快支持hasOneThrough的bind用法

        $goodsList = BlindboxGoodsModel::with(['hasOneGoods','hasOneThroughGoodsDetail'])
        			->where('blindbox_id',$params['id'])
        			->select()
        			->bindAttr('hasOneThroughGoodsDetail',['goods_name']);

moobing avatar Apr 26 '22 10:04 moobing