cakephp/orm
Composer 安装命令:
composer require cakephp/orm
包简介
CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.
README 文档
README
CakePHP ORM
The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a datamapper pattern the ORM allows you to manipulate data as entities allowing you to create expressive domain layers in your applications.
Database engines supported
The CakePHP ORM is compatible with:
- MySQL 5.1+
- Postgres 8+
- SQLite3
- SQLServer 2008+
- Oracle (through a community plugin)
Connecting to the Database
The first thing you need to do when using this library is register a connection object. Before performing any operations with the connection, you need to specify a driver to use:
use Cake\Datasource\ConnectionManager; ConnectionManager::setConfig('default', [ 'className' => \Cake\Database\Connection::class, 'driver' => \Cake\Database\Driver\Mysql::class, 'database' => 'test', 'username' => 'root', 'password' => 'secret', 'cacheMetadata' => true, 'quoteIdentifiers' => false, ]);
Once a 'default' connection is registered, it will be used by all the Table mappers if no explicit connection is defined.
Using Table Locator
In order to access table instances you need to use a Table Locator.
use Cake\ORM\Locator\TableLocator; $locator = new TableLocator(); $articles = $locator->get('Articles');
You can also use a trait for easy access to the locator instance:
use Cake\ORM\Locator\LocatorAwareTrait; $articles = $this->getTableLocator()->get('Articles');
By default, classes using LocatorAwareTrait will share a global locator instance.
You can inject your own locator instance into the object:
use Cake\ORM\Locator\TableLocator; use Cake\ORM\Locator\LocatorAwareTrait; $locator = new TableLocator(); $this->setTableLocator($locator); $articles = $this->getTableLocator()->get('Articles');
Creating Associations
In your table classes you can define the relations between your tables. CakePHP's ORM supports 4 association types out of the box:
- belongsTo - E.g. Many articles belong to a user.
- hasOne - E.g. A user has one profile.
- hasMany - E.g. A user has many articles.
- belongsToMany - E.g. An article belongsToMany tags.
You define associations in your table's initialize() method. See the
documentation for
complete examples.
Reading Data
Once you've defined some table classes you can read existing data in your tables:
use Cake\ORM\Locator\LocatorAwareTrait; $articles = $this->getTableLocator()->get('Articles'); foreach ($articles->find() as $article) { echo $article->title; }
You can use the query builder to create complex queries, and a variety of methods to access your data.
Saving Data
Table objects provide ways to convert request data into entities, and then persist those entities to the database:
use Cake\ORM\Locator\LocatorAwareTrait; $data = [ 'title' => 'My first article', 'body' => 'It is a great article', 'user_id' => 1, 'tags' => [ '_ids' => [1, 2, 3] ], 'comments' => [ ['comment' => 'Good job'], ['comment' => 'Awesome work'], ] ]; $articles = $this->getTableLocator()->get('Articles'); $article = $articles->newEntity($data, [ 'associated' => ['Tags', 'Comments'] ]); $articles->save($article, [ 'associated' => ['Tags', 'Comments'] ])
The above shows how you can easily marshal and save an entity and its associations in a simple & powerful way. Consult the ORM documentation for more in-depth examples.
Deleting Data
Once you have a reference to an entity, you can use it to delete data:
$articles = $this->getTableLocator()->get('Articles'); $article = $articles->get(2); $articles->delete($article);
Meta Data Cache
It is recommended to enable metadata cache for production systems to avoid performance issues. For e.g. file system strategy your bootstrap file could look like this:
use Cake\Cache\Engine\FileEngine; $cacheConfig = [ 'className' => FileEngine::class, 'duration' => '+1 year', 'serialize' => true, 'prefix' => 'orm_', ]; Cache::setConfig('_cake_model_', $cacheConfig);
Cache configs are optional, so you must require cachephp/cache to add one.
Creating Custom Table and Entity Classes
By default, the Cake ORM uses the \Cake\ORM\Table and \Cake\ORM\Entity classes to
interact with the database. While using the default classes makes sense for
quick scripts and small applications, you will often want to use your own
classes for adding your custom logic.
When using the ORM as a standalone package, you are free to choose where to
store these classes. For example, you could use the Data folder for this:
<?php // in src/Data/Table/ArticlesTable.php namespace Acme\Data\Table; use Acme\Data\Entity\Article; use Acme\Data\Table\UsersTable; use Cake\ORM\Table; class ArticlesTable extends Table { public function initialize() { $this->setEntityClass(Article::class); $this->belongsTo('Users', ['className' => UsersTable::class]); } }
This table class is now setup to connect to the articles table in your
database and return instances of Article when fetching results. In order to
get an instance of this class, as shown before, you can use the TableLocator:
<?php use Acme\Data\Table\ArticlesTable; use Cake\ORM\Locator\TableLocator; $locator = new TableLocator(); $articles = $locator->get('Articles', ['className' => ArticlesTable::class]);
Using Conventions-Based Loading
It may get quite tedious having to specify each time the class name to load. So the Cake ORM can do most of the work for you if you give it some configuration.
The convention is to have all ORM related classes inside the src/Model folder,
that is the Model sub-namespace for your app. So you will usually have the
src/Model/Table and src/Model/Entity folders in your project. But first, we
need to inform Cake of the namespace your application lives in:
<?php use Cake\Core\Configure; Configure::write('App.namespace', 'Acme');
You can also set a longer namaspace up to the place where the Model folder is:
<?php use Cake\Core\Configure; Configure::write('App.namespace', 'My\Log\SubNamespace');
Additional Documentation
Consult the CakePHP ORM documentation for more in-depth documentation.
cakephp/orm 适用场景与选型建议
cakephp/orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 253.7k 次下载、GitHub Stars 达 150, 最近一次更新时间为 2015 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「cakephp」 「data-mapper」 「data-mapper pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cakephp/orm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cakephp/orm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cakephp/orm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
🔥 A Laravel adapter for CycleORM, providing seamless integration of the Cycle DataMapper ORM for advanced database handling and object mapping in PHP applications.
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Thruster DataMapper Component
Thruster Mapper Bundle
统计信息
- 总下载量: 253.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 152
- 点击次数: 35
- 依赖项目数: 78
- 推荐数: 5
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-12