1. Go to this page and download the library: Download byjg/micro-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/ */
byjg / micro-orm example snippets
class Users
{
public $id;
public $name;
public $createdate;
}
// Creating the mapping
$mapper = new \ByJG\MicroOrm\Mapper(
Users::class, // The full qualified name of the class
'users', // The table that represents this entity
'id' // The primary key field
);
// Optionally you can define table mappings between the propoerties
// and the database fields;
// The example below will map the property 'createdate' to the database field 'created';
$mapper->addFieldMapping(FieldMap::create('createdate')->withFieldName('created'));
$dataset = \ByJG\AnyDataset\Db\Factory::getDbRelationalInstance('mysql://user:password@server/schema');
$repository = new \ByJG\MicroOrm\Repository($dataset, $mapper);
// Get a single data from your ID
$users = $repository->get(10);
// Persist the entity into the database:
// Will INSERT if does not exists, and UPDATE if exists
$users->name = "New name";
$repository->save($users);
$updateConstraint = \ByJG\MicroOrm\UpdateConstraint()::instance()
->withAllowOnlyNewValuesForFields('name');
$users->name = "New name";
$repository->save($users, $updateConstraint);
$query = \ByJG\MicroOrm\Query::getInstance()
->table('users')
->fields(['id', 'name'])
->where('name like :part', ['part' => 'A%']);
// Will return a collection o 'Users'
$collection = $repository->getByQuery($query);
$query = \ByJG\MicroOrm\Query::getInstance()
->table('order')
->join('item', 'order.id = item.orderid')
->where('name like :part', ['part' => 'A%']);
// Will return a collection of Orders and Items:
// $collection = [
// [ $order, $item ],
// [ $order, $item ],
// ...
// ];
$collection = $orderRepository->getByQuery(
$query,
[
$itemRepository->getMapper()
]
);
// Create the Mapper and the proper fieldAlias
$orderMapper = new \ByJG\MicroOrm\Mapper(...);
$orderMapper->addFieldMapping(FieldMapping::create('id')->withFieldAlias('orderid'));
$itemMapper = new \ByJG\MicroOrm\Mapper(...);
$itemMapper->addFieldMappping(FieldMapping::create('id')->withFieldAlias('itemid'));
$query = \ByJG\MicroOrm\Query::getInstance()
->field('order.id', 'orderid')
->field('item.id', 'itemid')
/* Other fields here */
->table('order')
->join('item', 'order.id = item.orderid')
->where('name like :part', ['part' => 'A%']);
// Will return a collection of Orders and Items:
// $collection = [
// [ $order, $item ],
// [ $order, $item ],
// ...
// ];
$collection = $orderRepository->getByQuery(
$query,
[
$itemRepository->getMapper()
]
);
// Creating the mapping
$mapper = new \ByJG\MicroOrm\Mapper(
Users::class, // The full qualified name of the class
'users', // The table that represents this entity
'id', // The primary key field
function () {
// calculate and return the unique ID
}
);
// Creating the mapping
$mapper = new \ByJG\MicroOrm\Mapper(...);
$fieldMap = FieldMap::create('propertyname') // The property name of the entity class
// The field name of the table. if not defined will use the property name.
->withFieldName('fieldname')
// The field alias of the field in the table. if not defined will use the field name.
->withFieldAlias('alias')
// Returns the pre-processed value before UPDATE/INSERT the $field name
// If the function returns NULL this field will not be
// This observer will be called after insert, update or delete a record on the table 'triggerTable'
$myRepository->addObserver(new class($this->infoMapper->getTable()) implements ObserverProcessorInterface {
private $table;
public function __construct($table)
{
$this->table = $table;
}
public function process(ObserverData $observerData)
{
// Do something here
}
public function getObserverdTable(): string
{
return $this->table;
}
});
$recursive = \ByJG\MicroOrm\Recursive::getInstance('test')
->field('start', 1, 'start + 10')
->field('end', 120, "end - 10")
->where('start < 100')
;
$query = \ByJG\MicroOrm\Query::getInstance()
->withRecursive($recursive)
->fields(['start', 'end']);
/*
This will produce the following SQL:
WITH RECURSIVE test(start, end) AS (
SELECT 1 as start, 120 as end
UNION ALL SELECT start + 10, end - 10 FROM test WHERE start < 100
) SELECT start, end FROM test
*/
$repo1 = new Repository(...);
$repo2 = new Repository(...);
// Create the TransactionManager
$transactionManager = new TransactionManager();
$transactionManager->addRepository($repo1);
$transactionManager->addRepository($repo2);
// Start the transaction
$transactionManager->beginTransaction();
//
// Do some Repository operations with the repo;
// ...
// commit (or rollback all transactions)
$transactionManager->commitTransaction();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.