[英]Special UUID primary key for laravel eloquent
I have a users table that doesn't use an auto increment primary key, instead it uses a binary uuid primary key. 我有一个用户表,它不使用自动增量主键,而是使用二进制uuid主键。 I set up my custom model to interact with my table however, I'm having trouble trying to find records by using
::find()
because for a case like this, this uuid needs to searched by using HEX()
and UNHEX()
mysql functions. 我设置了自定义模型来与表交互,但是,在尝试使用
::find()
查找记录时遇到了麻烦,因为在这种情况下,该uuid需要使用HEX()
和UNHEX()
mysql功能。 How to I override this and have whatever is in ::find()
to be hexed. 我该如何重写它,以及
::find()
是否要十六进制。 The name of the model is Player. 该模型的名称是Player。
So if I was to try to find a user with a uuid of 9d823b9eec224cbea10b69bec2c5adef
, I cannot find them by doing: 因此,如果要尝试查找uuid为
9d823b9eec224cbea10b69bec2c5adef
的用户,则无法通过以下方式找到他们:
Player::find('9d823b9eec224cbea10b69bec2c5adef')
since the uuid needs to be unhexed. Player::find('9d823b9eec224cbea10b69bec2c5adef')
因为uuid需要解开。
I've tried Player::find("UNHEX('9d823b9eec224cbea10b69bec2c5adef')");
我已经尝试过
Player::find("UNHEX('9d823b9eec224cbea10b69bec2c5adef')");
with no results. 没有结果。
Here's my model so far: 到目前为止,这是我的模型:
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
}
The datatype for uuid is binary(16) uuid的数据类型为二进制(16)
Update 更新
I've got it to work by using Player::find(DB::raw("UNHEX('9d823b9eec224cbea10b69bec2c5adef')"));
我已经通过使用
Player::find(DB::raw("UNHEX('9d823b9eec224cbea10b69bec2c5adef')"));
使它工作了Player::find(DB::raw("UNHEX('9d823b9eec224cbea10b69bec2c5adef')"));
however this is a lot to write every time I need to look up a user. 但是,每次我需要查找用户时,都要写很多东西。
Is there any way I can have the parameter for ::find()
always run through DB::raw("UNHEX('uuid')")
or be passed through the function hex2bin()
? 有什么办法可以让
::find()
的参数始终通过DB::raw("UNHEX('uuid')")
或通过函数hex2bin()
传递?
I am 100% certain I will always be using UUID so I want to override ::find()
, not create a new method. 我100%肯定我将一直使用UUID,所以我想重写
::find()
,而不是创建一个新方法。
I would try to unhex it in PHP prior to passing it to mysql: 在将其传递给mysql之前,我会尝试在PHP中将其解开:
Player::find(hex2bin('9d823b9eec224cbea10b69bec2c5adef'));
You could add this method to your Player
class: 您可以将此方法添加到
Player
类中:
public static function findUUID($uuid) {
return self::find(hex2bin($uuid));
}
Now any where in your project you can call it like: 现在,在项目中的任何位置都可以这样称呼:
$result = Player::findUUID('9d823b9eec224cbea10b69bec2c5adef');
If you do not want to declare it statically you can: 如果您不想静态声明它,则可以:
public function findUUID($uuid) {
return self::find(hex2bin($uuid));
}
and then reference it in your code with: 然后使用以下代码在您的代码中引用它:
$result = new Player;
$result->findUUID('9d823b9eec224cbea10b69bec2c5adef');
This should allow you to override the native find() behavior and not have to use findUUID() instead: 这应该允许您重写本机的find()行为,而不必使用findUUID()代替:
protected $primaryKey = 'uuid';
public static function find($uuid)
{
return parent::find(hex2bin($uuid));
}
If you really want it like that, you can also do Player::find('9d823b9eec224cbea10b69bec2c5adef')
with this 如果您真的想要这样,也可以使用以下代码执行
Player::find('9d823b9eec224cbea10b69bec2c5adef')
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
public static function find($uuid, $columns = array('*')) {
return self::find("UNHEX('$uuid')", $columns);
}
}
EDITED EDITED
added self, as user Elliot Fehr suggested 根据用户Elliot Fehr的建议添加了自我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.