安装和配置¶
OscarDB 是神舟通用为 [Laravel Framework](https://laravel.com) PHP ORM 框架开发的软件包,提供了在线和离线安装方式。
安装要求¶
PHP 版本要求:PHP7.3, PHP7.4, PHP8.0
警告
版本支持¶
目前神通适配了主流的 Laravel8.x 框架。
PDO_ACI 验证¶
安装神通方言 Laravel 之前,先检查 PDO_ACI 是否已加载到当前的 PHP 运行时环境中。
执行命令 php -m | grep PDO_ACI,
如果输出项查询到 PDO_ACI,那么当前PHP运行时已加载 PDO_ACI;
否则需要正确加载 PDO_ACI 扩展库。
root@deb10:~# php -m | grep PDO_ACI
PHP Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
PDO_ACI
加载 PDO_ACI 扩展库详细内容请参考 PDO_ACI 开发手册 -> 安装与配置
在线安装¶
通过 [Composer](https://getcomposer.org) 在线安装:
composer require microestc/oscardb
执行以上命令, 将自动为项目生成 OscarDB 依赖提供程序。
离线安装¶
获取该项目源码¶
源码 Laravel-OscarDB 内容如下
.
├── composer.json
├── phpunit.xml
├── readme.md
├── src
│ ├── config
│ │ └── oscardb.php
│ └── Microestc
│ └── OscarDB
│ ├── Connectors
│ │ └── OscarConnector.php
│ ├── OscarConnection.php
│ ├── OscarDBServiceProvider.php
│ ├── Query
│ │ ├── Grammars
│ │ │ └── OscarGrammar.php
│ │ ├── OscarBuilder.php
│ │ └── Processors
│ │ └── OscarProcessor.php
│ └── Schema
│ ├── Grammars
│ │ └── OscarGrammar.php
│ └── OscarSchemaBuilder.php
└── tests
├── mocks
│ └── PDOMocks.php
├── OscarDBConnectorTest.php
├── OscarDBPDOProcessorTest.php
├── OscarDBQueryBuilderTest.php
└── OscarDBSchemaGrammarTest.php
12 directories, 17 files
配置项目依赖组件¶
在项目配置文件 composer.json 中添加下列配置
"require": {
"microestc/oscardb": "*"
},
"repositories": [
{
"packagist.org": false,
"type": "path",
"url": "../Laravel-OscarDB",
"options": {
"symlink": false
}
}
],
警告
"url": "../Laravel-OscarDB", 该路径为Laravel-OscarDB源码文件的路径
执行离线安装¶
composer update
安装结果¶
root@deb10:~/works/webapp# composer update
PHP Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
PHP Warning: Module 'xdebug' already loaded in Unknown on line 0
Warning: Module 'xdebug' already loaded in Unknown on line 0
Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
PHP Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
Composer is operating slower than normal because you have Xdebug enabled. See https://getcomposer.org/xdebug
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? y
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking microestc/oscardb (dev-master)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing microestc/oscardb (dev-master): Mirroring from ../Laravel-OscarDB
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
PHP Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
PHP Warning: Module 'xdebug' already loaded in Unknown on line 0
Warning: Module 'xdebug' already loaded in Unknown on line 0
Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: microestc/oscardb
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
77 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
PHP Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
Warning: Module 'PDO_ACI' already loaded in Unknown on line 0
PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
PHP Warning: Module 'xdebug' already loaded in Unknown on line 0
Warning: Module 'xdebug' already loaded in Unknown on line 0
Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
No publishable resources for tag [laravel-assets].
Publishing complete.
配置¶
接下来,使用 vendor:publish Artisan 命令发布 OscarDB 的配置文件。这会将 OscarDB 的配置文件 config/oscardb.php 复制到项目中.
php artisan vendor:publish --tag=oscardb-config
要完成安装,请将环境变量(通常在 .env 文件中)设置为 config/oscardb.php 使用的相应 env 变量,例如 DB_HOST, DB_USERNAME 等。
当然,您也可以手动从 OscarDB 源码中拷贝 或者 手动创建配置文件 config/oscardb.php
<?php
return [
'oscardb' => [
'driver' => 'oscar',
'url' => env('DB_URL'),
'host' => '127.0.0.1',
'port' => '2003',
'database' => 'osrdb',
'username' => 'sysdba',
'password' => 'szoscar55',
'charset' => 'utf8',
'prefix' => '',
'quoting' => false,
],
];
并且配置文件 config/app.php 中添加神通数据库提供服务
在文件中 providers 项加入以下内容
Microestc\OscarDB\OscarDBServiceProvider::class,
基本用法¶
OscarDB 软件包的配置文件位于 config/oscardb.php.
在这个文件中,您可以定义所有 Oscar 数据库连接。如果需要建立多个连接,只需复制示例连接即可。如果要使其中一个连接成为默认连接,请在文件 config/database.php 的“默认数据库连接名称”部分中输入为连接指定的名称。
例如在配置文件 config/database.php
'default' => 'oscardb',
配置 OscarDB 数据库连接后,可以正常使用 DB:: 运行查询, 例如:
$results = DB::select('select * from users where id = ?', [1]);
上面的语句假定您已将默认连接设置在 config/database.php 文件中,并且将始终返回一个 array 结果,例如。
$results = DB::connection('oscardb')->select('select * from users where id = ?', [1]);
就像内置的数据库驱动程序一样,您可以使用连接方法来访问您在 config/oscardb.php 文件中设置的 Oscar 数据库。
将记录插入到具有自动递增 ID 的表中,并返回最后插入自增列 ID
$id = DB::connection('oscardb')->table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0], 'userid'
);
警告
注意:使用 insertGetId 方法时,可以将自动递增的列名指定为 insertGetId 函数中的第二个参数。如果未指定,它将默认为“id”。
更多文档信息转至 [Laravel Database 文档](https://laravel.com/docs/8.x/database).