Laravel 5渴望从外键加载下降的前导零 [英] Laravel 5 eager loading dropping leading zeros from foreign key

查看:79
本文介绍了Laravel 5渴望从外键加载下降的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信当外键是包含零填充数字的字符串时,我会遇到Laravel 5.3如何处理急切加载的错误.

I believe that I've encountered a bug in how Laravel 5.3 handles eager loading when the foreign key is a string which contains zerofilled numbers.

我有两个模型(使用mysql后端),SchoolStudent. School中的id字段是一个字符串,并且包含由状态分配的5位数字,其中包括前导零. Student BelongsTo School,并包含一个school_id字段,该字段的定义与School中的id字段相同.我已经进行了一些测试,发现当我调用Student::with('school')时,只要Student中的school_id没有前导零,我就得到了预期的School模型,但是它不返回School带有前导零的school_id值的模型.

I have two models (using a mysql backend), School and Student. The id field in School is a string, and contains 5-digit numbers assigned by the state, which include leading zeros. Student BelongsTo School, and contains a school_id field which is defined identically to the id field in School. I have done some testing, and I'm finding that when I call Student::with('school') I get the expected School models as long as the school_id in Student is free of leading zeroes, but it returns no School models for school_id values with leading zeroes.

我已经使用新鲜创建的School记录进行了直接测试,并且值在两个数据库表中都正确地存储了前导零,并且当我尝试直接查询表时,前导零可以正常工作,但是with()进入方程式,事情中断了.我试图通过其他方式重现故障,甚至手动构建whereIn()调用以镜像由with()构造的查询的语法,但其他所有方法都能正确返回预期的记录.

I've done direct testing with freshly minted School records, and the values are being stored correctly with leading zeroes in both database tables, and when I try directly querying the tables the leading zeroes work fine, but as soon as with() enters the equation, things break. I've attempted to reproduce the failure through other means, even manually constructing whereIn() calls to mirror syntax of the queries constructed by with(), but everything else correctly returns the expected records.

在将Laravel升级阶梯从4.1升级到5.3之前,此代码可以正常工作,所以我想知道可能发生了什么变化.我已经深入研究 BelongsTo的GitHub存储库,并且任何参数处理似乎都没有去除前导零,所以我对with()为何被破解感到迷茫这样.

This code worked fine prior to climbing the Laravel upgrade ladder from 4.1 to 5.3, so I'm wondering what may have changed. I've gone as far as digging into the GitHub repository for BelongsTo, and none of the parameter handling seems to strip leading zeroes, so I'm really at a loss as to why with() is breaking in this way.

那么,任何人都可以分享任何见解吗?我很困惑,宁愿不必围绕with()进行设计.我还要预先声明,我不能从id字段中删除前导零,这不是一个选项,必须将它们存储起来,而不仅仅是显示为ZEROFILL.

So, does anybody have any insights they can share? I'm stumped, and would rather not have to engineer around with(). I'll also state up front that I can't drop the leading zeroes from the id field, it's not an option, they must actually be stored, and not just displayed as with ZEROFILL.

更新:我已经附上了一个示例,该示例确定与with()语句分开使用时,存储在Student中的school_id可以成功连接到相应的School:

UPDATE: I've attached an example that establishes that the school_id stored in Student can successfully connect to the corresponding School when used separately from the with() statement:

    $interventions = Intervention::with('school')->where('id','=',780)->get();
    $schools = School::whereIn('id',$interventions->pluck('school_id')->all())->get();
    throw new \Exception(print_r($interventions,true).'|'.print_r($schools,true));

以下是\ Exception的(为简洁起见)结果:

Here are the (edited for brevity) results of the \Exception:

Exception in AdminController.php line 273:
Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
    (
        [0] => App\Models\Student Object
        (
            [attributes:protected] => Array
            (
                [id] => 780
                [school_id] => 01234
            )
            [relations:protected] => Array
            (
                [school] => 
            )
        )
    )
)


Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
    (
        [0] => App\Models\School Object
        (
            [attributes:protected] => Array
            (
                [id] => 01234
                [school] => Test1
                [district_id] => 81000
                [inactive] => 0
                [see] => 0
            )
        )
    )
)

因此,尽管Student::with('school')无法拉起相应的School,但成功将相同的Student->school_id值馈送到School::whereIn().我仍然感到迷茫.

So, while Student::with('school') fails to pull up the corresponding School, feeding the same Student->school_id values to School::whereIn() succeeds. I remain mystified.

推荐答案

您没有显示模型类,但是我猜测您需要在School雄辩模型中使用public $incrementing = false;.否则,当匹配关系时它将被强制为int,丢失所有前导零.

You are not showing the model classes, but my guess is you need public $incrementing = false; in the School Eloquent model. Otherwise it will be coerced to int when matching the relationship, losing all leading zeroes.

这篇关于Laravel 5渴望从外键加载下降的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆