john-kariuki/potato-orm

Andela Checkpoint two. A simple agnostic ORM that can perform the basic crud database operations.

1.1.3 2016-02-27 18:35 UTC

This package is not auto-updated.

Last update: 2024-09-11 22:49:37 UTC


README

#Checkpoint Two: Potato ORM

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

##Andela Checkpoint Two

A simple agnostic ORM that can perform the basic crud database operations.

#####TIA

##Install

Via Composer

$ composer require john-kariuki/potato-orm

Via Github

$ git clone git@github.com:andela-jkariuki/checkpoint-two-potato-orm.git

Update packages

$ composer install

Rename .env.example to .env

Update your .env to have your database credentials

DB_DRIVER = "sqlite"
DB_USERNAME = "homestead"
DB_PASSWORD = "secret"
DB_NAME = "potato.db"
DB_HOST = "localhost"
DB_PORT = 8000

##Usage

To use Potato ORM, extend the PotatoModel class.

The default naming convention for a table name associated with a class is it's lowercase plural syntax.

The default naming convention for the unique table field is id.

To overwrite the default naming conventions, declare protected variables as explained below.

/**
 * Default table name for Car class is cars.
 *
 * Default uniqueId field for table cars is id
 */
class Car extends PotatoModel
{
    //protected static $table = "your_table_name";
    //protected static $uniqueId = "your_unique_id";
}

Ensure the table exists before using the Potato ORM class to avoid exceptions.

Use the SQL statement template below

CREATE TABLE `cars` (
	`id`	INTEGER PRIMARY KEY AUTOINCREMENT,
	`name`	TEXT,
	`model`	TEXT,
	`year`	INTEGER
)

Potato ORM gives you access to the following methods;

Reading Data

//Get all rows that from a table
$cars = Car::getAll();

Inserting data

//insert a new row to the table
$car = new Car();
$car->name = "Boss";
$car->model = "Up";
$car->year = 2013;

$car->save(); //returns a boolean value. true or false if saved or not respectively.

Updating data

//Update an existing row in the database
$car = Car::find(1);
$car->name = "Me Hennesy";
$car->year = 2015;

Deleting data

//delete an existing row in the table
var_dump(Car::destroy(1));

###Sample Code

Ensure to write your code inside a try catch block to catch any exceptions

namespace DryRun;

use Potato\Manager\PotatoModel;
use PDOException;

require 'vendor/autoload.php';

class Car extends PotatoModel
{
    //protected static $table = "your_table_name";
    //protected static $uniqueId = "your_unique_id";
}
try {

    echo "Create a new Car\n";

    $car = new Car();
    $car->name = "Lambo";
    $car->model = "Hura";
    $car->year = 2013;

    if ( $car->save()) {
	   echo "\nCar has been created.\n\n";
    } else {
	   echo "There was an error saving your car.";
    }

    echo  "Find the car that has just been created and updated the name and year\n";

    $car = Car::find(1);
   
    $car->name = "Huracan";
    $car->year = 2015;


    echo $car->save();

    echo "\None row (of our new Car) has been updated.\n\n";

    echo "Get all cars and print them out\n\n";
    $cars = Car::getAll();
    print_r($cars);

    echo "\nAwesome.! Now let's delete the old car. Buy A new one if you can\n\n";

    var_dump(Car::destroy(1));
    print_r(Car::getAll());

    echo "\nYour old car is dead and gone\n";

} catch (PDOException $e) {
    echo $e->getMessage();
}

Contributing

Contributions are welcome and will be fully credited.

We accept contributions via Pull Requests on Github.

Pull Requests

  • PSR-2 Coding Standard - The easiest way to apply the conventions is to install PHP Code Sniffer.

  • Add tests! - Your patch won't be accepted if it doesn't have tests.

  • Document any change in behaviour - Make sure the README.md and any other relevant documentation are kept up-to-date.

  • Consider our release cycle - We try to follow SemVer v2.0.0. Randomly breaking public APIs is not an option.

  • Create feature branches - Don't ask us to pull from your master branch.

  • One pull request per feature - If you want to do more than one thing, send multiple pull requests.

  • Send coherent history - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.

Security

If you discover any security related issues, please email me at John Kariuki or create an issue.

Credits

John kariuki

License

The MIT License (MIT)

Copyright (c) 2016 John kariuki john.kariuki@andela.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.