ganga/potato-orm

potato-orm is an SQLite ORM package.

This package's canonical repository appears to be gone and the package has been frozen as a result.

v1.0.1 2016-01-07 10:41 UTC

This package is not auto-updated.

Last update: 2019-05-21 16:54:06 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Scrutinizer Code Quality

POTATO ORM is an SQL database ORM, enabling quick and seemles interaction with an SQL databases. Tested with SQLite, MySQL and PgSQL

Install

Via Composer

$ composer require ganga/potato-orm

Usage

Configuration for SQLite

NOTE that in SQLite a database is created in the root folder if one does not already exist.

$sqliteConfig = [
	"type" => "sqlite", //case insensitive
	"database" = "test.db" //path to db file
];

Configuration for Other SQL database

NOTE: It is advisable that configuration settings are loaded from the .env file. If you do not have a .env file in your root folder or don't know about it, please read this phpdotenv project.

Provide database type, database name, database user, database password and the host in the SQL database configuration.

// Load the `.env` variables for the project
// Check the `.env.example` file in the root of the project to see the environment variables needed.
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

Create the configuration and connect to your database

$mysqlConfig = [
    "type" => getenv('DB_TYPE'),
    "database" => getenv('DB_NAME'),
    "user" => getenv('DB_USER'),
    "password" => getenv('DB_PASS'),
    "host" => getenv('DB_HOST')
];

Once you have a configuration, either for SQLite or any other SQL database, make a connection.

$conn = new Connection($mysqlConfig);

Once we have a connection, we create the models

// By declaring the class name, we check for
// a plural name of the class as the table name
// in this case the table name is users
class User extends Potato
{

}

Database interactions

// create a new user
$user = new User;
$user->name = "Ganga Christopher";
$user->age = 23;
$user->address = "Kindaruma 525";
$user->company = "Andela";
$user->save();

Get all users from the users table

$users = User::getAll(); // Returns an array of the users found in the db
$users = User::getAll(['name', 'age']); // Selects only the name and age fields from the database table

Get a single user from the users table

$user = User::getOne(2); // 2 represents the id of the user to be retrieved
$user = User::getOne(2, ['name', 'age']); // Selects only the name and age fields from the database table

Edit an existing user

NOTE: User::find($id); is strictly used for finding a user to update. To to find and return a user, please use User::getOne($id);

$user = User::find(2);
$user->name = "Devy Kerr";
$user->address = "466 Video Productions";
$user->save();

Delete a user

//returns true or false based on success of the query
User::destroy(2); // 2 represents the id of the user to be deleted

Testing

$ composer test

Credits

License

The MIT License (MIT). Please see License File for more information.