1. Go to this page and download the library: Download j0113/odb 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/ */
j0113 / odb example snippets
use J0113\ODB\PDODatabase;
PDODatabase::connect("server.localhost", "myusername", "mypassword", "mydatabase");
use J0113\ODB\PDODatabaseObject;
class User extends PDODatabaseObject
{
// First you may define a table (defaults to the className)
protected const TABLE = "users";
// Than some fields:
protected ?string $username = null;
protected ?string $firstname = null;
protected ?string $lastname = null;
// Maybe some relations (more about that later)
protected const RELATIONS = [
"orders" => ["toOne", "Order", "user_id", "id"],
];
// And getters and setters
public function getUsername() : ?string
...
}
$user = new User();
$user->setFirstname("John");
$user->setLastname("Smith");
$user->setUsername("john_smith");
$user->save();
echo $user->getId();
use J0113\ODB\QueryBuilder;
/**
* Get some users and display info
* @param QueryBuilder
* @return User[]|null
*/
$users = User::get(
(new QueryBuilder())
->where("firstname", "John")
->andWhere("lastname", "Smith")
->limit(5)
);
if (!empty($users)){
foreach ($users as $user){
echo "Hello " . $user->getFirstname() . "!\n";
}
}
/**
* Get one user
* @param QueryBuilder
* @return User|null
*/
$user = User::getOne(
(new QueryBuilder())->where("username", "oliver12345")
);
use J0113\ODB\QueryBuilder;
/**
* Get one user
* @param QueryBuilder
* @return User|null
*/
$user = User::getOne(
(new QueryBuilder())->where("username", "oliver12345")
);
$user->setFirstname("Oli");
$user->save();
use J0113\ODB\PDODatabaseObject;
use J0113\ODB\QueryBuilder;
class User extends PDODatabaseObject
{
/**
* Stores all the relations, these are read only and results in more than one query IF the relation is queried.
* Must be an sub array with "key" => ["type", "class", "column", "property"].
* - Key: is under what $obj->key the relation can be accessed.
* - Type: can be toOne (this-1) or toMany (this-multiple)
* - Class: the class it should be connected to (must be an child of PDODatabaseObject)
* - Column: the column it should search in
* - Property: the value to match against, is a property (or variable) of the current object
*
* SQL will generated by:
* - LIMIT: Type
* - FROM: Class
* - WHERE: column = this->property
*
* protected const RELATIONS = ["customer" => ["toOne", "Model\User", "id", "user_id"] ];
* $order->customer = Model\User(...)
*
* Tip: Use the PHPDoc '@ property' to let the IDE know about the relation.
*
* @var array
*/
protected const RELATIONS = [
"orders" => ["toMany", "Order", "user_id", "id"]
// $this->orders would result in Order::get((new QueryBuilder())->where("user_id", $this->id));
];
/**
* You may use a getter
* @return Order[]
*/
public function getOrders() : array
{
return $this->orders;
}
}
$user = User::getOne((new QueryBuilder()));
$user_orders = $user->getOrders();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.