Download the PHP package denchikby/phalcon-mongodb-odm without Composer
On this page you can find all versions of the php package denchikby/phalcon-mongodb-odm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package phalcon-mongodb-odm
Phalcon MongoDB ODM
Tiny, simple and functional MongoDB ODM library for Phalcon framework for new mongodb php extension
Installation
Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php
Install the latest stable version using composer:
or
Configuration
Add settings and service to DI:
Creating Model
in this case will be used 'user' collection with same name as model.
To specify another collection name use getSource method:
Initialize Model
To initialize a new model instead of
use
Initialize filled model:
or
or init and save in db
Methods
To array:
Unset field:
Attributes Casting
It allow you to modify attribute type on setting/filling:
It help to save fields to db with need type, what is very important, cause mongo don't casting types in queries.
Supported types: integer, float, boolean, string, array, object, id
Casts also work in where methods of builder:
age will converted to integer and query will load normally.
Relations
There are two types of relations: one and many;
Definition:
field => [related model, type, local field, foreign field]
Relations can be loaded by two ways:
By one query:
it will use $lookup operator of aggregation framework.
By several queries, just call attribute with name of key in relations array:
Scopes
Scopes help to put common queries to methods:
Global scopes
This scope will binded to any query of model:
Mutators (getters/setters)
Mutators allow modify attributes when you getting, setting or filling it.
For example, when you creating user and set the password, hashing may be defined in model:
Events
Existed events before/after for actions save, create, update, delete.
Query Builder
Query builder could be called clearly or implicitly.
similar
Usage
Advanced Wheres
For grouping where conditions:
Query Result Collection
Every select query will return iterated collection class of models.
It could be iterated with foreach, or used as array $collection[0]->name;
Methods
Size of collection:
Will return array of assocs of each model:
Return json of array, created by toArray method:
Eager loading, similar as join:
Will load all for all comments by single query and put necessary into every document.
Grouping documents to arrays by specific field:
Keys the collection by the given key (unlike groupBy that value will single model, in groupBy array of models):
Return array of values specific field of collection:
Return associated array of specified key => value fields:
Return array of chunked collection by specified size: