{"id":15496693,"url":"https://github.com/byjg/php-micro-orm","last_synced_at":"2025-09-11T01:08:10.795Z","repository":{"id":54700672,"uuid":"61670018","full_name":"byjg/php-micro-orm","owner":"byjg","description":"A micro framework for create a very simple decoupled ORM (sqlite, mysql, postgres, sqlserver)","archived":false,"fork":false,"pushed_at":"2025-09-03T17:06:38.000Z","size":466,"stargazers_count":19,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-03T18:32:51.052Z","etag":null,"topics":["byjg","entity","micro-framework","mysql","mysql-orm","orm","sqlite3","sqlserver"],"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/byjg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"github":"byjg"}},"created_at":"2016-06-21T21:59:38.000Z","updated_at":"2025-08-12T11:50:01.000Z","dependencies_parsed_at":"2024-01-05T22:03:30.986Z","dependency_job_id":"0de3356e-b986-4600-a165-21bc69632243","html_url":"https://github.com/byjg/php-micro-orm","commit_stats":{"total_commits":130,"total_committers":2,"mean_commits":65.0,"dds":0.03076923076923077,"last_synced_commit":"2bf90433dc43fbbd46e6acac8e04ec72add7e57e"},"previous_names":["byjg/php-micro-orm","byjg/micro-orm"],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/byjg/php-micro-orm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-micro-orm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-micro-orm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-micro-orm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-micro-orm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/byjg","download_url":"https://codeload.github.com/byjg/php-micro-orm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-micro-orm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274557583,"owners_count":25307516,"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","status":"online","status_checked_at":"2025-09-10T02:00:12.551Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["byjg","entity","micro-framework","mysql","mysql-orm","orm","sqlite3","sqlserver"],"created_at":"2024-10-02T08:27:03.462Z","updated_at":"2025-09-11T01:08:10.780Z","avatar_url":"https://github.com/byjg.png","language":"PHP","funding_links":["https://github.com/sponsors/byjg"],"categories":[],"sub_categories":[],"readme":"# MicroOrm for PHP\n\n[![Build Status](https://github.com/byjg/php-micro-orm/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-micro-orm/actions/workflows/phpunit.yml)\n[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)\n[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-micro-orm/)\n[![GitHub license](https://img.shields.io/github/license/byjg/php-micro-orm.svg)](https://opensource.byjg.com/opensource/licensing.html)\n[![GitHub release](https://img.shields.io/github/release/byjg/php-micro-orm.svg)](https://github.com/byjg/php-micro-orm/releases/)\n\nA micro framework for create a very simple decoupled ORM.\nThis library intended to be very small and very simple to use;\n\nKey Features:\n\n* Can be used with any DTO, Entity, Model or whatever class with public properties or with getter and setter\n* The repository support a variety of datasources: MySql, Sqlite, Postgres, MySQL, Oracle (see byjg/anydataset)\n* A class Mapper is used for mapping the Entity and the repository\n* Small and simple to use\n\n## Architecture\n\nThese are the key components:\n\n```text\n┌──────────────────────────┐\n│ Repository               │              ┌─────────────────────┐\n│                          │         ┌────│        Model        │\n│                          │         │    └─────────────────────┘\n│          ┌───────────────┴─────┐   │               │\n│          │       Mapper        │───┤               │\n│          └───────────────┬─────┘   │               │\n│                     │    │         │    ┌─────────────────────┐\n│                     │    │         └────│    FieldMapping     │\n│                     │    │              └─────────────────────┘\n│                     │    │\n│          ┌───────────────┴─────┐\n│          │        Query        │\n│          └───────────────┬─────┘\n│                          │\n│          ┌───────────────┴─────┐\n│          │  DbDriverInterface  │───────────────┐\n│          └───────────────┬─────┘               │\n│                          │                     │\n└──────────────────────────┘                .─────────.\n                                           │           │\n                                           │`─────────'│\n                                           │           │\n                                           │    DB     │\n                                           │           │\n                                           │           │\n                                            `─────────'\n```\n\n* Model is a get/set class to retrieve or save the data into the database\n* Mapper will create the definitions to map the Model into the Database.\n* Query will use the Mapper to prepare the query to the database based on DbDriverInterface\n* DbDriverIntarce is the implementation to the Database connection.\n* Repository put all this together\n\n\n## Getting Started\n\n### Table Structure\n\nWe have the following table structure in the database for this example:\n\n```sql\nCREATE TABLE `mytable` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `name` varchar(255) DEFAULT NULL,\n  `company_id` int(11) DEFAULT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n```\n\nWe want to be able to interact with this table using the ORM.\n\n### Defining the Model\n\nA Model in our context is a class that symbolizes the data you wish to store or fetch from the database.\nThis Model class can be as simple as a class with public properties. \nAlternatively, it can be a class equipped with getter and setter methods for more controlled access and \nmanipulation of the data. \n\nTo map the database fields, you can add attributes to the Model class. Each property in the Model class represents a field in the database. \n\nLet's look at an example:\n```php\n#[TableAttribute(tableName: 'mytable')]\nclass MyModel\n{\n    #[FieldAttribute(primaryKey: true)]\n    public ?int $id;\n\n    #[FieldAttribute()]\n    public ?string $name;\n\n    #[FieldAttribute(fieldName: 'company_id')\n    public ?int $companyId;\n}\n```\n\nIn this example, we have a class `MyModel` with three properties: `id`, `name`, and `companyId`.\n\nThe `id` property is marked as a primary key. The `name` property is a simple field.\nThe `companyId` property is a field with a different name in the database `company_id`.\n\nThe `TableAttribute` is used to define the table name in the database.\n\n### Connecting the repository\n\nAfter defining the Model, you can connect the Model with the repository.\n\n```php\n$dbDriver = \\ByJG\\AnyDataset\\Db\\Factory::getDbRelationalInstance('mysql://user:password@server/schema');\n\n$repository = new \\ByJG\\MicroOrm\\Repository($dbDriver, MyModel::class);\n```\n\n### Querying the database\n\nYou can query the database using the repository.\n\n```php\n$myModel = $repository-\u003eget(1);\n```\n\nor\n\n```php\n$query = Query::getInstance()\n    -\u003efield('name')\n    -\u003ewhere('company_id = :cid', ['cid' =\u003e 1]);\n\n$result = $repository-\u003egetByQuery($query);\n```\n\nor, the same example above:\n\n```php\n$filterModel = $repository-\u003eentity([\n    'company_id' =\u003e 1\n]);\n\n$query = $repository-\u003equeryInstance($filterModel);\n$query-\u003efield('name');\n\n$result = $repository-\u003egetByQuery($query);\n```\n\n## Basics\n\n* [Defining the Model](docs/getting-started-model.md)\n* [Querying the Database](docs/querying-the-database.md)\n* [Updating the database](docs/updating-the-database.md)\n* [Using the Mapper Object](docs/using-mapper-object.md)\n\n## Advanced Topics\n\n* [Active Record](docs/active-record.md)\n* [Auto Discovering Relationship](docs/auto-discovering-relationship.md)\n* [The Literal Object](docs/the-literal-object.md)\n* [Soft Delete](docs/softdelete.md)\n* [Caching the Results](docs/cache.md)\n* [Observing the Database](docs/observers.md)\n* [Controlling the data queried/updated](docs/controlling-the-data.md)\n* [Using FieldAlias](docs/using-fieldalias.md)\n* [Tables without auto increments fields](docs/tables-without-auto-increment-fields.md)\n* [Using With Recursive SQL Command](docs/using-with-recursive-sql-command.md)\n* [QueryRaw (raw SQL)](docs/query-raw.md)\n\n## Install\n\nJust type:\n\n```bash\ncomposer require \"byjg/micro-orm\"\n```\n\n## Running Tests\n\n```bash\n./vendor/bin/phpunit\n```\n\n## Related Projects\n\n* [Database Migration](https://github.com/byjg/migration)\n* [Anydataset](https://github.com/byjg/anydataset)\n* [PHP Rest Template](https://github.com/byjg/php-rest-template)\n* [USDocker](https://github.com/usdocker/usdocker)\n* [Serializer](https://github.com/byjg/serializer)\n\n## Dependencies\n\n```mermaid\nflowchart TD\n    byjg/micro-orm --\u003e byjg/anydataset-db\n    byjg/micro-orm --\u003e ext-json\n```\n\n----\n[Open source ByJG](http://opensource.byjg.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyjg%2Fphp-micro-orm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyjg%2Fphp-micro-orm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyjg%2Fphp-micro-orm/lists"}