bixev / light-orm
Bixev light object relationship manager
Installs: 2 166
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.3
- bixev/light-logger: >=1.0.0
README
This ORM aims to manipulate objects from database the simpliest way
Installation
It's recommended that you use Composer to install InterventionSDK.
composer require bixev/orm "~1.0"
This will install this library and all required dependencies.
so each of your php scripts need to require composer autoload file
<?php require 'vendor/autoload.php';
Usage
Multiple databases
Initialise database with your own. $databaseName
is null by default. You can override it in the model classes
\Bixev\ORM\API::setDatabaseGetter(function($databaseName){ $db = new \PDO(''); return $db; });
$databaseName
is used within the models to communicate with correct database
Repository
Get repository to manipulate objects
$exampleRepository = \Bixev\ORM\API::get('Example');
Create one object and store it into database
$example = $exampleRepository->createNew(); // same as // $example = new \Bixev\ORM\Example(); $example->example_2_id = 5; $example->name = "Tom"; $example->save(); echo $example->getId();
Retrieve existing record by id
$id = 3; $example = $exampleRepository->find($id); // same as // $example = new \Bixev\ORM\Example($id); echo $example->name;
Retrieve existing record by sql row
$db = new \PDO(''); $row = $db->query("SELECT id, name, example_2_id FROM user")->fetch(); // row has to contain all declared fields $user = new \Bixev\ORM\Example($row);
Search
$examples = $exampleRepository->findBy(['name' => "Tom"]); $examples = $exampleRepository->findAll(); foreach ($examples as $example) { echo $example->name; }
Advanced instanciation of collection (by sql rows)
$db = new \PDO(''); $rows = $db->query("SELECT id, name, example_2_id FROM user")->fetchAll(); // rows have to contain all declared fields $examples = $exampleRepository->newCollection($rows);
Search ONE
$example = $exampleRepository->findOneBy(['name' => "Tom"]);