drmvc/database

DrMVC 框架的数据库高级插件

3.0.1 2018-07-18 10:26 UTC

This package is auto-updated.

Last update: 2024-08-29 03:47:39 UTC


README

Latest Stable Version Build Status Total Downloads License PHP 7 ready Code Climate Scrutinizer CQ

DrMVC\Database

用于操作数据库和模型的先进模块。

composer require drmvc/database

这是一个特定模块,允许您同时使用多个数据库,您可以从模型类切换数据库,可以使用不同数据库调用相同的模型方法,您可以从一个控制器中操作 MongoDB 和 Mysql,一切您想要的。

支持的数据库

目前仅支持以下列表中的数据库,但很快将添加对新数据库的支持。

数据库配置

您可以在这里找到一些数据库配置文件的示例,包括描述和链接。

MySQL database.php 配置文件示例

<?php
return [
    'default' => [
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'port'      => '3306',
        'username'  => 'admin',
        'password'  => 'admin_pass',
        'dbname'    => 'database',
        'prefix'    => 'prefix_',
        'collation' => 'utf8_unicode_ci',
        'charset'   => 'utf8',
    ],
];

位置

  • default - 默认使用的数据库连接名称
  • driver - 此列表中的任何驱动程序
  • prefix - 表名称的前缀,几乎所有方法都需要(但不是 select()rawSQL()

如何使用

基本示例

这是一个使用最简单的 CRUD 逻辑操作的数据库示例,它们没有 JOIN、ORDER 等复杂方法,只有基本功能。

example.php 文件的源代码

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

// Load configuration of current database instance
$config = new \DrMVC\Config();
$config->load(__DIR__ . '/database.php', 'database');

// Initiate model, send database config and table name 'test' (without prefix)
$model = new \DrMVC\Database\Model($config, 'test');

// Get all records from table
$select = $model->select();

// Direct call query via model
$where = ['name' => 'somename', 'email' => 'some@email'];
$select = $model->select($where);

// Advanced example of select usage
$bind = ['name' => 'somename', 'email' => 'some@email'];
$raw = $model->rawSQL("SELECT * FROM prefix_users WHERE name = :name AND email = :email", $bind);

// Call insert method
$data = ['key' => 'value', 'key2' => 'value2'];
$insert = $model->insert($data);

// Update some data in table
$data = ['key' => 'value', 'key2' => 'value2'];
$where = ['id' => 111];
$update = $model->update($data, $where);

// Execute query in silent mode
$model->rawSQL('create table example_table');

// Remove some item from table
$where = ['id' => 111];
$delete = $model->delete($where);

简单连接到数据库

有时需要实现直接与数据库对象(例如某些 ORM 或自定义查询)交互的系统,为此在 Database 类中实现了 getInstance 方法。

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

use \DrMVC\Config;
use \DrMVC\Database;

// Load configuration of current database instance
$config = new Config();
// Example in "Database configs" chapter
$config->load(__DIR__ . '/database.php');

// Create database object
$db = new Database($config->get('default'));

// Get only instance (PDO or MongoManager, depending on the "driver"
// which you set in config)
$instance = $db->getInstance();

面向对象风格

如前所述,此库实现了基本的 CRUD 支持,即您可以动态声明模型并写入简单的数据库查询,对于这些查询 ORM 是多余的。例如,您需要插入一行、删除或查询表中的所有行。

<?php

namespace MyApp\Models;

use DrMVC\Database\Model;

//
// You need create object of this model with DrMVC\Config class as
// first parameter, because it is required by parent Database class:
//
// parent::__construct(ConfigInterface $config = null)
//

class Test extends Model
{
    protected $collection = 'test';

    public function select_all()
    {
        return $this->select();
    }

    public function sql_insert(array $data = ['key' => 'value', 'key2' => 'value2'])
    {
        return $this->insert($data);
    }

    public function sql_update(int $id)
    {
        $data = ['key' => 'value', 'key2' => 'value2'];
        $where = ['id' => $id];
        return $this->update($data, $where);
    }

    public function sql_delete(array $where)
    {
        return $this->delete($where);
    }
}

链接