little-apps/serializable-model

用于Laravel模型中序列化列的简单包。

v1.0.1 2020-04-20 05:54 UTC

This package is auto-updated.

Last update: 2024-09-20 16:26:48 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

SerializableModel是一个用于Laravel模型中序列化列的简单包。它使用serializeunserialize PHP函数将值存储在数据库中。

许可证

SerializableModel是免费且开源的,并使用MIT许可证授权。

Copyright 2018 Little Apps

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

安装

使用composer安装

cd /path/to/laravel/app
composer require little-apps/serializable-model

用法

此包包含一个特质,可以被包含在任何继承自Illuminate\Database\Eloquent\Model的类中。

use LittleApps\SerializableModel\Serializable;
use Illuminate\Database\Eloquent\Model;

class Foo extends Model {
   use Serializable;
}

下一步是定义应该在$serializable属性中序列化的列。

use LittleApps\SerializableModel\Serializable;
use Illuminate\Database\Eloquent\Model;

class Foo extends Model {
   use Serializable;
   
   protected $serializable = [
       'column1',
       'column2',
       'column3',
       'column4',
   ];
}

分配给addressesphone_numbers列的任何值都将作为原始数据类型的字符串表示存储在数据库中。

$foo = new Foo();

// Will be stored in the database as "i:9999;"
$foo->column1 = 9999;

// Will be stored in the database as "a:1:{s:3:"key";s:5:"value";}"
$foo->column2 = ['key' => 'value'];

// Will be stored in the database as "d:96.67;"
$foo->column3 = 96.67;

// Will be stored in the database as "Hello World"
$foo->column4 = 'Hello World';

列的值将被反序列化(如果需要)并返回。以下是根据上面示例设置的值。

// $value will be set to 9999
$value = $foo->column1;

// $value will be set to ['key' => 'value']
$value = $foo->column2;

// $value will be set to 96.67
$value = $foo->column3;

// $value will be set to "Hello World"
$value = $foo->column4;

数据库迁移

在MySQL数据库中用于序列化列的数据类型可能不同。PHP文档对于serializable以及StackOverflow上的此答案建议使用BLOB而不是CHARTEXT。在Laravel数据库迁移中使用BLOB的命令是binary()

Schema::table('foo', function (Blueprint $table) {
    $table->binary('column1');
});

注意

  • 为了节省数据库空间,字符串保持原样,不进行序列化。
  • 资源不能被序列化,尝试这样做将导致未定义的行为。

显示您的支持

Little Apps依赖于像您这样的人来保持我们的软件运行。如果您想对Little System Cleaner表示支持,可以通过PayPal、Payza或信用卡(通过Stripe)进行捐款。请注意,任何金额都很有帮助(甚至只有1美元)。