{"id":15187593,"url":"https://github.com/chrisandchris/passive-record-orm","last_synced_at":"2025-10-02T02:31:35.970Z","repository":{"id":30275303,"uuid":"33826786","full_name":"chrisandchris/passive-record-orm","owner":"chrisandchris","description":"A database layer, passive record and orm abstraction layer for your php application.","archived":true,"fork":false,"pushed_at":"2018-08-15T07:56:20.000Z","size":542,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-15T11:07:21.859Z","etag":null,"topics":["database","database-abstraction","doctrine-orm","mysql","postgres","postgresql","query","query-builder","query-language","symfony","symfony2"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrisandchris.png","metadata":{"files":{"readme":"README.md","changelog":"changelog/v2.0.2.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-12T17:42:34.000Z","updated_at":"2023-01-28T10:18:53.000Z","dependencies_parsed_at":"2022-08-30T10:42:23.749Z","dependency_job_id":null,"html_url":"https://github.com/chrisandchris/passive-record-orm","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisandchris%2Fpassive-record-orm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisandchris%2Fpassive-record-orm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisandchris%2Fpassive-record-orm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisandchris%2Fpassive-record-orm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisandchris","download_url":"https://codeload.github.com/chrisandchris/passive-record-orm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234929155,"owners_count":18908883,"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","database-abstraction","doctrine-orm","mysql","postgres","postgresql","query","query-builder","query-language","symfony","symfony2"],"created_at":"2024-09-27T18:40:26.195Z","updated_at":"2025-10-02T02:31:35.626Z","avatar_url":"https://github.com/chrisandchris.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A QueryBuilder and passive record ORM for Symfony2\n\n[![Build Status](https://travis-ci.org/chrisandchris/passive-record-orm.svg?branch=master)](https://travis-ci.org/chrisandchris/passive-record-orm)\n[![Code Climate](https://codeclimate.com/github/chrisandchris/passive-record-orm/badges/gpa.svg)](https://codeclimate.com/github/chrisandchris/passive-record-orm)\n[![Test Coverage](https://codeclimate.com/github/chrisandchris/passive-record-orm/badges/coverage.svg)](https://codeclimate.com/github/chrisandchris/passive-record-orm/coverage)\n[![Version](https://img.shields.io/packagist/v/chrisandchris/passive-record-orm.svg)](https://packagist.org/packages/chrisandchris/passive-record-orm)\n[![Downloads](https://img.shields.io/packagist/dt/chrisandchris/passive-record-orm.svg)](https://packagist.org/packages/chrisandchris/passive-record-orm)\n[![Licence](https://img.shields.io/packagist/l/chrisandchris/passive-record-orm.svg)](https://github.com/chrisandchris/passive-record-orm/blob/master/LICENSE)\n\nDespite it's package name, it's not simply a row mapper. And it's not simply for Symfony.\nThis project is a *QueryBuilder* and a *Mapper for SQL Result Sets*,\nboth combined but still very separated, so you can use them independent.\n\n```php\n\u003c?php\n\nuse ChrisAndChris\\Common\\RowMapperBundle\\Services\\Model\\ConcreteModel;\n\nclass DemoRepo {\n    /** @var ConcreteModel  */\n    private $model;\n    \n    public function __construct(ConcreteModel $model){\n        $this-\u003emodel = $model;\n    }\n    \n    public function getCustomerName($customerId) {\n        $query = $this-\u003emodel-\u003egetDependencyProvider()-\u003egetBuilder()-\u003eselect()\n            -\u003efield('customer_name')\n            -\u003etable('customer')\n            -\u003ewhere()\n                -\u003efield('customer_id')-\u003eequals()-\u003evalue($customerId)\n            -\u003eclose()\n            -\u003egetSqlQuery();\n\n        return $this-\u003emodel-\u003erunWithFirstKeyFirstValue($query);\n    }\n}\n```\n\nThis doc gives a short overview of all the possibilities this package provides.\nWe are moving the contents continuously to the `doc/` directory, so look more detailed\ninformation up there.\n\n## Was it does\n* Opens and handles MySQL-Connection\n* Provides a simple interface for building prepared statements and querying the database\n* Provides a simple interface for mapping the results to classes\n\n## Internals\nThe basic internal principal is the following:\n\n* There are Types (simple key-value classes) which represent a part of a statement\n* The query gets parsed using a Parser and the same-named snippets (they contain sql)\n* The query is returned\n\n# How To Use\n## Configuration\nConfigure your symfony2 project as you do always. The bundle uses the database\ninformation stored in the parameters.yml and automatically connects to the given\ndatabase.\n\nActually, there is no further configuration possible.\n\n## A simple query\nLet's create a service definition:\n```yml\nservices:\n    project.demo_repo:\n        class: DemoRepo\n        arguments: ['@common_rowmapper.model']\n```\n\nCreate the repository:\n```php\n\u003c?php\n\nuse ChrisAndChris\\Common\\RowMapperBundle\\Services\\Model\\ConcreteModel;\n\nclass DemoRepo {\n    /** @var ConcreteModel  */\n    private $model;\n    \n    public function __construct(ConcreteModel $model){\n        $this-\u003emodel = $model;\n    }\n    \n    public function getCustomerName($customerId) {\n        $query = $this-\u003emodel-\u003egetDependencyProvider()-\u003egetBuilder()-\u003eselect()\n            -\u003efield('customer_name')\n            -\u003etable('customer')\n            -\u003ewhere()\n                -\u003efield('customer_id')-\u003eequals()-\u003evalue($customerId)\n            -\u003eclose()\n            -\u003egetSqlQuery();\n\n        return $this-\u003emodel-\u003erunWithFirstKeyFirstValue($query);\n    }\n}\n```\n\nIf you want to map a more complicated query to a class, use something like this:\n```php\n\u003c?php\n\nuse ChrisAndChris\\Common\\RowMapperBundle\\Entity\\Entity;\n\nclass CustomerEntity implements Entity {\n    public $customerId;\n    public $name;\n    public $street;\n    public $zip;\n    public $city;\n}\n```\n\nAnd to map, use this method\n```php\n\u003c?php\n\nuse ChrisAndChris\\Common\\RowMapperBundle\\Services\\Model\\ConcreteModel;\n\nclass DemoModel {\n    \n    /** @var ConcreteModel  */\n    private $model;\n    \n    public function __construct(ConcreteModel $model){\n        $this-\u003emodel = $model;\n    }\n    \n    public function getCustomer($customerId) {\n        $query = $this-\u003emodel-\u003egetDependencyProvider()-\u003egetBuilder()-\u003eselect()\n            -\u003efieldlist([\n                'customer_id' =\u003e 'customerId',\n                'cus_name' =\u003e 'name',\n                'street',\n                'zip',\n                'city'\n            ])\n            -\u003etable('customer')\n            -\u003ewhere()\n                -\u003efield('customer_id')-\u003eequals()-\u003evalue($customerId)\n            -\u003eclose()\n            -\u003egetSqlQuery();\n\n        return $this-\u003emodel-\u003erun($query, new SomeEntity());\n    }\n}\n```\n## Some more information\n\n### The field() method\nYou could use an array for separating database, table, field:\n```\nfield(['database', 'table', 'field'])`\n```\n\nIf you fetch single fields, you must append a comma by yourself:\n```php\n-\u003efield('field1')-\u003ec()\n-\u003efield('field2')-\u003ec()\n```\n\nYou could also give a closure as parameter:\n```php\n-\u003efield(function () { return $value; });\n```\n\n### The value() method\nUse this method to append a parameter to the query:\n```php\n-\u003evalue($someValue);\n-\u003evalue(function () { return $someValue; });\n```\n\n### The fieldlist() method\nThis method is even much more powerful, use it as follows:\n\nSimple key-value usage:\n```php\nfieldlist([\n    'field' =\u003e 'alias',\n    'customer_id' =\u003e 'customerId',\n    'cus_name' =\u003e 'name'\n])\n```\n\nSpecify database, table, field:\n```php\nfieldlist([\n    'database:table:field' =\u003e 'alias'\n]);\n```\n\nMix anything\n```php\nfieldlist([\n    'database:table:field' =\u003e 'alias',\n    'field1', // fetched by real field name\n    'field2' =\u003e 'alias1'\n]);\n```\n### The f(), where(), order(), groupBy()\nAny of these four types open so-called \"braces\". A brace represents a kind of\nsub-query which is fully independent from the query before. In its internals, during\nparsing this sub-query, the parser has principally no access to the other statements.\n\nSo, if you finish one of these, simply call close() or end() to close the brace:\n```php\n-\u003ewhere()\n    -\u003efield('field1')-\u003eequals()-\u003evalue(true)\n-\u003eclose()\n```\n\n### The raw()\nBecause of the lack of time and to fulfill any requirement, I simply implemented\na raw method. And gladly, this method is able to use parameters :D\n\n```php\n-\u003eraw('SELECT customer_id FROM customer WHERE customer_name LIKE ?', [\n    '%github%'\n]);\n```\n\n### The in()\nYou can simply build IN-clauses with the two following methods:\n\n```php\n// option a\n-\u003ein([1, 2, 3, 4, 5, 6])\n// option b\n-\u003ein()\n    -\u003eselect()\n    -\u003evalue(1)\n-\u003eclose()\n```\n\nOption A uses prepared statements all-the-way, any value within the array gets\nis way as a parameter to the database.\n\n### Conditional appending\nThere are three methods to provide conditional appending:\n* _if()\n* _else()\n* _end()\n\nYou are allowed to nest ifs, and you are allowed to push a closure as parameter to the if:\n```php\n-\u003e_if($condition === true)\n    -\u003ewhere()\n    -\u003e_if(function() { return $condition === true; })\n        // ...\n    -\u003e_end()\n        // ...\n    -\u003eclose()\n-\u003e_else()\n    //\n-\u003e_end()\n```\n\n### Some other methods\n* f() - for functions\n* where() - build wheres\n* any() - a god-blessed star (evil `SELECT *`)\n* value() - a parameter\n* null() - a sql `NULL`\n* isNull() - compares to null using `IS NULL`\n* join() - join tables\n* using() - using clause for joined tables\n* on() -  on clause for joined tables\n* union() - create `UNION` statements\n* asLong() - creating while loop\n* each() - creating each loop\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisandchris%2Fpassive-record-orm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisandchris%2Fpassive-record-orm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisandchris%2Fpassive-record-orm/lists"}