PHPackages                             inlm/query-object - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. inlm/query-object

AbandonedLibrary[Database &amp; ORM](/categories/database)

inlm/query-object
=================

Concept of Query Object for LeanMapper

v2.0.1(7y ago)02.3kMITPHP &gt;=5.3.0

Since Dec 29Compare

[ Source](https://github.com/inlm/query-object)[ Packagist](https://packagist.org/packages/inlm/query-object)[ RSS](/packages/inlm-query-object/feed)WikiDiscussions Synced yesterday

READMEChangelog (3)Dependencies (2)Versions (9)Used By (0)

**WARNING!** This library is **ABANDONED**, use [mibk/LeanMapperQuery](https://github.com/mibk/LeanMapperQuery) instead. It contains all changes from this library and much more.

---

Lean Mapper Query
=================

[](#lean-mapper-query)

Lean Mapper Query is a concept of a *query object* for [Lean Mapper library](https://github.com/Tharos/LeanMapper) which helps to build complex queries using automatic joins (*idea taken from [NotORM library](http://www.notorm.com/)*). Look at the [suggested base classes](https://gist.github.com/mbohuslavek/9410266). For Czech documentation have a look at the [wiki](https://github.com/inlm/query-object/wiki).

Features
--------

[](#features)

- it behaves as an `SQL` preprocessor, hence most SQL expressions are available
- automatic joins using *dot notation* (`@book.tags.name`)
- ability to query both repositories or entities
- support for implicit filters

Installation
------------

[](#installation)

It can be installed via [Composer](http://getcomposer.org/download).

```
composer require inlm/query-object

```

What does it do?
----------------

[](#what-does-it-do)

Suppose we have following repositories:

```
class BaseRepository extends LeanMapper\Repository
{
	public function find(IQuery $query)
	{
		$this->createEntities($query
			->applyQuery($this->createFluent(), $this->mapper)
			->fetchAll()
		);
	}
}

class BookRepository extends BaseRepository
{
}
```

and following entities:

```
/**
 * @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
{
}
```

We build a *query*:

```
$query = new Inlm\QueryObject\Query;
$query->where('@author.name', 'Karel');
```

Now, if we want to get all books whose author's name is Karel, we have to do this:

```
$bookRepository = new BookRepository(...);
$books = $bookRepository->find($query);
```

Database query will look like this:

```
SELECT [book].*
FROM [book]
LEFT JOIN [author] ON [book].[author_id] = [author].[id]
WHERE ([author].[name] = 'Karel')
```

You can see it performs automatic joins via *dot notation*. It supports all types of relationships which are known to **Lean Mapper**.

It is very easy to use SQL functions. We can update query like this:

```
$query->where('DATE(@pubdate) > %d', '1998-01-01');
$books = $bookRepository->find($query);
```

and change the database query into following:

```
SELECT [book].*
FROM [book]
LEFT JOIN [author] ON [book].[author_id] = [author].[id]
WHERE ([author].[name] = 'Karel') AND (DATE([book].[pubdate]) > '1998-01-01')
```

Don't repeat yourself
---------------------

[](#dont-repeat-yourself)

You can extend `Query` and define own methods.

```
class BookQuery extends Inlm\QueryObject\Query
{
	public function restrictAvailable()
	{
		$this->where('@available', TRUE)
			->orderBy('@author.name');
		return $this;
	}
}

/////////

$query = new BookQuery;
$query->restrictAvailable();
$books = $this->bookRepository->find($query);
```

Querying entities
-----------------

[](#querying-entities)

It is also possible to query an entity property (*currently only those properties with `BelongsToMany` or `HasMany` relationships*). Let's build `BaseEntity`:

```
class BaseEntity extends Inlm\QueryObject\Entity
{
	protected static $magicMethodsPrefixes = array('find');

	protected function find($field, IQuery $query)
	{
		$entities = $this->queryProperty($field, $query);
		return $this->entityFactory->createCollection($entities);
	}
}

/*
 * ...
 */
class Book extends BaseEntity
{
}
```

*Note that `BaseEntity` extends `Inlm\QueryObject\Entity` to make the following possible.*

We have defined the `find` method as `protected` because with specifying the method name in `$magicMethodsPrefixes` property you can query entities like this:

```
$book; // previously fetched instance of an entity from a repository
$query = new LeanMapper\Query;
$query->where('@name !=', 'ebook');
$tags = $book->findTags($query);
```

*The magic method `findTags` will eventually call your protected method `find` with 'tags' as the 1 argument.*

The resulting database query looks like this:

```
SELECT [tag].*
FROM [tag]
WHERE [tag].[id] IN (1, 2) AND ([tag].[name] != 'ebook')
```

The first condition in where clause `[tag].[id] IN (1, 2)` is taken from the entity traversing (*tags are queried against this particular book entity's own tags*).

What else you can do?
---------------------

[](#what-else-you-can-do)

If we slightly modify our `BaseRepository` and `BaseEntity` we can simplify working with query objects. *To achieve this look at the [suggested base classes](https://gist.github.com/mbohuslavek/9410266)*. It makes the following possible.

```
$books = $bookRepository->query()
	->where('@author.name', 'Karel')
	->where('DATE(@pubdate) > ?', '1998-01-01')
	->find();

// or...

$tags = $book->queryTags()
	->where('@name !=', 'ebook')
	->find();
```

License
-------

[](#license)

Copyright (c) 2013 Michal Bohuslávek

Licensed under the MIT license.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 78.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~237 days

Recently: every ~193 days

Total

8

Last Release

2905d ago

Major Versions

v0.8 → v1.0.02016-05-28

v0.9.0 → v2.0.02017-01-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c980b1511b4a0350442dc23d89c99d4d9a2411b7e765d52c133ccacf616968b?d=identicon)[janpecha](/maintainers/janpecha)

---

Top Contributors

[![mibk](https://avatars.githubusercontent.com/u/2324898?v=4)](https://github.com/mibk "mibk (62 commits)")[![janpecha](https://avatars.githubusercontent.com/u/637719?v=4)](https://github.com/janpecha "janpecha (17 commits)")

### Embed Badge

![Health badge](/badges/inlm-query-object/health.svg)

```
[![Health](https://phpackages.com/badges/inlm-query-object/health.svg)](https://phpackages.com/packages/inlm-query-object)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M117](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)[mbohuslavek/leanmapper-query

Concept of Query Object for LeanMapper

1012.1k2](/packages/mbohuslavek-leanmapper-query)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
