Download the PHP package purekid/mongodm without Composer
On this page you can find all versions of the php package purekid/mongodm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mongodm
=======
- Introduction
- Features
- Requirements
- Installation
- Setup Database
- Basic Usage - CRUD
- Relationship - Reference
- Relationship - Embed
- Collection
- Inheritance
- Other methods
- Model Hooks
- Special Thanks
Introduction
Mongodm is a MongoDB ORM that includes support for references,embed and even multilevel inheritance.
Features
- ORM
- Simple and flexible
- Support for embed
- Support for references (lazy loaded)
- Support for multilevel inheritance
- Support for local collection operations
Requirements
- PHP 5.3 or greater (Tested for 5.5,5.6,7.0,7.1)
- Mongodb 1.3 or greater
- PHP Mongo extension
Installation
1. Setup in composer.json:
2. Install by composer:
$ php composer.phar update
Setup Database
Database config file (By default it locates at /vendor/purekid/mongodm/config.php)
Authentication
Authentication information is passed in via the options array. If you do not specifiy authSource, then the PHP Mongo Driver will choose the "admin" database.
Setup database in application
1.You can set up configuration using the MongoDB::setConfigBlock
method.
2.Or you can duplicate a config file into your project, then define a global constanct 'MONGODM_CONFIG' with it's location.
Choose config section with APPLICATION_ENV
Which config section Mongodm use ? Mongodm choose 'default' section by default.
You have two ways to specify section :
1.'$config' attribute in Model , you can find this attribute in example below.
2.With environment constanct 'APPLICATION_ENV' ,this constanct can be set by webserver,your code or shell environment. In this case,you should set $config='default' or don't declare $config in your own model class.
Create a model and enjoy it
Types supported for model attributes
If you put a object instance into a Model attribute and this attribute is undefined in $attrs of Model class,the data of attribute will be omitted when Model saving.
Model CRUD
Create
Create with initial value
Create using set method
Set and get values
You can set/get values via variable $user->name = "John"
or by method $user->getName()
.
Set using variable or method
Get using variable or method
Update
Update attributes by array
Unset attributes
Retrieve single record
retrieve one record by MongoId
Retrieve records
Retrieve records that name is 'Michael' and acount of owned books equals 2
Retrieve all records
Count records
Delete record
Relationship - Reference
Lazyload a 1:1 relationship record
Lazyload 1:many relationship records
Relationship - Embed
Single Embed
Embeds
Collection
$users is instance of Collection
Save
Delete
Count
Iteration
Sort
Slice and Take
Map
Filter
Determine a record exists in the collection by object instance
Determine a record exists in the collection by numeric index
Determine a record exists in the collection by MongoID
Get a record by numeric index
Get a record by MongoID
Remove a record by numeric index
Remove a record by MongoID
Add a single record to collection
Add records to collection
Merge two collection
Export data to a array
Inheritance
Define multilevel inheritable models:
Use:
Retrieve and check value:
Retrieve subclass
Retrieve all Human records , queries without '_type' because of it's a toplevel class.
Retrieve all Student records , queries with { "_type":"Student" } because of it's a subclass.
Retrieve subclass without _type
To retrieve a record without the _type
criteria (i.e. { "_type":"Student" }
) set:
_Make sure to set a collection otherwise you will get results with every _type
._
Other static methods in Model
Model Hooks
The following hooks are available:
__init()
Executed after the constructor has finished
__preInsert()
Executed before saving a new record
__postInsert()
Executed after saving a new record
__preUpdate()
Executed before saving an existing record
__postUpdate()
Executed after saving an existing record
__preSave()
Executed before saving a record
__postSave()
Executed after saving a record
__preDelete()
Executed before deleting a record
__postDelete()
Executed after deleting a record