PHP Data Access Libraries (ORM, ActiveRecord, Persistence)
Likes 2
For description on the comparison criteria, please read full
article here:
medium.com/...
Link | Doctrine | Agile Data | Eloquent | Yii ORM | Cake ORM | Fuel ORM | Red Bean PHP | Ting | Spiral ORM | .. |
---|---|---|---|---|---|---|---|---|---|---|
Website | doctrine-project.org | git.io/ad | yiiframework.com | redbeanphp.com | ccmbg.com/... | spiral-framework.com/... | ||||
Description | Data Access Framework for high-latency databases (Cloud SQL/NoSQL). | RedBeanPHP is an easy to use ORM for PHP. It's a Zero Config ORM lib that 'automagically' builds your database schema. | Simple: no magic inside : it’s basically SQL queries | ORM engine with automatic database scaffolding, strict schemas, code discovery, modular database partitions and various relation loaders. | ||||||
Github / Bitbucket / Sourceforge | github.com/... | git.io/ad | github.com/... | github.com/... | bitbucket.org/... | github.com/... | ||||
License | MIT | MIT | BSD-3 | New BSD / GPL | Apache 2 | MIT | ||||
Latest version | 2.5.5 | 1.2.x | 2.0.10 | 4.3.2 | 3.4.2 | 1.0.8 | ||||
How schema is defined? | PHP, XML, YAML | in PHP. Method init() in Model class defines fields, relations, conditions, etc. | Model class properties | Active record magic properties. | Automatically discovered on the fly. | PHP | PHP Const array. Column class. | |||
Code generation | Yes schema-tool | Yes https://github.com/atk4/schema | Yes Web and console generator https://github.com/yiisoft/yii2-gii | |||||||
Can be used in Any Framework | Yes composer / packagist | Yes Through composer/packagist | Yes Through Capsule | - Planned in 2.1.x | Yes | Yes composer / packagist | ||||
Automated Cache | Yes Frameworks focuses on optimizing queries rather then caching. Still possible with cross-persistence saving. | Yes | Yes | |||||||
Anti-patterns (purposely included) | Model close-coupled with Persistence and Field classes. | Eloquent is a God Class; Global functions; DB Conection is global; | ||||||||
Audience | ||||||||||
Dependencies | Medium: ±5 packages. | Minimal: 2 packages: DSQL Query Builder (git.io/... | Minimal: 3, pimple, doctrine/cache, aura/sqlquery | |||||||
Simplicity | no rating | 5.0/5 2 ratings | no rating | 5.0/5 2 ratings | 5.0/5 1 rating | no rating | no rating | 5.0/5 1 rating | no rating | no rating |
Enterprise Compliance | 5.0/5 1 rating | 5.0/5 2 ratings | 3.0/5 1 rating | 4.5/5 2 ratings | 5.0/5 1 rating | no rating | no rating | 5.0/5 1 rating | no rating | no rating |
Minimum PHP Version | 5.4 | 5.6 | 5.4 | 5.5 | ||||||
Basic Features | ||||||||||
PDO Support | Yes Yes | Yes Yes | Yes | Yes | - But made a custom driver to support it is easy | Yes https://spiral-framework.com/guide/database-databases | ||||
Transparent support for NoSQL | - Through 3rd party drivers. | No Eloquent is built on Query Builder. Perhaps moloquent? Also - Through Caching backend, but not as ORM native persistence. | Yes Avialable cross sql and nosql datababase relations (https://github.com/yiisoft/yii2-mongodb) | No Build around plain queries. | No | No No, through ODM (https://spiral-framework.com/guide/odm-databases) | ||||
How to invoke db-vendor-specific extensions? | Yes DBAL | Yes For SQL - save/load/delete operations are expressed in DSQL Queries that can be manipulated: http://git.io/dsql | Yes Through Query Builder / RAW queries. | Yes SQL - Query Builder. | - Can add SQL code chunks. | Yes Ting use native query, you can use everyting of your SGBD | Yes Query models: https://spiral-framework.com/guide/orm-query | |||
Array as a persistence | Yes CRUD operations supported | Yes https://github.com/yii2tech/filedb | No | No | ||||||
RestAPI as a persistence | - Through 3rd party extension. | - To extend BaseActiveRecord | No | No | ||||||
JSON string as a persistence | Yes Supported with few extra lines of code. | - To extend BaseActiveRecord | No | No | ||||||
Same model, multiple persistences | Yes Same class. Different object. | - (no docs) | ||||||||
Basic Single-record operations (C,R,U,D) | ||||||||||
Table Name Mapping | Yes Yes | Yes Yes | Yes Same as model class name by default | Yes Using static method of active record. http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#declaring-ar-classes | No mapping no longer supported. | Yes Yes | ||||
Field Name Mapping | Yes Yes, (Annotations in PHP) | Yes Yes (Field::actual property) | - Through accessors/3rd party extension | Yes | No mapping no longer supported. | Yes Yes | ||||
Map Entity to SubQuery | No Must be physical table | Yes Yes. Extension exist for UNION-models. https://github.com/atk4/report | No Must be physical table. | - table = new Expression(..) | No | No This concept is no-sense for Ting | ||||
Map property to expression | No Must be physical field | Yes $model->addExpression() | No Must be physical table | - (couldn't find docs) | No | No This concept is no-sense for Ting | ||||
Map native types (DateTime) | Yes $field->type = 'date' | Yes Accessors and Mutators | Yes Active record behaviors | Yes | Yes Yes | - No support for DateTime. https://spiral-framework.com/guide/orm-accessors | ||||
User-defined types (e.g. "23 USD") | Yes Type class | Yes Yes. Independent from Model. by extending Field class. | Yes Accessors and Mutators | Yes Active record behaviors | Yes Yes, with custom serializer | |||||
Map field of related Entity ('currency' = currency_id->Model Currency.name) | - Specific record only | Yes Transparently maps into sub-select. | - Relationship, but only single record. | Yes $model->currency->name | No | Yes Yes | ||||
Map field to sub-query on related entity (Client.balance = Client->orders->sum(total)) | No | Yes Yes. hasMany()->addField(..aggregate) | No | - (couldn't find docs) | No | - This concept is no-sense for Ting | ||||
Entity to join multiple tables (adding new record populates both tables) | Yes Associations | Yes $model->join('other_table') | No | No No native support. | No | Yes Yes | ||||
Criteria, Scope, Conditions (Model will refuse to work with records that does not match criteria) | ||||||||||
Support model-level criteria | Yes Yes. Applied on loading / saving / update. | - Global Scope (querying only) | No | No | ||||||
Soft-delete | Yes Extension | Yes Through criteria. Example given in the docs. | Yes Trait and Scope | Yes Events and behaviors | No | |||||
Domain Model Criteria | Yes $m->addCondition('gender','M'); where 'gender' can be arbitrary expression, sub-select, field from association(join) or mapped no another physical field. | - Scopes, but not in domain model. | No Only for specific query: User::find()->byEmail('...')->active()->one() | No | ||||||
Expression-based condition | Yes $m->addCondition($m->expr('..')); | Yes through Query Builder | No Only for query: User::find()->andWhere('...')->all() | No | ||||||
Criteria Compliance (new record must match condition) | Yes Transaction rollback and exception. Automated-rloading. | No | No | |||||||
Dynamic criteria | Yes $user->addCondition('is_expired', false) | No Only Querying: User::find()->where(['a' => '1', 'b' => true])->all(); | No | |||||||
Query Building (Convert Model into Query object for further SQL tweaking) | ||||||||||
Convert Model into Query Object | Yes $q = $model->action('select'); | Yes Model is a Query object. See: Model::where() | Yes \yii\db\ActiveRecord::find() | No | Yes RecordSelector | |||||
Raw expression | Yes new Expression('any sql') | Yes DB::raw() | Yes new \yii\db\Expression('...') | Yes | Yes | |||||
Nested/Composite Queries/Expressions | Yes Expression can be in any part of query | No | Yes Expression can be in any part of query | No | ||||||
Reference domain-model field in query | Yes through Expressionable interface | No No fields. Only properties. | No Field is class property. | No | No | |||||
field, where, order, limit | Yes Expressions and Parametric values | Yes | Yes \yii\db\ActiveQuery | No | Yes | |||||
options, rollup, partition | - Through custom templates, or expressions. | - not sure. | ||||||||
UPDATE, DELETE, INSERT query building | Yes | Yes | No | Yes | ||||||
REPLACE, TRUNCATE | Yes | - Throught raw sql in \yii\db\Command | - wipe(), | |||||||
SHOW, DESCRIBE, CALL | - Supported through Expressions or Custom query template | - Throught raw sql in \yii\db\Command | - Automatic schema alter / discovery. | |||||||
ALTER, CREATE query building | Yes Addon: http://github.com/atk4/schema | Yes | No | |||||||
User-defined query template | Yes Templates for all queries | Yes (please link doc) | No | |||||||
full support for OR conditions | Yes Through Object Composition and Expressions | - Partial | - | No only raw query | ||||||
Use Domain logic in multi-record update | - Supported, but must map fields/values manually. | No | No | No | ||||||
Data Fetching (different ways to retrieve data from database) | ||||||||||
Get All Data (2x array) | Yes | Yes | Yes \yii\db\Query::all() | Yes exportAll() | Yes | |||||
Associative Array (id=>value) | No Planned | Yes lists() | ||||||||
Select only required fields | Yes | |||||||||
Typecasting (e.g. DateTime) | Yes | Yes | ||||||||
Import (multiple records) | Yes | Yes | ||||||||
Respect domain model mapping (and criteria) | Yes | |||||||||
Single row | Yes loadAny | Yes | Yes \yii\db\Query::one() | Yes | ||||||
Single Value | Yes Through action | Yes | Yes \yii\db\Query::scalar() | Yes | ||||||
Single Column | - may need to map resulting array | Yes | Yes \yii\db\Query::column() | Yes | Yes | |||||
Iterator | Yes | Yes | Yes | |||||||
Bypass persistence mapping | Yes action('select')->getAll() | Yes ?? (e..g prevent date from being transformed into Carbon) | ||||||||
Loading and Saving individual records | ||||||||||
Load by ID | Yes load(id) | Yes User::findOne(1) | Yes | Yes get(id) | Yes findByPK | |||||
Load by other field | Yes loadBy('field', $value) | Yes User::findOne(['id' => 1]) | No | Yes getBy(['propertyName' => 'value']); | ||||||
Read-only Models | Yes Yes | No | ||||||||
Model without primary key | Yes Yes, but only in Read-only Mode | No | Yes | |||||||
Specify fields to load | Yes onlyFields() | No Fields load with SELECT * | Yes User::find()->select([])->one() | No | Yes | |||||
Save only sends dirty fields | Yes | Yes | Yes | - (there is Dirty support, so I assuming it is supported) | Yes | Yes | ||||
Guarded / Fillable properties | Yes Conditions and OnlyFields do same thing but more reliably. | Yes Prevents "create($_GET)" from messing up important fields. | ||||||||
Object Hierarchy Model | ||||||||||
Admin extends User, adds criteria (is_admin=1) | Yes Yes, recommended pattern. "User" class adds condition. | No | No Can be done as wrapper: User::findAdmin(..) or custom CDbCriteria | No | ||||||
Admin extends User, adds accessible fields | Yes Yes, recommended pattern. | No | No | No | ||||||
Extend model, add Join (Disjoined SubTypes) | Yes Yes. Unlimited number of joins. | No | No | No | ||||||
Extend model change table | Yes Yes. Used to tweak 3rd party models. | No | ||||||||
Relations/References (One model can relate to another model. NOT A TABLE JOIN) | ||||||||||
Model can define relation to other Model | Yes References are implemented as Objects | Yes Relations | Yes Relations | Yes | ||||||
One-to-Many | Yes $user->hasMany('Orders', new Order()); | Yes | Yes | |||||||
One-to-many traversal strategy | Returns destination model instance with dynamically applied criteria (parent_id=123). No queries are executed. Inserting record into this model will ensure association with parent model. | Returns array of objects that may either be pre-loaded with record data or only contain IDs (lazy-loaded). | Uses classical eager/lazy loading approach. with() can help pre-fetch the data for related entities. | pre-fetch and store objects in an array. | ||||||
Lazy/Eager-loading | No Anti-pattern | Yes | Yes | Yes | ||||||
One-to-One | Yes $order->hasOne('user_id', new User()); | Yes | Yes | |||||||
Many-to-Many | Yes $user->addCondition('vip', true)->ref('Orders'); | Yes relations() method of an entity can define. | ||||||||
Deep Many-to-Many traversal. | No | Yes $user->ref('Orders')->ref('Payments')->action('count'); No intermediate queries. | - only with "to-one" references: $book->author->address->street; Executes multiple queries. | - only with to-one references. Executes multiple queries. | ||||||
Cross-Persistence traversal | Yes $user->hasMany('Log', new Log($mongodb)); | - No docs. | No | |||||||
Cross-persistence joins, aggregation, reporting | ||||||||||
Multi-persistence join | No | No | No | No | ||||||
UNION existing models | No | Yes Yes, in add-on https://github.com/atk4/report/. Can join UNION result with more tables. | No | No | ||||||
Domain-model Aggregation | Yes Group by multiple fields. Apply aggregation functions. Preserve types. | No | ||||||||
Model to Model join | No Planned. | No | ||||||||
Multi-persistence UNION | No | No | No | No | ||||||
Behaviours / Hooks | ||||||||||
Before/After operation | Yes | Yes | ||||||||
Override standard C,R,U,D operations | Yes | Yes | ||||||||
Persistence-specific hooks (to modify Query) | Yes | - (no docs) | ||||||||
User-defined hooks | Yes | Yes | ||||||||
Auditing | ||||||||||
Global Auditing | Yes http://www.agiletoolkit.org/data/extensions/audit | No | ||||||||
Store old/new field values | Yes Yes in JSON and user-readable string. | Yes | ||||||||
Revert action (undo) and replay (redo) | Yes | No | No | |||||||
Reactive actions | Yes Each action will be automatically linked with sub-actions which may be invoked through Hooks. | |||||||||
Store log in any persistence | Yes | No | ||||||||
Override, Add fields to audit log | Record dirty fields before persisting. Type-cast into JSON string and store in a single field of AuditLog Model. This model can be persisted anywhere, table, file, etc. | |||||||||
Custom events | Yes | Yes | ||||||||
Access to record-specific history | Yes $item->ref('AuditLog'); | - Through a custom query. | ||||||||
Date of creation, modification | - No but easy to add through hook. | Yes \yii\behaviors\TimestampBehavior | Yes TimestampTrait | |||||||
User/IP creation, modification | - Easy to add through a hook. | Yes \yii\behaviors\BlameableBehavior | ||||||||
Store incremental revisions | - Easy to add through a hook | Yes | ||||||||
Integration | ||||||||||
Field meta-information provided | Type, caption, possible values, mapped field, join table spec, encryption, serialization, UI-specific settings, validation rules. | |||||||||
UI extensions | No | Yes CRUD, Form, Table, Grid, Entity-manager. (see https://github.com/atk4/ad) | No | No | No | |||||
API extensions | Yes RestFULL API, automated CRUD operations + RPC https://github.com/atk4/api | |||||||||
Like | Likes 0 | Likes 3 | Likes 0 | Likes 1 | Likes 1 | Likes 0 | Likes 0 | Likes 0 | Likes 0 | Likes 0 |
User reviews and comments
Probably the easiest at using without up front configuration
Easiest ORM, I think!
Using in Recent modules of our ERP
Most simple among what I used (almost all popular ones)