vakata / database
A database abstraction with support for various drivers.
Installs: 5 302
Dependents: 5
Suggesters: 2
Security: 0
Stars: 3
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=8.0.0
- vakata/collection: ~1.5
- dev-master
- 5.2.6
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.20
- 5.1.19
- 5.1.18
- 5.1.17
- 5.1.16
- 5.1.15
- 5.1.14
- 5.1.13
- 5.1.12
- 5.1.11
- 5.1.10
- 5.1.9
- 5.1.8
- 5.1.7
- 5.1.6
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.10.8
- 4.10.7
- 4.10.6
- 4.10.5
- 4.10.4
- 4.10.3
- 4.10.2
- 4.10.1
- 4.10.0
- 4.9.4
- 4.9.3
- 4.9.2
- 4.9.1
- 4.9.0
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.2
- 4.7.1
- 4.7.0
- 4.6.2
- 4.6.1
- 4.6.0
- 4.5.12
- 4.5.11
- 4.5.10
- 4.5.9
- 4.5.8
- 4.5.7
- 4.5.6
- 4.5.5
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.0
- 3.30.0
- 3.29.0
- 3.28.3
- 3.28.2
- 3.28.1
- 3.28.0
- 3.27.2
- 3.27.1
- 3.27.0
- 3.26.1
- 3.26.0
- 3.25.0
- 3.24.1
- 3.24.0
- 3.23.0
- 3.22.0
- 3.21.0
- 3.20.0
- 3.19.5
- 3.19.4
- 3.19.3
- 3.19.2
- 3.19.1
- 3.19.0
- 3.18.1
- 3.18.0
- 3.17.2
- 3.17.1
- 3.17.0
- 3.16.0
- 3.15.0
- 3.14.4
- 3.14.3
- 3.14.2
- 3.14.1
- 3.14.0
- 3.13.0
- 3.12.0
- 3.11.6
- 3.11.5
- 3.11.4
- 3.11.3
- 3.11.2
- 3.11.1
- 3.11.0
- 3.10.5
- 3.10.4
- 3.10.3
- 3.10.2
- 3.10.1
- 3.10.0
- 3.9.5
- 3.9.4
- 3.9.3
- 3.9.2
- 3.9.1
- 3.9.0
- 3.8.3
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.5
- 3.7.4
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.1
- 2.0.0
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.13
- 1.2.12
- 1.2.11
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-11-28 13:57:56 UTC
README
A database abstraction with support for various drivers (mySQL, postgre, oracle, ibase, sqlite and even PDO).
Install
Via Composer
$ composer require vakata/database
Usage
$db = new \vakata\database\DB('mysql://user:pass@127.0.0.1/database_name?charset=utf8'); // get an array result: $db->all('SELECT id, name FROM table'); // [ [ 'id' => 1, 'name' => 'name 1' ], [ 'id' => 2, 'name' => 'name 2' ] ] // passing parameters $db->all('SELECT id, name FROM table WHERE id = ? OR id = ?', [ 1, 2]); // [ [ 'id' => 1, 'name' => 'name 1' ], [ 'id' => 2, 'name' => 'name 2' ] ] // if selecting a single column there is no wrapping array: $db->all('SELECT name FROM table'); // [ 'name 1', 'name 2' ] // setting a key for the resulting array $db->all('SELECT id, name FROM table', null, 'id'); // [ 1 => [ 'id' => 1, 'name' => 'name 1' ], 2 => [ 'id' => 2, 'name' => 'name 2' ] ] // skipping the key (which leaves a single column so it is not wrapped anymore) $db->all('SELECT id FROM table', null, 'id', true); // [ 1 => 'name 1', 2 => 'name 2' ] // selecting a single row: $db->one('SELECT id, name FROM table WHERE id = ?', [1]); // [ 'id' => 1, 'name' => 'name 1' ] // selecting a single value from a single row (no wrapping array): $db->one('SELECT name FROM table WHERE id = ?', [1]); // "name 1" // insert / update / delete queries (affected rows count and last insert ID) $db->query("UPDATE table SET name = ? WHERE id = ?", ['asdf', 1])->affected(); // 1 $db->query("INSERT INTO table (name) VALUES(?)", ['asdf'])->insertID(); // 3 $db->query("DELETE FROM table WHERE id = ?", [3])->affected(); // 1 // queries using the "all" method can also use the "get" method // "get" does not create an array in memory, instead it fetches data from the mysql client // the resulting object is not an array but can be iterated and supports indexes // basically it can be used as an array as it implements all neccessary interfaces foreach($db->get('SELECT id, name FROM table') as $v) { echo $v['id'] . ' '; } // 1 2 $db->get('SELECT id, name FROM table', null, 'id', true)[2]; // "name 2" // SHORTCUT METHODS // assuming there is a book table with a name column foreach ($schema->book() as $book) { echo $book['name'] . "\n"; } // you could of course filter and order foreach ($schema->book()->filter('year', 2016)->sort('name') as $book) { // iterate over the books from 2016 } // the same as above foreach ($schema->book()->filterByYear(2016)->sortByName() as $book) { // iterate over the books from 2016 } // if using mySQL or Oracle (others to come soon) foreign keys are automatically detected and can be fetched // for example if there is an author table and the book table references it foreach ($schema->book()->with('author') as $book) { echo $book['author']['name'] . "\n"; } // provided there is a linking table book_tag and a tag table and each book has many tags you can do this foreach ($schema->book()->with('tag') as $book) { echo $book['tag'][0]['name'] . "\n"; // the name of the first tag which the current book has } // filtering and ordering works on relations too $schema->book()->filter('author.name', 'A. Name'); // you can also specify what fields to select $schema->book()->select(['name', 'author.name']); // there are "low-level" where / order and limit methods $schema->book() ->with('author') ->where('created = CURDATE() OR promoted = ?', [1]) ->order("author.name ASC") ->limit(5) ->select(); // when not dealing with foreign keys you can also join tables (and use group by / having) $schema->categories() ->join('questions', [ 'category_id' => 'id' ]) ->group(['id']) ->having('cnt > ?', [300]) ->order('cnt DESC') ->limit(2) ->select(['id', 'cnt' => 'COUNT(questions.id)']) // you can also insert / update or delete records $schema->book()->insert(['name' => 'Book title']); $schema->book()->where('id = 5')->update(['name' => 'New title']); $schema->book()->where('id = 5')->delete();
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email github@vakata.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.