2017 © Pedro Peláez
 

library database

Advanced plugin for work with database for DrMVC Framework

image

drmvc/database

Advanced plugin for work with database for DrMVC Framework

  • Wednesday, July 18, 2018
  • by EvilFreelancer
  • Repository
  • 1 Watchers
  • 0 Stars
  • 477 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 16 % Grown

The README.md

Latest Stable Version Build Status Total Downloads License PHP 7 ready Code Climate Scrutinizer CQ, (*1)

DrMVC\Database

Advanced module for work with databases and models., (*2)

composer require drmvc/database

It is a specific module, allows you to use multiple databases at the same time, you can switch database from model class, you can call same model methods with different databases, you can work with MongoDB and Mysql from one controller, everything what you want., (*3)

Supported databases

For now only databases from list below is supported, but support of some new databases will be added soon., (*4)

Driver Database
mysql MySQL and MariaDB
pgsql PostgreSQL
sqlite SQLite (file and memory modes)
mongodb MongoDB (php 7.0 and above only)

Database configuration

You can find few examples of database config files with description and links here., (*5)

Example of MySQL database.php config file:, (*6)

<?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',
    ],
];

Where:, (*7)

  • default - name of database connection which must be used by default
  • driver - any driver from this list
  • prefix - prefix of tables names, required by almost all methods (but not for select() or rawSQL())

How to use

Basic example

A small example of working with a database with the simplest implementation of the basic CRUD logic, they do not have JOIN, ORDER and other complex methods, only basic functionality., (*8)

Source code of example.php file:, (*9)

<?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);

Simple connect to database

Sometimes it is required to implement a system that works directly with the database object (for example some ORM or custom queries), for this purpose in Database class the getInstance method was implemented., (*10)

<?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();

OOP style

As mentioned earlier in this library, implemented basic CRUD support, that is, you can declare the models dynamically and write simple queries into the database for which the ORM would be redundant. For example, you need to insert a row, delete or query all rows from the table., (*11)

<?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);
    }
}

The Versions

18/07 2018

dev-master

9999999-dev https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

18/07 2018

3.0.1

3.0.1.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

15/04 2018

3.0.0

3.0.0.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

12/12 2017

0.1.9

0.1.9.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

03/11 2017

0.1.8

0.1.8.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

10/08 2017

0.1.7

0.1.7.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

15/06 2017

0.1.6

0.1.6.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

15/06 2017

0.1.5

0.1.5.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

30/05 2017

0.1.4

0.1.4.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

10/04 2017

0.1.3

0.1.3.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin php pdo mysql mongo sqlite pgsql drmvc

07/04 2017

0.1.2

0.1.2.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

plugin php pdo mysql mongo sqlite pgsql drmvc

06/04 2017

0.1.1

0.1.1.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

plugin php pdo mysql mongo sqlite pgsql drmvc

06/04 2017

0.1

0.1.0.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

plugin php pdo mysql mongo sqlite pgsql drmvc

26/03 2017

0.0.1

0.0.1.0 https://drmvc.com/

Advanced plugin for work with database for DrMVC Framework

  Sources   Download

MIT

The Requires

 

plugin php pdo mysql mongo sqlite pgsql drmvc