1. Go to this page and download the library: Download hawkbit/database 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/ */
use Hawkbit\Database\ConnectionManager;
// add connections
$connection = ConnectionManager::getInstance()->get();
// setup schema
$connection->exec('CREATE TABLE post (id int, title VARCHAR(255), content TEXT, date DATETIME DEFAULT CURRENT_DATE )');
$gateway = $connection->createGateway('post');
// create a new post
$gateway->create()
->setValue('title', 'Anything')
->setValue('content', 'Lorem Ipsum')
->execute();
use Application\Persistence\Mappers\PostMapper;
// register mappers
$connection->getMapperLocator()->register(PostMapper::class);
use Application\Persistence\Entities\Post;
$entity = new Post();
$mapper = $connection->loadMapper($entity);
use Application\Persistence\Entities\Post;
// entity instance is also allowed
$mapper = $connection->loadMapper(Post::class);
// or create entity from mapper
use \Doctrine\DBAL\Query\QueryBuilder;
$posts = $mapper->select(function(QueryBuilder $queryBuilder){
// build even more complex queries
$queryBuilder->where('id = 1');
});
// process entries
/** @var \Application\Persistence\Entities\Post $post */
foreach ($posts as $post){
echo sprintf('<h1>%s</h1><p>%s</p>', $post->getTitle(), $post->getContent());
}
use \Doctrine\DBAL\Query\QueryBuilder;
$one = true;
$post = $mapper->select(function(QueryBuilder $queryBuilder){
// build even more complex queries
$queryBuilder->where('id = 1');
}, ['*'], $one);
try {
$connection->beginTransaction();
$gateway->update()->set('content', 'blah')->where('id = 1');
$gateway->update()->set('content', 'blub')->where('id = 2');
$gateway->delete()->where('id in(32,45,16)');
$gateway->create()->setValue('content', 'i am new!')->execute();
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
// you may want to pass exception to next catch
throw $e;
}
use Application\Persistence\Entities\Post;
$unitOfWork = $connection->createUnitOfWork();
$entity = new Post();
// or create entity from mapper
//$entity = $mapper->createEntity();
// create entity
$entity->setContent('cnt');
$unitOfWork->create($entity);
// commit transaction
if(false === $unitOfWork->commit()){
//handle exception
$unitOfWork->getException();
// get last processed entity
$unitOfWork->getLastProcessed();
}
// get all modified entities
$unitOfWork->getModified();
// get a list of all entities by state
$unitOfWork->getProcessed();
// find entity by primary key or compound key
$mapper = $connection->loadMapper($entity);
$mapper->find(['id' => 1]);
// update entity
$entity->setContent('FOO');
$unitOfWork->update($entity);
// delete entity
$unitOfWork->delete($entity);
// commit transaction, again
$unitOfWork->commit();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.