1. Go to this page and download the library: Download blast/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/ */
blast / orm example snippets
use Blast\Orm\ConnectionManager;
$connection = ConnectionManager::create('mysql://root:root@localhost/defaultdb?charset=UTF-8');
use Blast\Orm\ConnectionManager;
$connections = new ConnectionManager();
use Blast\Orm\Query\Events\QueryBuilderEvent;
$query->getEmitter()->addListener('build.select', function (QueryBuilderEvent $event) {
$event->getBuilder()->setEntity(Post::class);
});
$result = $query->select()->from('post')->where('id = 1')->execute();
use Blast\Orm\Query\Events\QueryResultEvent;
use Blast\Orm\Query;
$query->getEmitter()->addListener('result.select', function (QueryResultEvent $event, Query $builder) {
$result = $event->getResult();
foreach ($result as $key => $value) {
$result[$key]['contentSize'] = strlen($value['content']);
}
$event->setResult($result);
});
$result = $query->select()->from('post')->where('id = 1')->execute();
use Blast\Orm\Query\Events\QueryBuilderEvent;
$query->getEmitter()->addListener('build.select', function (QueryBuilderEvent $event) {
$event->setCanceled(true);
});
use Blast\Orm\Query\Events\QueryResultEvent;
use Blast\Orm\Query;
$query->getEmitter()->addListener('result.select', function (QueryResultEvent $event) {
$event->setCanceled(true);
});
class Post
{
}
use Blast\Orm\Entity\Definition;
use Blast\Orm\Mapper;
use Blast\Orm\Query;
$definition = new Definition();
$definition->setConfiguration([
'tableName' => 'user_role'
]);
//from mapper
$mapper = new Mapper($definition);
//from query
$query = new Query($connection, $definition);
class Post
{
public static $definition = '\Acme\Domain\Entities\Definition\PostDefinition';
}
class Post
{
public static function definition(){
return '\Acme\Domain\Entities\Definition\PostDefinition';
}
}
class Post
{
public static function definition(){
return new PostDefinition;
}
}
use Blast\Orm\Entity\Provider;
// add an entity as class name
$provider = new Provider(Post::class);
// add an entity as object
$provider = new Provider(Post::class);
// add an entity as table name
// entity object is an array object
$provider = new Provider('user_roles');
class Post
{
/**
* Get table name
*
* @var string
*/
public static $tableName = 'post';
}
class Post
{
/**
* Get primary key name
*
* @var string
*/
public static $primaryKeyName = 'id';
}
use Blast\Orm\Mapper;
class Post
{
/**
* Get mapper for entity
*
* @return string
*/
public static $mapper = Mapper::class;
}
use Blast\Orm\Mapper;
class Post
{
/**
* Get mapper for entity
*
* @return string
*/
public static function relations(EntityWithRelation $entity, Mapper $mapper){
return [
$mapper->hasOne($entity, 'otherTable')
];
}
}
use Blast\Orm\Entity\Provider;
$post = new Post;
$postProvider = new Provider(Post::class);
use Blast\Orm\Mapper;
class Post {
private $comments = null;
public function getComments(){
return $this->comments;
}
public static function relation(Post $entity, Mapper $mapper){
return [
$mapper->hasMany($entity, Comments::class)
];
}
}
$post = $mapper->find(1);
$comments = $post->getComments()->execute();
use Blast\Orm\MapperFactoryInterface;
use Blast\Orm\MapperFactoryTrait;
use Blast\Orm\RepositoryInterface;
use Blast\Orm\Hydrator\HydratorInterface;
class PostRepository implements MapperFactoryInterface, RepositoryInterface
{
use MapperFactoryTrait;
/**
* Get repository entity
*/
public function getEntity(){
return Post::class;
}
/**
* Get a collection of all entities
*
* @return \SplStack|array
*/
public function all()
{
return $this->createMapper($this->getEntity())->select()->execute(HydratorInterface::HYDRATE_COLLECTION);
}
/**
* Find entity by primary key
*
* @param mixed $primaryKey
* @return \ArrayObject|\stdClass|object
*/
public function find($primaryKey)
{
return $this->createMapper($this->getEntity())->find($primaryKey)->execute(HydratorInterface::HYDRATE_ENTITY);
}
/**
* Save new or existing entity data
*
* @param object|array $data
* @return int|bool
*/
public function save($data)
{
return $this->createMapper($data)->save($data)->execute();
}
}
use Blast\Orm\AbstractRepository;
class PostRepository extends AbstractRepository {
use \Blast\Orm\Entity\EntityAwareTrait;
/**
* Init repository and bind related entity
*/
public function __construct(){
$this->setEntity(Post::class);
}
}
$postRepository = new PostRepository();
$post = $postRepository->find(1);
$posts = $postRepository->all();
foreach($posts as $post){
//do something
}
$post = new Post();
$post['title'] = 'Hello World';
$post['content'] = 'Some content about hello world.';
$post['date'] = new \DateTime();
//create or update entity
$repository->save($post);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.