問題描述
好的,這是我的遷移...
公共函數(shù) up(){Schema::create('instagrams', function (Blueprint $table) {$table->bigInteger('id')->unsigned()->primary();//...});}公共函數(shù) up(){Schema::create('users', function (Blueprint $table) {$table->increments('id');$table->bigInteger('instagram_id')->unsigned()->nullable();//...});}
我有一個(gè)用戶模型和一個(gè) instagram 模型.這是我的 Instagram 模型:
class Instagram 擴(kuò)展模型{公共函數(shù)用戶(){返回 $this->hasOne('AppUser');}}
我的問題是 Instagram 與用戶的關(guān)系不起作用.我無法從 Instagram 訪問用戶,即使他們都在數(shù)據(jù)庫中.
<預(yù)><代碼>>>>$u = AppUser::first()=>應(yīng)用用戶 {#695編號:1,instagram_id: "3620243170",}>>>$i = AppInstagram::first()=>應(yīng)用Instagram {#696id: "3620243170",}>>>$i->用戶=>空值所以,我花了很長時(shí)間絞盡腦汁,直到找到這些有用的修補(bǔ)方法……這就是它給我的:
<預(yù)><代碼>>>>$i->user()->toSql()=>從`users` 中選擇* 其中`users`.`instagram_id` = ? 并且`users`.`instagram_id` 不為空">>>$i->user()->getBindings()=>[2147483647,]除了 ID 被隱藏在 Laravel 中的任何代碼限制在 32 位限制之外,一切都井然有序……ID 需要大于 32 位,因?yàn)?Instagram 的 ID 就是這樣存儲的.我怎樣才能讓這種關(guān)系發(fā)揮作用?
聽起來您使用的是 32 位版本的 PHP,其中最大整數(shù)值為 2147483647.
問題是當(dāng)關(guān)系查詢獲取Instagram
實(shí)例的鍵值來查詢用戶時(shí),它會(huì)自動(dòng)將該id 值轉(zhuǎn)換為$keyType定義的類型代碼> 模型上的屬性.此屬性默認(rèn)為
int
.
因此,即使您的 Instagram
實(shí)例 ID 是 "3620243170"
,它也會(huì)被轉(zhuǎn)換為 int,在 32 位 PHP 中會(huì)將其轉(zhuǎn)換為 2147483647
.
您可以嘗試幾種方法來緩解此問題:
使用 64 位版本的 PHP.64 位 PHP 的最大 int 大小與可用于有符號 bigint 字段的最大 int 匹配.但是,如果您使用的是未簽名的 bigint,一旦您的 ID 超過 9223372036854775807(不太可能),您就會(huì)再次遇到此問題.
將
Instagram
模型上的$keyType
屬性更改為float
,或者可能是string
.這只會(huì)影響 Eloquent 在 PHP 中對變量的轉(zhuǎn)換,不會(huì)影響它們在數(shù)據(jù)庫中的存儲方式.
將protected $keyType = 'float';
添加到您的Instagram
模型中.
Alright, so here's my migrations...
public function up()
{
Schema::create('instagrams', function (Blueprint $table) {
$table->bigInteger('id')->unsigned()->primary();
// ...
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('instagram_id')->unsigned()->nullable();
// ...
});
}
I have a user model, and an instagram model. Here's my instagram model:
class Instagram extends Model
{
public function user()
{
return $this->hasOne('AppUser');
}
}
My problem is the instagram's relationship with the user isn't working. I can't access the user from an instagram, even when they're both in the database.
>>> $u = AppUser::first()
=> AppUser {#695
id: 1,
instagram_id: "3620243170",
}
>>> $i = AppInstagram::first()
=> AppInstagram {#696
id: "3620243170",
}
>>> $i->user
=> null
So, I spent a long time wracking my brain until I found these helpful tinker methods... here's what it's giving me:
>>> $i->user()->toSql()
=> "select * from `users` where `users`.`instagram_id` = ? and `users`.`instagram_id` is not null"
>>> $i->user()->getBindings()
=> [
2147483647,
]
Everything is in order except the ID is being maxed at the 32 bit limit by whatever code is hiding in laravel... the ID needs to be bigger than 32 bits because that's how instagram's IDs are stored. How can I get this relationship to work?
It sounds like you're using a 32-bit version of PHP, where the max integer value is 2147483647.
The issue is that when the relationship query gets the key value of the Instagram
instance to query the users, it automatically casts that id value to the type defined by the $keyType
property on the model. This property is int
by default.
So, even though your Instagram
instance id is "3620243170"
, it is cast to an int, which in 32-bit PHP will turn it into 2147483647
.
There are a couple things you can try to mitigate this issue:
Use a 64-bit version of PHP. The max int size for 64-bit PHP matches the max int available for a signed bigint field. However, if you're using an unsigned bigint, you will run into this issue again once your ids exceed 9223372036854775807 (not likely).
Change the
$keyType
property on yourInstagram
model tofloat
, or possiblystring
. This only affects Eloquent's casting of the variables in PHP, it does not affect how they are stored in the database.
Addprotected $keyType = 'float';
to yourInstagram
model.
這篇關(guān)于Laravel bigInteger 在關(guān)系中四舍五入為 int的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!