https://github.com/erusev/base
Simpler Database Intaractions in PHP
https://github.com/erusev/base
Last synced: 2 months ago
JSON representation
Simpler Database Intaractions in PHP
- Host: GitHub
- URL: https://github.com/erusev/base
- Owner: erusev
- License: mit
- Created: 2015-01-23T23:45:12.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-01-05T14:38:07.000Z (over 1 year ago)
- Last Synced: 2025-03-29T15:08:58.273Z (2 months ago)
- Language: PHP
- Homepage:
- Size: 56.6 KB
- Stars: 262
- Watchers: 15
- Forks: 16
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## Base
Base is a simple library that makes it easier to work with databases in PHP.
I have been using it since 2012 and I felt like it is about time I share it.
### Features
- Simple
- Intuitive
- Independent
- Secure
- Tested in 5.3, 5.4, 5.5, 5.6, 7 and [HHVM](http://hhvm.com/)### Installation
Include both `Base.php` and `Collection.php` or install [the composer package](https://packagist.org/packages/erusev/base).
### Examples
Connect to a database:
```php
# the constructor takes the same parameters as the PDO constructor
$Base = new \Base\Base('mysql:host=localhost;dbname=example', 'username', 'password');
```Work with records:
```php
# read user 1
$Base->readItem('user', 1);
# update the username of user 1
$Base->updateItem('user', 1, ['username' => 'john.doe']);
# create a user
$Base->createItem('user', ['username' => 'jane.doe', 'email' => '[email protected]']);
# delete user 1
$Base->deleteItem('user', 1);
```Work with collections:
```php
# read all users
$Base->find('user')->read();
# read the users that are marked as verified in a desc order
$Base->find('user')->whereEqual('is_verified', 1)->orderDesc('id')->read();
# read the user with the most reputation
$Base->find('user')->limit(1)->orderDesc('reputation')->readRecord();
# mark users 1 and 3 as verified
$Base->find('user')->whereIn('id', [1, 3])->update(['is_verified' => 1]);
# count the users that don't have a location
$Base->find('user')->whereNull('location')->count();
# plain sql conditions are also supported
$Base->find('user')->where('is_verified = ?', [1])->read();
```Handle relationships:
```php
# read the users that have a featured post
$Base->find('user')->has('post')->whereEqual('post.is_featured', 1)->read();
# read the posts of user 1
$Base->find('post')->belongsTo('user')->whereEqual('user.id', 1)->read();
# read the posts that are tagged "php"
$Base->find('post')->hasAndBelongsTo('tag')->whereEqual('tag.name', 'php')->read();
# unconventional FK names are also supported
$Base->find('user')->has('post', 'author_id')->whereEqual('user.id', 1)->read();
```Execute queries:
```php
# read all users
$Base->read('SELECT * FROM user');
# read user 1
$Base->readRecord('SELECT * FROM user WHERE id = ?', [1]);
# read the username of user 1
$Base->readField('SELECT username FROM user WHERE id = ?', [1]);
# read all usernames
$Base->readFields('SELECT username FROM user');
# update all users
$Base->update('UPDATE user SET is_verified = ?', [1]);
```### Notes
- Not tested on other RDBMSs than MySQL
- Relationship methods assume that table names are singular - ex: `user` instead of `users`
- Relationship methods assume that FK names end in `_id` - use `$fkEnding` to customize