uepg/laravel-sybase

Laravel 10.x 的基于 Sybase 的 Eloquent 模块扩展

4.0.0 2024-06-07 17:46 UTC

README

Packagist Version PHP from Packagist Packagist GitHub contributors GitHub

  • 启用使用多种类型的字段。
  • 使用默认 eloquent:与 odbc 和 dblib 兼容!
  • 迁移!(进行中 - Work in Progress)

安装

在您的 composer.json 文件的 require 部分添加以下内容

Laravel 5.1, 5.2, 5.3

"uepg/laravel-sybase": "~1.0"

Laravel 5.4, 5.5, 5.6, 5.7, 5.8, 6.x, 7.x, 8.x, 9.x

"uepg/laravel-sybase": "~2.0"

Laravel 10.x

"uepg/laravel-sybase": "~3.0" // old version
//or The new version
"uepg/laravel-sybase": "~4.0" // new version

执行以下命令更新包依赖项

composer update

config/app.php 文件中您的 providers 数组中添加以下条目,在 Laravel 5.5 或更高版本中是可选的

Uepg\LaravelSybase\SybaseServiceProvider::class,

config/app.php 文件中您的 aliases 数组中添加以下条目,在 Laravel 5.5 或更高版本中是可选的

'UepgBlueprint' => Uepg\LaravelSybase\Database\Schema\Blueprint::class,

更新您的 config/database.php 的默认驱动程序为 sybase 或您自定义的 odbc 的设置。以下是一个示例

<?php

...

return [
    ...

    'connections' => [
        ...

        'sybase' => [
            'driver' => 'sybasease',
            'host' => env('DB_HOST', 'sybase.myserver.com'),
            'port' => env('DB_PORT', '5000'),
            'database' => env('DB_DATABASE', 'mydatabase'),
            'username' => env('DB_USERNAME', 'user'),
            'password' => env('DB_PASSWORD', 'password'),
            'charset' => 'utf8', // Experimental yet, prefer use the `DB_CHARSET` and `APPLICATION_CHARSET`
            'prefix' => '',
        ],

        ...
    ],

    ...
]

更新您的 .env 文件为 sybase 或您自定义的 odbc 的设置。以下是一个示例

...

DB_CONNECTION=sybase
DB_HOST=sybase.myserver.com
DB_PORT=5000
DB_DATABASE=mydatabase
DB_USERNAME=user
DB_PASSWORD=password

...

配置 freetds 驱动程序

在 Linux 系统中,驱动程序版本必须在 freetds.conf 文件中设置为正确的字符集页面使用。

该文件通常位于 /etc/freetds/freetds.conf。以下示例将全局部分配置如下

[global]
    # TDS protocol version
    tds version = 5.0

配置数据库和应用程序之间的字符集

为了配置数据库和应用程序之间的字符集,在 .env 文件中添加 DB_CHARSETAPPLICATION_CHARSET 字段,以下是一个示例

DB_CHARSET=CP850
APPLICATION_CHARSET=UTF8

配置缓存

由于库在收到请求时总是查询表信息,因此可以使用缓存来避免过多的查询。

要使用缓存,将 SYBASE_CACHE_COLUMNSSYBASE_CACHE_COLUMNS_TIME 字段添加到 .env 文件中,以下是一个示例

SYBASE_CACHE_COLUMNS=true
SYBASE_CACHE_COLUMNS_TIME=3600 # cache table information by `3600` seconds

设置使用数值数据类型

在迁移文件中,您必须将 use Illuminate\Database\Schema\Blueprint; 替换为 use Uepg\LaravelSybase\Database\Schema\Blueprint;。以下是一个示例

<?php

use Illuminate\Support\Facades\Schema;
// use Illuminate\Database\Schema\Blueprint;
use Uepg\LaravelSybase\Database\Schema\Blueprint; // or "use UepgBlueprint as Blueprint"
use Illuminate\Database\Migrations\Migration;

class CreateTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->numeric('column_name', length, autoIncrement);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('table_name');
    }
}