spoom-php/sql-mysql

Spoom 框架的 MySQL 支持

安装: 4

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:spoom-extension

dev-development 2018-09-02 10:14 UTC

This package is auto-updated.

Last update: 2024-09-12 09:47:08 UTC


README

Spoom 是一系列协作库(扩展),您可以使用它“构建”一个符合您需求的框架。

关于 MySQL

...

安装

使用以下命令安装最新版本:

$ composer require spoom-php/sql-mysql

使用方法

您可以使用以下方式创建连接和执行命令:

<?php require __DIR__ . '/vendor/autoload.php';

use Spoom\Sql\MySQL;

// TODO ...you should create an application first...

// create a (lazy) connection. You can force to connect with $connection->connect()
$connection = new MySQL\Connection( '127.0.0.1:3306', 'root', '', 'test_database' );

// select some items from the table 'test', where foo is 'bar'
$result = $connection->execute( 'SELECT title FROM test WHERE foo = {foo}', [ 'foo' => 'bar' ] );

// the statment can be created in builder style. The code below is equivalent with the above ->execute() but it's universal
// $result = $connection->statement()->addTable( 'test' )->addField( 'title' )->addFilter( 'foo = {foo}' )->search( [ 'foo' => 'bar' ] );

// results used in a loop is equals with $result->getArrayList()
foreach( $result as $i => $item ) {
  echo "{$i}. item's title is '{$item['title']}'\n";
}

您还可以从扩展公共目录中的配置文件创建连接。文件 spoom/spoom-sql-mysql/configuration/connection.json 应包含类似以下内容:

{
  "myfancyconnection": {
    "host": "127.0.0.1:3306",
    "user": "root",
    "password": "",
    "database": "test_database",
    
    "option": {}
  }
}

然后您就可以进行以下操作:

<?php 

use Spoom\Sql\MySQL;

// note that every instance from the same configuration name will be the exact same object
$connection = MySQL\Connection::instance( 'myfancyconnection' );

许可证

Spoom 框架是开源软件,采用MIT 许可证授权。