{"id":29128215,"url":"https://github.com/mibk/leanmapperquery","last_synced_at":"2025-06-30T01:37:49.255Z","repository":{"id":12832421,"uuid":"15507814","full_name":"mibk/LeanMapperQuery","owner":"mibk","description":"Concept of Query Object for LeanMapper","archived":false,"fork":false,"pushed_at":"2025-04-30T13:53:02.000Z","size":173,"stargazers_count":10,"open_issues_count":1,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-31T02:43:40.386Z","etag":null,"topics":["database","leanmapper","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mibk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-12-29T11:58:20.000Z","updated_at":"2025-04-30T13:53:05.000Z","dependencies_parsed_at":"2025-04-30T14:56:15.345Z","dependency_job_id":"e4a7380a-844d-49ef-a14a-e00546bd9f49","html_url":"https://github.com/mibk/LeanMapperQuery","commit_stats":{"total_commits":104,"total_committers":4,"mean_commits":26.0,"dds":"0.29807692307692313","last_synced_commit":"a7b03d5c6350c9df112dfd035fc3bc6e055dc5af"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/mibk/LeanMapperQuery","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mibk%2FLeanMapperQuery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mibk%2FLeanMapperQuery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mibk%2FLeanMapperQuery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mibk%2FLeanMapperQuery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mibk","download_url":"https://codeload.github.com/mibk/LeanMapperQuery/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mibk%2FLeanMapperQuery/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262694476,"owners_count":23349854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","leanmapper","php","php-library"],"created_at":"2025-06-30T01:37:48.266Z","updated_at":"2025-06-30T01:37:49.235Z","avatar_url":"https://github.com/mibk.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Lean Mapper Query\n=================\n\nLean Mapper Query is a concept of a *query object* for\n[Lean Mapper library](https://github.com/Tharos/LeanMapper) which helps to build complex\nqueries using automatic joins (*idea taken from [NotORM library](https://www.notorm.com/)*).\nLook at the [suggested base classes](https://gist.github.com/mibk/9410266). For Czech\ndocumentation have a look at the [wiki](https://github.com/mibk/LeanMapperQuery/wiki).\n\nFeatures\n--------\n\n- behaves as a `SQL` preprocessor, hence most SQL expressions are available\n- automatic joins using the *dot notation* (`@book.tags.name`)\n- ability to query repositories or entities\n- support for implicit filters\n\n\nInstallation\n------------\n\nIt can be installed via [Composer](https://getcomposer.org/download).\n\n```\ncomposer require mbohuslavek/leanmapper-query\n```\n\n\nWhat does it do?\n----------------\n\nSuppose we have the following repositories:\n\n```php\nclass BaseRepository extends LeanMapper\\Repository\n{\n\tpublic function find(Query $query)\n\t{\n\t\t$this-\u003ecreateEntities($query\n\t\t\t-\u003eapplyQuery($this-\u003ecreateFluent(), $this-\u003emapper)\n\t\t\t-\u003efetchAll()\n\t\t);\n\t}\n}\n\nclass BookRepository extends BaseRepository\n{\n}\n```\n\nand the following entities:\n\n```php\n/**\n * @property int    $id\n * @property string $name\n */\nclass Tag extends LeanMapper\\Entity\n{\n}\n\n/**\n * @property int      $id\n * @property Author   $author m:hasOne\n * @property Tag[]    $tags m:hasMany\n * @property DateTime $pubdate\n * @property string   $name\n * @property bool     $available\n */\nclass Book extends LeanMapper\\Entity\n{\n}\n\n/**\n * @property int    $id\n * @property string $name\n * @property Book[] $books m:belongsToMany\n */\nclass Author extends LeanMapper\\Entity\n{\n}\n```\n\nWe build a *query*:\n\n```php\n$query = new LeanMapperQuery\\Query;\n$query-\u003ewhere('@author.name', 'Karel');\n```\n\nNow, if we want to get all books whose author's name is Karel, we have to do this:\n\n```php\n$bookRepository = new BookRepository(...);\n$books = $bookRepository-\u003efind($query);\n```\n\nThe database query will look like this:\n\n```sql\nSELECT [book].*\nFROM [book]\nLEFT JOIN [author] ON [book].[author_id] = [author].[id]\nWHERE ([author].[name] = 'Karel')\n```\n\nYou can see it performs automatic joins via the *dot notation*. It supports all relationship\ntypes known to **Lean Mapper**.\n\nIt is very easy to use SQL functions. We can update query like this:\n\n```php\n$query-\u003ewhere('DATE(@pubdate) \u003e %d', '1998-01-01');\n$books = $bookRepository-\u003efind($query);\n```\n\nwhich changes the database query into the following:\n\n```sql\nSELECT [book].*\nFROM [book]\nLEFT JOIN [author] ON [book].[author_id] = [author].[id]\nWHERE ([author].[name] = 'Karel') AND (DATE([book].[pubdate]) \u003e '1998-01-01')\n```\n\nDon't repeat yourself\n---------------------\n\nYou can extend the `Query` class and define your own methods.\n\n```php\nclass BookQuery extends LeanMapperQuery\\Query\n{\n\tpublic function restrictAvailable()\n\t{\n\t\t$this-\u003ewhere('@available', true)\n\t\t\t-\u003eorderBy('@author.name');\n\t\treturn $this;\n\t}\n}\n\n/////////\n\n$query = new BookQuery;\n$query-\u003erestrictAvailable();\n$books = $this-\u003ebookRepository-\u003efind($query);\n```\n\nQuerying entities\n-----------------\n\nIt is also possible to query an entity property (*currently only those properties with\n`BelongsToMany` or `HasMany` relationships*). Let's make the `BaseEntity` class:\n\n```php\nclass BaseEntity extends LeanMapperQuery\\Entity\n{\n\tprotected static $magicMethodsPrefixes = ['find'];\n\n\tprotected function find($field, Query $query)\n\t{\n\t\t$entities = $this-\u003equeryProperty($field, $query);\n\t\treturn $this-\u003eentityFactory-\u003ecreateCollection($entities);\n\t}\n}\n\n/*\n * ...\n */\nclass Book extends BaseEntity\n{\n}\n```\n\n*Note that `BaseEntity` must extend `LeanMapperQuery\\Entity` to make the following possible.*\n\nWe have defined the `find` method as `protected` because by specifying the method name in the\n`$magicMethodsPrefixes` property, you can query entities like this:\n\n```php\n$book; // previously fetched instance of an entity from a repository\n$query = new LeanMapper\\Query;\n$query-\u003ewhere('@name !=', 'ebook');\n$tags = $book-\u003efindTags($query);\n```\n\n*The magic method `findTags` will eventually call your protected method `find` with 'tags' as\nthe 1st argument.*\n\nThe resulting database query looks like this:\n\n```sql\nSELECT [tag].*\nFROM [tag]\nWHERE [tag].[id] IN (1, 2) AND ([tag].[name] != 'ebook')\n```\n\nThe first condition in the `where` clause, `[tag].[id] IN (1, 2)`, is taken from the entity\ntraversing (*tags are queried against this particular book entity's own tags*).\n\n\nWhat else you can do?\n---------------------\n\nIf we slightly modify `BaseRepository` and `BaseEntity`, we can simplify working with query objects.\n*To achieve this look at the [suggested base classes](https://gist.github.com/mibk/9410266)*. It makes\nthe following possible.\n\n```php\n$books = $bookRepository-\u003equery()\n\t-\u003ewhere('@author.name', 'Karel')\n\t-\u003ewhere('DATE(@pubdate) \u003e ?', '1998-01-01')\n\t-\u003efind();\n\n// or...\n\n$tags = $book-\u003equeryTags()\n\t-\u003ewhere('@name !=', 'ebook')\n\t-\u003efind();\n```\n\n\nLicense\n-------\n\nCopyright (c) 2013 Michal Bohuslávek\n\nLicensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmibk%2Fleanmapperquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmibk%2Fleanmapperquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmibk%2Fleanmapperquery/lists"}