PHP code example of mbohuslavek / leanmapper-query
1. Go to this page and download the library: Download mbohuslavek/leanmapper-query 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/ */
mbohuslavek / leanmapper-query example snippets
class BaseRepository extends LeanMapper\Repository
{
public function find(Query $query)
{
$this->createEntities($query
->applyQuery($this->createFluent(), $this->mapper)
->fetchAll()
);
}
}
class BookRepository extends BaseRepository
{
}
/**
* @property int $id
* @property string $name
*/
class Tag extends LeanMapper\Entity
{
}
/**
* @property int $id
* @property Author $author m:hasOne
* @property Tag[] $tags m:hasMany
* @property DateTime $pubdate
* @property string $name
* @property bool $available
*/
class Book extends LeanMapper\Entity
{
}
/**
* @property int $id
* @property string $name
* @property Book[] $books m:belongsToMany
*/
class Author extends LeanMapper\Entity
{
}
$query = new LeanMapperQuery\Query;
$query->where('@author.name', 'Karel');
$bookRepository = new BookRepository(...);
$books = $bookRepository->find($query);
class BookQuery extends LeanMapperQuery\Query
{
public function restrictAvailable()
{
$this->where('@available', true)
->orderBy('@author.name');
return $this;
}
}
/////////
$query = new BookQuery;
$query->restrictAvailable();
$books = $this->bookRepository->find($query);
class BaseEntity extends LeanMapperQuery\Entity
{
protected static $magicMethodsPrefixes = ['find'];
protected function find($field, Query $query)
{
$entities = $this->queryProperty($field, $query);
return $this->entityFactory->createCollection($entities);
}
}
/*
* ...
*/
class Book extends BaseEntity
{
}
$book; // previously fetched instance of an entity from a repository
$query = new LeanMapper\Query;
$query->where('@name !=', 'ebook');
$tags = $book->findTags($query);