saightsystems/eloquent-transformable

按您希望的样子与Laravel Eloquent模型一起工作,而不是它们实际的样子。

0.0.5 2019-03-13 20:36 UTC

This package is auto-updated.

Last update: 2024-09-15 07:20:44 UTC


README

Latest Version on Packagist Total Downloads Build Status

使用简单的转换层,按您希望的样子(而不是实际的样子)与Laravel Eloquent模型一起工作。

安装

您可以通过composer安装该包到任何Laravel 5.8.*

composer require saintsystems/eloquent-transformable

使用场景

Laravel Eloquent基于约定。这些约定做出了某些假设,例如主键应命名为id或外键列应如何命名。您可以覆盖这些约定,但这需要大量的配置。此外,像Laravel Nova这样的工具假设默认的Eloquent约定。将Nova和Eloquent配置为使用不同的命名约定会变得很痛苦,并且代码会变得脆弱,因为它明确地依赖于非传统的数据库命名标准。

数据库并不总是在我们控制之下。它们可能由DBA管理,可能是第三方系统,或者可能只是一个不遵循Laravel约定的遗留数据库,我们不想改变或无法改变以使其符合Laravel的约定。这可能以非传统的表名、列前缀、列命名约定、外键名称等形式出现。我们并不总是控制Laravel应用程序可能运行的数据库。

Eloquent Transformable允许您通过简单的转换定义数据库列的外观,然后像它们遵循Eloquent命名约定一样使用您的Eloquent模型。

Transformable还可以用作简单的转换层,以保护应用程序不受底层数据库结构的影响。

用法

  1. 在您的项目中创建一个基本的Model.php类,并将其添加到Transformable特质中。
    namespace App;

    use Illuminate\Database\Eloquent\Model as EloquentModel;
    use SaintSystems\Eloquent\Transformable\Transformable;

    class Model extends EloquentModel
    {
        use Transformable;
    }
  1. 创建一个代表您的“实际”数据库模型的模型。

假设表定义为

表名: tbl_Database_Table

    namespace App;

    class ActualDatabaseModel extends Model
    {
        protected $table = 'tbl_Database_Table';

        protected $primaryKey = 'PK_Database_ID';

        protected $guarded = [];
    }
  1. 创建一个代表您的“期望”数据库模型的模型。
namespace App;

class DesiredDatabaseModel extends ActualDatabaseModel
{
    // Desired $primaryKey name (PK_Database_ID is the actual PK in the database)
    protected $primaryKey = 'id';

    /**
     * Transformation Mapping of DB Column Names to desired Eloquent Model Attribute Names
     * This variable comes from the SaintSystems\Eloquent\Transformable\Transformable Trait
     * used in the base Model.php
     * @var array
     */
    protected $transform = [
        'id' => 'PK_Database_ID',
        'name' => 'DB_Name',
        'foreign_key_id' => 'FK_DB_Foreign_Key_ID'
    ]; // TransformationMap;
}
  1. 按您希望的任何方式使用您的新“转换”模型
    $model = new DesiredDatabaseModel([
        'id' => 1,
        'name' => 'Name',
        'foreign_key_id' => 2
    ]);

    dd($desiredModel->toArray());
    /*
    Will output the following:
    [
        'id' => 1,
        'name' => 'Name',
        'foreign_key_id' => 2
    ]
    */

    // Now, save the model
    $model->save();
    // Despite using transformed attributes above, the record will still save using the transformed attributes we defined.

    // We can even query the model with our desired/transformed column names
    $model = new DesiredDatabaseModel::where('name','Joe')->orWhere('name','Judy')->get();

    /*
        The call above will result in the following query being run:
        select *
        from "tbl_Database_Table"
        where "DB_Name" = 'Joe' or "DB_Name" = 'Judy'

        But will come back in the following structure:
        [
            [
                'id' => 1,
                'name' => 'Joe',
                'foreign_key_id' => 1
            ],
            [
                'id' => 2,
                'name' => 'Judy',
                'foreign_key_id' => 1
            ]
        ]
    */

好处

使用Eloquent Transformable,我们可以围绕Laravel Eloquent的约定构建我们的应用程序,并像底层数据库已按照Laravel约定构建一样使用我们的模型。如果我们有时间并且能够最终将数据库结构移动到Laravel的约定,我们可以简单地从我们的模型中删除转换。这使我们免受底层数据库更改的影响,并允许我们控制底层数据库在应用程序或API中的外观。

致谢

许可证

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