{"id":13623458,"url":"https://github.com/cakephp/orm","last_synced_at":"2025-06-25T23:07:18.474Z","repository":{"id":27241162,"uuid":"30712982","full_name":"cakephp/orm","owner":"cakephp","description":"[READ-ONLY] A flexible, lightweight and powerful Object-Relational Mapper for PHP, implemented using the DataMapper pattern. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp","archived":false,"fork":false,"pushed_at":"2025-06-21T03:38:14.000Z","size":3528,"stargazers_count":147,"open_issues_count":1,"forks_count":18,"subscribers_count":27,"default_branch":"master","last_synced_at":"2025-06-21T04:29:16.379Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cakephp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2015-02-12T16:57:52.000Z","updated_at":"2024-12-16T06:53:13.000Z","dependencies_parsed_at":"2023-01-14T06:17:17.055Z","dependency_job_id":"734644db-93a0-49a8-839e-c6302ed40388","html_url":"https://github.com/cakephp/orm","commit_stats":{"total_commits":2040,"total_committers":142,"mean_commits":"14.366197183098592","dds":0.7877450980392157,"last_synced_commit":"ee70543b6caa6783954d153ce844c597a2b719a4"},"previous_names":[],"tags_count":305,"template":false,"template_full_name":null,"purl":"pkg:github/cakephp/orm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cakephp","download_url":"https://codeload.github.com/cakephp/orm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cakephp%2Form/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261967132,"owners_count":23237663,"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":[],"created_at":"2024-08-01T21:01:32.004Z","updated_at":"2025-06-25T23:07:18.452Z","avatar_url":"https://github.com/cakephp.png","language":"PHP","readme":"[![Total Downloads](https://img.shields.io/packagist/dt/cakephp/orm.svg?style=flat-square)](https://packagist.org/packages/cakephp/orm)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.txt)\n\n# CakePHP ORM\n\nThe CakePHP ORM provides a powerful and flexible way to work with relational\ndatabases. Using a datamapper pattern the ORM allows you to manipulate data as\nentities allowing you to create expressive domain layers in your applications.\n\n## Database engines supported\n\nThe CakePHP ORM is compatible with:\n\n* MySQL 5.1+\n* Postgres 8+\n* SQLite3\n* SQLServer 2008+\n* Oracle (through a [community plugin](https://github.com/CakeDC/cakephp-oracle-driver))\n\n## Connecting to the Database\n\nThe first thing you need to do when using this library is register a connection\nobject.  Before performing any operations with the connection, you need to\nspecify a driver to use:\n\n```php\nuse Cake\\Datasource\\ConnectionManager;\n\nConnectionManager::setConfig('default', [\n\t'className' =\u003e \\Cake\\Database\\Connection::class,\n\t'driver' =\u003e \\Cake\\Database\\Driver\\Mysql::class,\n\t'database' =\u003e 'test',\n\t'username' =\u003e 'root',\n\t'password' =\u003e 'secret',\n\t'cacheMetadata' =\u003e true,\n\t'quoteIdentifiers' =\u003e false,\n]);\n```\n\nOnce a 'default' connection is registered, it will be used by all the Table\nmappers if no explicit connection is defined.\n\n## Using Table Locator\n\nIn order to access table instances you need to use a *Table Locator*.\n\n```php\nuse Cake\\ORM\\Locator\\TableLocator;\n\n$locator = new TableLocator();\n$articles = $locator-\u003eget('Articles');\n```\n\nYou can also use a trait for easy access to the locator instance:\n\n```php\nuse Cake\\ORM\\Locator\\LocatorAwareTrait;\n\n$articles = $this-\u003egetTableLocator()-\u003eget('Articles');\n```\n\nBy default, classes using `LocatorAwareTrait` will share a global locator instance.\nYou can inject your own locator instance into the object:\n\n```php\nuse Cake\\ORM\\Locator\\TableLocator;\nuse Cake\\ORM\\Locator\\LocatorAwareTrait;\n\n$locator = new TableLocator();\n$this-\u003esetTableLocator($locator);\n\n$articles = $this-\u003egetTableLocator()-\u003eget('Articles');\n```\n\n## Creating Associations\n\nIn your table classes you can define the relations between your tables. CakePHP's ORM\nsupports 4 association types out of the box:\n\n* belongsTo - E.g. Many articles belong to a user.\n* hasOne - E.g. A user has one profile.\n* hasMany - E.g. A user has many articles.\n* belongsToMany - E.g. An article belongsToMany tags.\n\nYou define associations in your table's `initialize()` method. See the\n[documentation](https://book.cakephp.org/4/en/orm/associations.html) for\ncomplete examples.\n\n## Reading Data\n\nOnce you've defined some table classes you can read existing data in your tables:\n\n```php\nuse Cake\\ORM\\Locator\\LocatorAwareTrait;\n\n$articles = $this-\u003egetTableLocator()-\u003eget('Articles');\nforeach ($articles-\u003efind() as $article) {\n\techo $article-\u003etitle;\n}\n```\n\nYou can use the [query builder](https://book.cakephp.org/4/en/orm/query-builder.html) to create\ncomplex queries, and a [variety of methods](https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html)\nto access your data.\n\n## Saving Data\n\nTable objects provide ways to convert request data into entities, and then persist\nthose entities to the database:\n\n```php\nuse Cake\\ORM\\Locator\\LocatorAwareTrait;\n\n$data = [\n\t'title' =\u003e 'My first article',\n\t'body' =\u003e 'It is a great article',\n\t'user_id' =\u003e 1,\n\t'tags' =\u003e [\n\t\t'_ids' =\u003e [1, 2, 3]\n\t],\n\t'comments' =\u003e [\n\t\t['comment' =\u003e 'Good job'],\n\t\t['comment' =\u003e 'Awesome work'],\n\t]\n];\n\n$articles = $this-\u003egetTableLocator()-\u003eget('Articles');\n$article = $articles-\u003enewEntity($data, [\n\t'associated' =\u003e ['Tags', 'Comments']\n]);\n$articles-\u003esave($article, [\n\t'associated' =\u003e ['Tags', 'Comments']\n])\n```\n\nThe above shows how you can easily marshal and save an entity and its\nassociations in a simple \u0026 powerful way. Consult the [ORM documentation](https://book.cakephp.org/4/en/orm/saving-data.html)\nfor more in-depth examples.\n\n## Deleting Data\n\nOnce you have a reference to an entity, you can use it to delete data:\n\n```php\n$articles = $this-\u003egetTableLocator()-\u003eget('Articles');\n$article = $articles-\u003eget(2);\n$articles-\u003edelete($article);\n```\n\n## Meta Data Cache\n\nIt is recommended to enable metadata cache for production systems to avoid performance issues.\nFor e.g. file system strategy your bootstrap file could look like this:\n\n```php\nuse Cake\\Cache\\Engine\\FileEngine;\n\n$cacheConfig = [\n   'className' =\u003e FileEngine::class,\n   'duration' =\u003e '+1 year',\n   'serialize' =\u003e true,\n   'prefix'    =\u003e 'orm_',\n];\nCache::setConfig('_cake_model_', $cacheConfig);\n```\n\nCache configs are optional, so you must require ``cachephp/cache`` to add one.\n\n## Creating Custom Table and Entity Classes\n\nBy default, the Cake ORM uses the `\\Cake\\ORM\\Table` and `\\Cake\\ORM\\Entity` classes to\ninteract with the database. While using the default classes makes sense for\nquick scripts and small applications, you will often want to use your own\nclasses for adding your custom logic.\n\nWhen using the ORM as a standalone package, you are free to choose where to\nstore these classes. For example, you could use the `Data` folder for this:\n\n```php\n\u003c?php\n// in src/Data/Table/ArticlesTable.php\nnamespace Acme\\Data\\Table;\n\nuse Acme\\Data\\Entity\\Article;\nuse Acme\\Data\\Table\\UsersTable;\nuse Cake\\ORM\\Table;\n\nclass ArticlesTable extends Table\n{\n    public function initialize()\n    {\n        $this-\u003esetEntityClass(Article::class);\n        $this-\u003ebelongsTo('Users', ['className' =\u003e UsersTable::class]);\n    }\n}\n```\n\nThis table class is now setup to connect to the `articles` table in your\ndatabase and return instances of `Article` when fetching results. In order to\nget an instance of this class, as shown before, you can use the `TableLocator`:\n\n```php\n\u003c?php\nuse Acme\\Data\\Table\\ArticlesTable;\nuse Cake\\ORM\\Locator\\TableLocator;\n\n$locator = new TableLocator();\n$articles = $locator-\u003eget('Articles', ['className' =\u003e ArticlesTable::class]);\n```\n\n### Using Conventions-Based Loading\n\nIt may get quite tedious having to specify each time the class name to load. So\nthe Cake ORM can do most of the work for you if you give it some configuration.\n\nThe convention is to have all ORM related classes inside the `src/Model` folder,\nthat is the `Model` sub-namespace for your app. So you will usually have the\n`src/Model/Table` and `src/Model/Entity` folders in your project. But first, we\nneed to inform Cake of the namespace your application lives in:\n\n```php\n\u003c?php\nuse Cake\\Core\\Configure;\n\nConfigure::write('App.namespace', 'Acme');\n```\n\nYou can also set a longer namaspace up to the place where the `Model` folder is:\n\n```php\n\u003c?php\nuse Cake\\Core\\Configure;\n\nConfigure::write('App.namespace', 'My\\Log\\SubNamespace');\n```\n\n\n## Additional Documentation\n\nConsult [the CakePHP ORM documentation](https://book.cakephp.org/4/en/orm.html)\nfor more in-depth documentation.\n","funding_links":[],"categories":["Table of Contents","目录","PHP","数据库 Database","数据库( Database )"],"sub_categories":["Database","数据库 Database"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcakephp%2Form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcakephp%2Form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcakephp%2Form/lists"}