PHP code example of john-kariuki / potato-orm

1. Go to this page and download the library: Download john-kariuki/potato-orm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

john-kariuki / potato-orm example snippets


/**
 * 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";
}

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

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

//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.

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

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

namespace DryRun;

use Potato\Manager\PotatoModel;
use PDOException;

r_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();
}