在Laravel中存储32位二进制文​​件的正确方法 [英] Proper way to store 32-bit binary in Laravel

查看:77
本文介绍了在Laravel中存储32位二进制文​​件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我正在使用

 /***运行迁移.** @返回无效*/公共功能up(){Schema :: create('test_binary',function(Blueprint $ table){$ table-> increments('id');$ table-> char('binary_number',32)-> charset('binary');//来自:https://stackoverflow.com/a/62615777/5675325$ table-> timestamps();});} 

存储32位二进制数字,例如 00000000000000000000000000000010 ,这些数字

播种机有类似的东西

 'binary_number'=>str_pad(base_convert(2,10,2),32,'0',STR_PAD_LEFT), 

关于在数据库中具有 BINARY 类型(如上图所示), apokryfos 建议

我认为在这里是您所需要的,尽管我不确定Laravel支持的功能,因此您可能需要使用 DB :: raw("b'000000010'")将数据插入位列

Jarek Tkaczyk 同意(我不太确定Laravel是否支持)部分

具有位类型字段意味着您每次插入/更新该字段时都需要使用原始值作为解决方法.(...)

DB :: table('table')->插入(['bit_field'=> DB :: raw(0)]);//插入0

他建议如果可能的话,将OP更改为tinyint(对于列的值可以为0或1的情况).

这可能暗示,如果要通过Laravel迁移处理32位的数据,则可能要使用比tinyint大的整数.

因此,如果我想使用32号整数,请 Alexey Mezenin 指出

您不能执行此操作,但是可以使用不同类型的整数:

  $ table-> bigInteger()$ table-> mediumInteger()$ table-> integer()$ table-> smallInteger()$ table-> tinyInteger() 

应该怎么办

  $ table-> char('binary_number',32)-> charset('binary'); 

然后将32位二进制数容纳为值,以及如何插入/检索此类记录?

解决方案

您数据库中的输入应为无符号整数类型,其长度为32位或4个字节.

  $ table-> unsignedInteger(); 

因此,保存值不成问题,您只需使用二进制代码即可使用 bindec()转换为小数,或者使用常量方法进行简单的SUM转换.

对于查询部分,假设您希望结果的第3位为1.

  $ query-> whereRaw('BIT_COUNT(4& fieldName)= 1') 

我经常在相关模型上为这些值分配常量,以帮助维护代码&调试.

  class用户扩展模型{const NOTIFICATION_FRIEND = 1;//2 ^ 0或0000 ... 0000 0001const NOTIFICATION_CLIENT = 2;//2 ^ 1或0000 ... 0000 0010const NOTIFICATION_APP = 4;//2 ^ 2或0000 ... 0000 0100const NOTIFICATION_VENDOR = 8;//2 ^ 3或0000 ... 0000 1000//...//最多32个1点是2147483648(或2 ^ 31)} 

所以查询看起来像这样

 <代码> $ usersToBeNotified = $ currentUser-> friends()-> whereRaw('BIT_COUNT('.User :: NOTIFICATION_FRIEND.'& notification_preferences)= 1')-> get(); 

要从表格中存储要存储的整数,只需对常量求和即可.

  $ user-> notification_preferences = User :: NOTIFICATION_FRIEND + User :: NOTIFICATION_APP; 

At the moment I'm using

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('test_binary', function (Blueprint $table) {
        $table->increments('id');
        $table->char('binary_number', 32)->charset('binary'); // From: https://stackoverflow.com/a/62615777/5675325
        $table->timestamps();

    });
}

to store 32-bit binary numbers like 00000000000000000000000000000010 which are result of

$binaryString = str_pad(base_convert(2, 10, 2),32,'0',STR_PAD_LEFT);

and are stored in such table

The seeder has something like

'binary_number' => str_pad(base_convert(2, 10, 2),32,'0',STR_PAD_LEFT),

In relation to have a BINARY type in the DB (as the previous image show), apokryfos suggested

I think bit is what you need here, though I'm not fully sure that Laravel supports that so you might need to use DB::raw("b'000000010'") to insert data to bit columns

Jarek Tkaczyk agrees with the (I'm not fully sure that Laravel supports that) part

Having bit type field means that you need to use raw values as a workaround whenever you are inserting/updating that field. (...)

DB::table('table')->insert(['bit_field' => DB::raw(0)]); // inserts 0

and he suggests for the OP to change to tinyint if he could (for a case of a column that can have as value 0 or 1).

This might hint that if one wants to deal with bit of size 32 through Laravel migrations, one would want to use an integer of higher size than tinyint.

So, if I wanted to have an int with size 32, Alexey Mezenin points out

You can't do this, but you can use different types of integer:

$table->bigInteger()
$table->mediumInteger()
$table->integer()
$table->smallInteger()
$table->tinyInteger()

What should happen to

$table->char('binary_number', 32)->charset('binary');

then to accomodate the 32-bit binary numbers as values and how to insert/retrieve such records?

解决方案

The input in your database should be of type unsigned integer wich is 32 bit size or 4 bytes.

$table->unsignedInteger();

So, saving the value should not be an issue, you just have a binary value to switch to a decimal one using bindec() or a simple SUM using the constant method.

For the query part, let say you want the results wich has the 3rd bit as 1.

$query->whereRaw('BIT_COUNT(4 & fieldName) = 1')

i'm used to assign constants for those values on the model concerned wich help maintaining the code & debug.

class User extends Model {
const NOTIFICATION_FRIEND = 1; //2^0 or 0000 ... 0000 0001
const NOTIFICATION_CLIENT = 2; //2^1 or 0000 ... 0000 0010
const NOTIFICATION_APP = 4; //2^2 or 0000 ... 0000 0100
const NOTIFICATION_VENDOR = 8; //2^3 or 0000 ... 0000 1000
//...
// up to a max of 32nd one wich is 2147483648 (or 2^31)
}

So the query looks like this

$usersToBeNotified = $currentUser->friends()
    ->whereRaw('BIT_COUNT('.User::NOTIFICATION_FRIEND .' & notification_preferences) = 1')
    ->get();

To build the integer to be stored in from a form, just sum the constants.

$user->notification_preferences  = User::NOTIFICATION_FRIEND + User::NOTIFICATION_APP;

这篇关于在Laravel中存储32位二进制文​​件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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