laracraft-tech/laravel-dynamic-model

v3.0.3 2023-04-14 08:57 UTC

This package is auto-updated.

Last update: 2024-09-08 16:57:48 UTC


README

Latest Version on Packagist Tests Check & fix styling License

通常情况下,Laravel 中的每个模型都只为一个表编写,打破这个惯例并不容易。这样做有很好的理由——它确保了模型设计得很好且干净。但在非常具体的情况下,您可能需要通过单个模型处理多个表。这时,Laravel Dynamic Model 就派上用场了!它提供了一个优雅的模型,最终可以处理多个表,如果您愿意,还可以处理多个数据库连接!

安装

依赖项

此包依赖于 Doctrine/DBAL,请确保您已安装。

composer require doctrine/dbal

composer require laracraft-tech/laravel-dynamic-model

使用

让我们创建一些虚拟表

php artisan make:migration create_foo_table
php artisan make:migration create_bar_table

为表创建迁移

Schema::create('foo', function (Blueprint $table) {
    $table->id();
    $table->string('col1');
    $table->integer('col2');
    $table->timestamps();
});

Schema::create('bar', function (Blueprint $table) {
    $table->date('period')->primary();
    $table->string('col1');
    $table->integer('col2');
    $table->timestamps();
});
php artisan migrate

让我们创建一个动态模型

如果您想创建一个 动态模型,则必须使用 DynamicModelFactory。工厂确保为您的创建模型设置 table 和可选的 connection。它还检查您提供的表的架构,以设置适当的值:primaryKeykeyTypeincrementing。这意味着,即使您定义的表架构有一个名为实例的 period 的主键,并且具有 date 类型,工厂也会为您处理。

请注意,默认的 DynamicModel 设置为 unguarded。如果您不喜欢这种方式或希望您的动态模型有一些自定义功能,请查看下面的部分并创建自己的动态模型。

use LaracraftTech\LaravelDynamicModel\DynamicModel;
use LaracraftTech\LaravelDynamicModel\DynamicModelFactory;

$foo = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo');

$foo->create([
    'col1' => 'asdf',
    'col2' => 123
]);

$faz = app(DynamicModelFactory::class)->create(DynamicModel::class, 'faz');

$faz->create([
    'period' => '2023-01-01',
    'col1' => 'asdf',
    'col2' => 123
]);

// optionally use another db connection (this one must be defined in your config/database.php file)
$fooOtherDB = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo', 'mysql2');
$fooOtherDB->create([...]);

dump($foo->first());
dump($faz->first());
dump($fooOtherDB->first());

这将为您带来

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_foo {#328 ▼
  #connection: "mysql"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_faz {#328 ▼
  #connection: "mysql"
  #table: "faz"
  #primaryKey: "period"
  #keyType: "string"
  +incrementing: false
  #attributes: array:5 [▼
    "period" => "2023-01-01"
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql2_foo {#328 ▼
  #connection: "mysql2"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

使用您自己的动态模型

如果您需要向动态模型添加 自定义 方法或可能 保护 它,您可以创建自己的 Eloquent 模型,然后通过 Factory 调用它。这将创建一个新的模型实例,它通过您的原始模型进行扩展。确保您的模型实现 DynamicModelInterface 或扩展 DynamicModel 类。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use LaracraftTech\LaravelDynamicModel\DynamicModel;
use LaracraftTech\LaravelDynamicModel\DynamicModelInterface;

// class MyDynamicModel extends Model implements DynamicModelInterface (this would also work)
class MyDynamicModel extends DynamicModel
{
    proteced $guarded = ['id'];

    public function doSomething()
    {
        // do something
    }
}

$foo = app(DynamicModelFactory::class)->create(MyDynamicModel::class, 'foo');

$foo->create([
    'col1' => 'asdf',
    'col2' => 123
]);

$foo->doSomething();

dd($foo->first());

这将为您带来

^ App\Model\MyDynamicModel_mysql_foo {#328 ▼
  #connection: "mysql"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
  ...
}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

有关如何报告安全漏洞的信息,请参阅 我们的安全策略

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件