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 | .. |
---|---|---|---|---|---|---|---|---|---|---|
Webseite | doctrine-project.org | git.io/ad | yiiframework.com | redbeanphp.com | ccmbg.com/... | spiral-framework.com/... | ||||
Beschreibung | 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 | Ja schema-tool | Ja https://github.com/atk4/schema | Ja Web and console generator https://github.com/yiisoft/yii2-gii | |||||||
Can be used in Any Framework | Ja composer / packagist | Ja Through composer/packagist | Ja Through Capsule | - Planned in 2.1.x | Ja | Ja composer / packagist | ||||
Automated Cache | Ja Frameworks focuses on optimizing queries rather then caching. Still possible with cross-persistence saving. | Ja | Ja | |||||||
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 | Ja Yes | Ja Yes | Ja | Ja | - But made a custom driver to support it is easy | Ja https://spiral-framework.com/guide/database-databases | ||||
Transparent support for NoSQL | - Through 3rd party drivers. | Nein Eloquent is built on Query Builder. Perhaps moloquent? Also - Through Caching backend, but not as ORM native persistence. | Ja Avialable cross sql and nosql datababase relations (https://github.com/yiisoft/yii2-mongodb) | Nein Build around plain queries. | Nein | Nein No, through ODM (https://spiral-framework.com/guide/odm-databases) | ||||
How to invoke db-vendor-specific extensions? | Ja DBAL | Ja For SQL - save/load/delete operations are expressed in DSQL Queries that can be manipulated: http://git.io/dsql | Ja Through Query Builder / RAW queries. | Ja SQL - Query Builder. | - Can add SQL code chunks. | Ja Ting use native query, you can use everyting of your SGBD | Ja Query models: https://spiral-framework.com/guide/orm-query | |||
Array as a persistence | Ja CRUD operations supported | Ja https://github.com/yii2tech/filedb | Nein | Nein | ||||||
RestAPI as a persistence | - Through 3rd party extension. | - To extend BaseActiveRecord | Nein | Nein | ||||||
JSON string as a persistence | Ja Supported with few extra lines of code. | - To extend BaseActiveRecord | Nein | Nein | ||||||
Same model, multiple persistences | Ja Same class. Different object. | - (no docs) | ||||||||
Basic Single-record operations (C,R,U,D) | ||||||||||
Table Name Mapping | Ja Yes | Ja Yes | Ja Same as model class name by default | Ja Using static method of active record. http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#declaring-ar-classes | Nein mapping no longer supported. | Ja Yes | ||||
Field Name Mapping | Ja Yes, (Annotations in PHP) | Ja Yes (Field::actual property) | - Through accessors/3rd party extension | Ja | Nein mapping no longer supported. | Ja Yes | ||||
Map Entity to SubQuery | Nein Must be physical table | Ja Yes. Extension exist for UNION-models. https://github.com/atk4/report | Nein Must be physical table. | - table = new Expression(..) | Nein | Nein This concept is no-sense for Ting | ||||
Map property to expression | Nein Must be physical field | Ja $model->addExpression() | Nein Must be physical table | - (couldn't find docs) | Nein | Nein This concept is no-sense for Ting | ||||
Map native types (DateTime) | Ja $field->type = 'date' | Ja Accessors and Mutators | Ja Active record behaviors | Ja | Ja Yes | - No support for DateTime. https://spiral-framework.com/guide/orm-accessors | ||||
User-defined types (e.g. "23 USD") | Ja Type class | Ja Yes. Independent from Model. by extending Field class. | Ja Accessors and Mutators | Ja Active record behaviors | Ja Yes, with custom serializer | |||||
Map field of related Entity ('currency' = currency_id->Model Currency.name) | - Specific record only | Ja Transparently maps into sub-select. | - Relationship, but only single record. | Ja $model->currency->name | Nein | Ja Yes | ||||
Map field to sub-query on related entity (Client.balance = Client->orders->sum(total)) | Nein | Ja Yes. hasMany()->addField(..aggregate) | Nein | - (couldn't find docs) | Nein | - This concept is no-sense for Ting | ||||
Entity to join multiple tables (adding new record populates both tables) | Ja Associations | Ja $model->join('other_table') | Nein | Nein No native support. | Nein | Ja Yes | ||||
Criteria, Scope, Conditions (Model will refuse to work with records that does not match criteria) | ||||||||||
Support model-level criteria | Ja Yes. Applied on loading / saving / update. | - Global Scope (querying only) | Nein | Nein | ||||||
Soft-delete | Ja Extension | Ja Through criteria. Example given in the docs. | Ja Trait and Scope | Ja Events and behaviors | Nein | |||||
Domain Model Criteria | Ja $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. | Nein Only for specific query: User::find()->byEmail('...')->active()->one() | Nein | ||||||
Expression-based condition | Ja $m->addCondition($m->expr('..')); | Ja through Query Builder | Nein Only for query: User::find()->andWhere('...')->all() | Nein | ||||||
Criteria Compliance (new record must match condition) | Ja Transaction rollback and exception. Automated-rloading. | Nein | Nein | |||||||
Dynamic criteria | Ja $user->addCondition('is_expired', false) | Nein Only Querying: User::find()->where(['a' => '1', 'b' => true])->all(); | Nein | |||||||
Query Building (Convert Model into Query object for further SQL tweaking) | ||||||||||
Convert Model into Query Object | Ja $q = $model->action('select'); | Ja Model is a Query object. See: Model::where() | Ja \yii\db\ActiveRecord::find() | Nein | Ja RecordSelector | |||||
Raw expression | Ja new Expression('any sql') | Ja DB::raw() | Ja new \yii\db\Expression('...') | Ja | Ja | |||||
Nested/Composite Queries/Expressions | Ja Expression can be in any part of query | Nein | Ja Expression can be in any part of query | Nein | ||||||
Reference domain-model field in query | Ja through Expressionable interface | Nein No fields. Only properties. | Nein Field is class property. | Nein | Nein | |||||
field, where, order, limit | Ja Expressions and Parametric values | Ja | Ja \yii\db\ActiveQuery | Nein | Ja | |||||
options, rollup, partition | - Through custom templates, or expressions. | - not sure. | ||||||||
UPDATE, DELETE, INSERT query building | Ja | Ja | Nein | Ja | ||||||
REPLACE, TRUNCATE | Ja | - 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 | Ja Addon: http://github.com/atk4/schema | Ja | Nein | |||||||
User-defined query template | Ja Templates for all queries | Ja (please link doc) | Nein | |||||||
full support for OR conditions | Ja Through Object Composition and Expressions | - Partial | - | Nein only raw query | ||||||
Use Domain logic in multi-record update | - Supported, but must map fields/values manually. | Nein | Nein | Nein | ||||||
Data Fetching (different ways to retrieve data from database) | ||||||||||
Get All Data (2x array) | Ja | Ja | Ja \yii\db\Query::all() | Ja exportAll() | Ja | |||||
Associative Array (id=>value) | Nein Planned | Ja lists() | ||||||||
Select only required fields | Ja | |||||||||
Typecasting (e.g. DateTime) | Ja | Ja | ||||||||
Import (multiple records) | Ja | Ja | ||||||||
Respect domain model mapping (and criteria) | Ja | |||||||||
Single row | Ja loadAny | Ja | Ja \yii\db\Query::one() | Ja | ||||||
Single Value | Ja Through action | Ja | Ja \yii\db\Query::scalar() | Ja | ||||||
Single Column | - may need to map resulting array | Ja | Ja \yii\db\Query::column() | Ja | Ja | |||||
Iterator | Ja | Ja | Ja | |||||||
Bypass persistence mapping | Ja action('select')->getAll() | Ja ?? (e..g prevent date from being transformed into Carbon) | ||||||||
Loading and Saving individual records | ||||||||||
Load by ID | Ja load(id) | Ja User::findOne(1) | Ja | Ja get(id) | Ja findByPK | |||||
Load by other field | Ja loadBy('field', $value) | Ja User::findOne(['id' => 1]) | Nein | Ja getBy(['propertyName' => 'value']); | ||||||
Read-only Models | Ja Yes | Nein | ||||||||
Model without primary key | Ja Yes, but only in Read-only Mode | Nein | Ja | |||||||
Specify fields to load | Ja onlyFields() | Nein Fields load with SELECT * | Ja User::find()->select([])->one() | Nein | Ja | |||||
Save only sends dirty fields | Ja | Ja | Ja | - (there is Dirty support, so I assuming it is supported) | Ja | Ja | ||||
Guarded / Fillable properties | Ja Conditions and OnlyFields do same thing but more reliably. | Ja Prevents "create($_GET)" from messing up important fields. | ||||||||
Object Hierarchy Model | ||||||||||
Admin extends User, adds criteria (is_admin=1) | Ja Yes, recommended pattern. "User" class adds condition. | Nein | Nein Can be done as wrapper: User::findAdmin(..) or custom CDbCriteria | Nein | ||||||
Admin extends User, adds accessible fields | Ja Yes, recommended pattern. | Nein | Nein | Nein | ||||||
Extend model, add Join (Disjoined SubTypes) | Ja Yes. Unlimited number of joins. | Nein | Nein | Nein | ||||||
Extend model change table | Ja Yes. Used to tweak 3rd party models. | Nein | ||||||||
Relations/References (One model can relate to another model. NOT A TABLE JOIN) | ||||||||||
Model can define relation to other Model | Ja References are implemented as Objects | Ja Relations | Ja Relations | Ja | ||||||
One-to-Many | Ja $user->hasMany('Orders', new Order()); | Ja | Ja | |||||||
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 | Nein Anti-pattern | Ja | Ja | Ja | ||||||
One-to-One | Ja $order->hasOne('user_id', new User()); | Ja | Ja | |||||||
Many-to-Many | Ja $user->addCondition('vip', true)->ref('Orders'); | Ja relations() method of an entity can define. | ||||||||
Deep Many-to-Many traversal. | Nein | Ja $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 | Ja $user->hasMany('Log', new Log($mongodb)); | - No docs. | Nein | |||||||
Cross-persistence joins, aggregation, reporting | ||||||||||
Multi-persistence join | Nein | Nein | Nein | Nein | ||||||
UNION existing models | Nein | Ja Yes, in add-on https://github.com/atk4/report/. Can join UNION result with more tables. | Nein | Nein | ||||||
Domain-model Aggregation | Ja Group by multiple fields. Apply aggregation functions. Preserve types. | Nein | ||||||||
Model to Model join | Nein Planned. | Nein | ||||||||
Multi-persistence UNION | Nein | Nein | Nein | Nein | ||||||
Behaviours / Hooks | ||||||||||
Before/After operation | Ja | Ja | ||||||||
Override standard C,R,U,D operations | Ja | Ja | ||||||||
Persistence-specific hooks (to modify Query) | Ja | - (no docs) | ||||||||
User-defined hooks | Ja | Ja | ||||||||
Auditing | ||||||||||
Global Auditing | Ja http://www.agiletoolkit.org/data/extensions/audit | Nein | ||||||||
Store old/new field values | Ja Yes in JSON and user-readable string. | Ja | ||||||||
Revert action (undo) and replay (redo) | Ja | Nein | Nein | |||||||
Reactive actions | Ja Each action will be automatically linked with sub-actions which may be invoked through Hooks. | |||||||||
Store log in any persistence | Ja | Nein | ||||||||
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 | Ja | Ja | ||||||||
Access to record-specific history | Ja $item->ref('AuditLog'); | - Through a custom query. | ||||||||
Date of creation, modification | - No but easy to add through hook. | Ja \yii\behaviors\TimestampBehavior | Ja TimestampTrait | |||||||
User/IP creation, modification | - Easy to add through a hook. | Ja \yii\behaviors\BlameableBehavior | ||||||||
Store incremental revisions | - Easy to add through a hook | Ja | ||||||||
Integration | ||||||||||
Field meta-information provided | Type, caption, possible values, mapped field, join table spec, encryption, serialization, UI-specific settings, validation rules. | |||||||||
UI extensions | Nein | Ja CRUD, Form, Table, Grid, Entity-manager. (see https://github.com/atk4/ad) | Nein | Nein | Nein | |||||
API extensions | Ja RestFULL API, automated CRUD operations + RPC https://github.com/atk4/api | |||||||||
Ich mag das | 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)