{"id":15104900,"url":"https://github.com/drmvc/database","last_synced_at":"2025-04-05T12:40:36.526Z","repository":{"id":56972287,"uuid":"86003921","full_name":"drmvc/database","owner":"drmvc","description":"Advanced plugin for work with database for DrMVC Framework","archived":false,"fork":false,"pushed_at":"2018-07-19T13:36:41.000Z","size":87,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T23:52:50.428Z","etag":null,"topics":["database","mongodb","multiple","pdo","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drmvc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-23T22:35:28.000Z","updated_at":"2018-07-18T10:26:31.000Z","dependencies_parsed_at":"2022-08-21T07:10:28.410Z","dependency_job_id":null,"html_url":"https://github.com/drmvc/database","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drmvc%2Fdatabase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drmvc%2Fdatabase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drmvc%2Fdatabase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drmvc%2Fdatabase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drmvc","download_url":"https://codeload.github.com/drmvc/database/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339132,"owners_count":20923009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","mongodb","multiple","pdo","sql"],"created_at":"2024-09-25T20:03:48.055Z","updated_at":"2025-04-05T12:40:36.506Z","avatar_url":"https://github.com/drmvc.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/drmvc/database/v/stable)](https://packagist.org/packages/drmvc/database)\n[![Build Status](https://travis-ci.org/drmvc/database.svg?branch=master)](https://travis-ci.org/drmvc/database)\n[![Total Downloads](https://poser.pugx.org/drmvc/database/downloads)](https://packagist.org/packages/drmvc/database)\n[![License](https://poser.pugx.org/drmvc/database/license)](https://packagist.org/packages/drmvc/database)\n[![PHP 7 ready](https://php7ready.timesplinter.ch/drmvc/database/master/badge.svg)](https://travis-ci.org/drmvc/database)\n[![Code Climate](https://codeclimate.com/github/drmvc/database/badges/gpa.svg)](https://codeclimate.com/github/drmvc/database)\n[![Scrutinizer CQ](https://scrutinizer-ci.com/g/drmvc/database/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/drmvc/database/)\n\n# DrMVC\\Database\n\nAdvanced module for work with databases and models.\n\n    composer require drmvc/database\n\nIt is a specific module, allows you to use multiple databases at the\nsame time, you can switch database from model class, you can call same\nmodel methods with different databases, you can work with MongoDB and\nMysql from one controller, everything what you want.\n\n## Supported databases\n\nFor now only databases from list below is supported, but support of\nsome new databases will be added soon.\n\n| Driver  | Database |\n|---------|----------|\n| mysql   | MySQL and MariaDB |\n| pgsql   | PostgreSQL |\n| sqlite  | SQLite (file and memory modes) |\n| mongodb | MongoDB (php 7.0 and above only) |\n\n## Database configuration\n\nYou can find few examples of database config files with description\nand links [here](extra/configs/).\n\nExample of MySQL `database.php` config file:\n\n```php\n\u003c?php\nreturn [\n    'default' =\u003e [\n        'driver'    =\u003e 'mysql',\n        'host'      =\u003e '127.0.0.1',\n        'port'      =\u003e '3306',\n        'username'  =\u003e 'admin',\n        'password'  =\u003e 'admin_pass',\n        'dbname'    =\u003e 'database',\n        'prefix'    =\u003e 'prefix_',\n        'collation' =\u003e 'utf8_unicode_ci',\n        'charset'   =\u003e 'utf8',\n    ],\n];\n```\n\nWhere:\n\n* default - name of database connection which must be used by default\n* driver - any driver from [this](#supported-databases) list\n* prefix - prefix of tables names, required by almost all methods (but not for `select()` or `rawSQL()`)\n\n## How to use\n\n### Basic example\n\nA small example of working with a database with the simplest\nimplementation of the basic CRUD logic, they do not have JOIN, ORDER\nand other complex methods, only basic functionality.\n\nSource code of `example.php` file:\n\n```php\n\u003c?php\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\n// Load configuration of current database instance\n$config = new \\DrMVC\\Config();\n$config-\u003eload(__DIR__ . '/database.php', 'database');\n\n// Initiate model, send database config and table name 'test' (without prefix)\n$model = new \\DrMVC\\Database\\Model($config, 'test');\n\n// Get all records from table\n$select = $model-\u003eselect();\n\n// Direct call query via model\n$where = ['name' =\u003e 'somename', 'email' =\u003e 'some@email'];\n$select = $model-\u003eselect($where);\n\n// Advanced example of select usage\n$bind = ['name' =\u003e 'somename', 'email' =\u003e 'some@email'];\n$raw = $model-\u003erawSQL(\"SELECT * FROM prefix_users WHERE name = :name AND email = :email\", $bind);\n\n// Call insert method\n$data = ['key' =\u003e 'value', 'key2' =\u003e 'value2'];\n$insert = $model-\u003einsert($data);\n\n// Update some data in table\n$data = ['key' =\u003e 'value', 'key2' =\u003e 'value2'];\n$where = ['id' =\u003e 111];\n$update = $model-\u003eupdate($data, $where);\n\n// Execute query in silent mode\n$model-\u003erawSQL('create table example_table');\n\n// Remove some item from table\n$where = ['id' =\u003e 111];\n$delete = $model-\u003edelete($where);\n```\n\n### Simple connect to database\n\nSometimes it is required to implement a system that works directly\nwith the database object (for example some ORM or custom queries),\nfor this purpose in `Database` class the `getInstance` method was\nimplemented.\n\n```php\n\u003c?php\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\nuse \\DrMVC\\Config;\nuse \\DrMVC\\Database;\n\n// Load configuration of current database instance\n$config = new Config();\n// Example in \"Database configs\" chapter\n$config-\u003eload(__DIR__ . '/database.php');\n\n// Create database object\n$db = new Database($config-\u003eget('default'));\n\n// Get only instance (PDO or MongoManager, depending on the \"driver\"\n// which you set in config)\n$instance = $db-\u003egetInstance();\n```\n\n### OOP style\n\nAs mentioned earlier in this library, implemented basic CRUD support,\nthat is, you can declare the models dynamically and write simple\nqueries into the database for which the ORM would be redundant. For\nexample, you need to insert a row, delete or query all rows from the\ntable.\n\n```php\n\u003c?php\n\nnamespace MyApp\\Models;\n\nuse DrMVC\\Database\\Model;\n\n//\n// You need create object of this model with DrMVC\\Config class as\n// first parameter, because it is required by parent Database class:\n//\n// parent::__construct(ConfigInterface $config = null)\n//\n\nclass Test extends Model\n{\n    protected $collection = 'test';\n\n    public function select_all()\n    {\n        return $this-\u003eselect();\n    }\n\n    public function sql_insert(array $data = ['key' =\u003e 'value', 'key2' =\u003e 'value2'])\n    {\n        return $this-\u003einsert($data);\n    }\n\n    public function sql_update(int $id)\n    {\n        $data = ['key' =\u003e 'value', 'key2' =\u003e 'value2'];\n        $where = ['id' =\u003e $id];\n        return $this-\u003eupdate($data, $where);\n    }\n\n    public function sql_delete(array $where)\n    {\n        return $this-\u003edelete($where);\n    }\n}\n```\n\n## Links\n\n* [DrMVC project website](https://drmvc.com/)\n* [PDO MySQL](http://php.net/manual/en/ref.pdo-mysql.connection.php)\n* [PDO PostgreSQL](http://php.net/manual/en/ref.pdo-pgsql.connection.php)\n* [PDO SQLite](http://php.net/manual/en/ref.pdo-sqlite.connection.php)\n* [MongoDB](http://php.net/manual/en/set.mongodb.php)\n* [AerodORM](https://github.com/drmvc/aerodorm)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrmvc%2Fdatabase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrmvc%2Fdatabase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrmvc%2Fdatabase/lists"}