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