定制 statamic/eloquent-driver 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

statamic/eloquent-driver

Composer 安装命令:

composer require statamic/eloquent-driver

包简介

Allows you to store Statamic data in a database.

README 文档

README

Eloquent Driver

Provides support for storing your Statamic data in a database, rather than flat files.

Installation & Usage

You can install and configure the Eloquent Driver using a single command:

php please install:eloquent-driver

The command will install the statamic/eloquent-driver package, publish the config file, then prompt you to select which repositories you wish to move to the database. The command will then publish the relevant migrations and run php artisan migrate behind the scenes.

The command will also give you the opportunity to indicate whether you'd like existing data to be imported.

Importing flat-file content

If you originally opt-out of importing existing content, then later change your mind, you can import existing content by running the relevant commands:

  • Addon Settings: php please eloquent:import-addon-settings
  • Assets: php please eloquent:import-assets
  • Blueprints and Fieldsets: php please eloquent:import-blueprints
  • Collections: php please eloquent:import-collections
  • Entries: php please eloquent:import-entries
  • Forms: php please eloquent:import-forms
  • Globals: php please eloquent:import-globals
  • Navs: php please eloquent:import-navs
  • Revisions: php please eloquent:import-revisions
  • Taxonomies: php please eloquent:import-taxonomies
  • Sites: php please eloquent:import-sites

Assets

Syncing

If your assets are being driven by the Eloquent Driver and you're managing your assets outside of Statamic (eg. directly in the filesystem), you should run the php please eloquent:sync-assets command to add any missing files to the database, and remove files that no longer exist on the filesystem.

Exporting to flat files

If you wish to move back to flat-files, you may use the following commands to export your content out of the database:

  • Addon Settings: php please eloquent:export-addon-settings
  • Assets: php please eloquent:export-assets
  • Blueprints and Fieldsets: php please eloquent:export-blueprints
  • Collections: php please eloquent:export-collections
  • Entries: php please eloquent:export-entries
  • Forms: php please eloquent:export-forms
  • Globals: php please eloquent:export-globals
  • Navs: php please eloquent:export-navs
  • Revisions: php please eloquent:export-revisions
  • Taxonomies: php please eloquent:export-taxonomies
  • Sites: php please eloquent:export-sites

Configuration

The configuration file, found in config/statamic/eloquent-driver.php is automatically published when you install the Eloquent Driver.

For each of the repositories, it allows you to determine if they should be driven by flat-files (file) or Eloquent (eloquent). Some repositories also have additional options, like the ability to override the model used.

Using dedicated columns for data

Note: This feature is currently only available for Entries.

By default, the Eloquent Driver stores all data in a single data column. However, it is possible to store fields in their own columns.

  1. First, you'll need to enable the map_data_to_columns option in the entries section of the configuration file:

    // config/statamic/eloquent-driver.php
    
    'entries' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Entries\EntryModel::class,
        'entry' => \Statamic\Eloquent\Entries\Entry::class,
        'map_data_to_columns' => false,
    ],
  2. Create a new migration to add the columns to the entries table:

    php artisan make:migration add_columns_to_entries_table
    public function up()
    {
        Schema::create('entries', function (Blueprint $table) {
            $table->string('description')->nullable();
            $table->json('featured_images')->nullable();
        });
    }

    You should ensure that the column names match the field handles in your blueprints. You should also ensure the column type matches that of the fieldtype. As a general rule of thumb, here are some common mappings:

    • Text fields should be stored as string columns.
    • Relationship fields should be stored as json columns. (Unless max_items is set to 1, in which case it should be stored as a string column.)
    • Number fields should be stored as integer or decimal columns.
  3. Run the migration:

    php artisan migrate
  4. If you're adding a column that requires an Eloquent cast (eg. a json or integer column), you will need to provide your own Entry model in order to set the appropriate casts. You can do this by creating a new model which extends the default Entry model:

    <?php
    
    namespace App\Models;
    
    class Entry extends \Statamic\Eloquent\Entries\EntryModel
    {
        protected function casts(): array
        {
            return [
                // The casts from Statamic's base model...
                'date'      => 'datetime',
                'data'      => 'json',
                'published' => 'boolean',
        
                // Your custom casts...
                'featured_images' => 'json',
            ];
        }
    }

    If you're using UUIDs as your entry IDs (which is the default if you imported existing entries into the database), you should extend the Statamic\Eloquent\Entries\UuidEntryModel class instead:

    class Entry extends \Statamic\Eloquent\Entries\UuidEntryModel

    Once created, you will need to update the model in the entries section of the configuration file:

    - 'model' => \Statamic\Eloquent\Entries\EntryModel::class,
    + 'model' => \App\Models\Entry::class,
  5. If you have existing entries, you will need to re-save them to populate the new columns. You can do this by pasting the following snippet into php artisan tinker:

    \Statamic\Facades\Entry::all()->each->save();
  6. And that's it! Statamic will now read and write data to the new columns in the entries table, rather than the data column.

Selecting specific blueprint types for Eloquent

By default, setting the driver for blueprints to eloquent will apply to all blueprints. However, if you only wish to move certain groups of blueprint over, you can do so by setting an array of namespaces, such as:

// config/statamic/eloquent-driver.php

'blueprints' => [
    'driver' => 'eloquent',
    'model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
    'namespaces' => ['forms', 'navigation'],
],

The above example will set all forms and navigation blueprints to use eloquent, while keeping the rest as files.

Upgrading

After updating to a new version of the Eloquent Driver, please ensure you run php artisan migrate to update your database to the latest schema.

Questions

Can I store users in the database too?

By default, Statamic users live in the users directory of your project. If you wish to move them to the database, please follow this guide.

Can I store some collections in the database, while keeping others in flat-files?

This driver does not make it possible to have some collections flat-file driven and others Eloquent driven. If you're looking for that, you may want to checkout the Runway addon, which is part of The Rad Pack.

statamic/eloquent-driver 适用场景与选型建议

statamic/eloquent-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 715.39k 次下载、GitHub Stars 达 125, 最近一次更新时间为 2020 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 statamic/eloquent-driver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 statamic/eloquent-driver 我们能提供哪些服务?
定制开发 / 二次开发

基于 statamic/eloquent-driver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 715.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 125
  • 点击次数: 16
  • 依赖项目数: 8
  • 推荐数: 1

GitHub 信息

  • Stars: 125
  • Watchers: 8
  • Forks: 98
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-19