1. Go to this page and download the library: Download wasabi-web/mongodm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
//in a global initialization place
define('MONGODM_CONFIG',__DIR__."/../config/mongodm.php");
class User extends \WasabiWeb\Mongodm\Model
{
static $collection = "user";
/** use specific config section **/
public static $config = 'testing';
/** specific definition for attributes, not necessary! **/
protected static $attrs = array(
// 1 to 1 reference
'book_fav' => array('model'=>'WasabiWeb\Mongodm\Test\Model\Book','type'=>'reference'),
// 1 to many references
'books' => array('model'=>'WasabiWeb\Mongodm\Test\Model\Book','type'=>'references'),
// you can define default value for attribute
'age' => array('default'=>16,'type'=>'integer'),
'money' => array('default'=>20.0,'type'=>'double'),
'hobbies' => array('default'=>array('love'),'type'=>'array'),
'born_time' => array('type'=>'timestamp'),
'family'=>array('type'=>'object'),
'pet_fav' => array('model'=>'WasabiWeb\Mongodm\Test\Model\Pet','type'=>'embed'),
'pets' => array('model'=>'WasabiWeb\Mongodm\Test\Model\Pet','type'=>'embeds'),
);
public function setFirstName($name) {
$name = ucfirst(strtolower($name));
$this->__setter('firstName', $name);
}
public function getLastName($name) {
$name = $this->__getter('name');
return strtoupper($name);
}
}
$types = [
'mixed', // mixed type
'string',
'reference', // 1 : 1 reference
'references', // 1 : many references
'embed',
'embeds',
'integer',
'int', // alias of 'integer'
'double', // float
'timestamp', // store as MongoTimestamp in Mongodb
'date', // store as DateTime
'boolean', // true or false
'array',
'object'
]
$object = new \stdClass();
$object->name = 'ooobject';
$user = new User();
$user->name = 'michael';
$user->myobject = $object; // this attribute will be omitted when saving to DB
$user->save();
$id = "517c850641da6da0ab000004";
$id = new \MongoId('517c850641da6da0ab000004'); //another way
$user = User::id( $id );
$params = array( 'name'=>'Michael','books'=>array('$size'=>2) );
$users = User::find($params); // $users is instance of Collection
echo $users->count();
$users = User::all();
$count = User::count(array('age'=>16));
$user = User::one();
$user->delete();
$book = new Book();
$book->name = "My Love";
$book->price = 15;
$book->save();
// !!!remember you must save book before!!!
$user->book_fav = $book;
$user->save();
// now you can do this
$user = User::one([
'name' => 'michael'
]);
echo $user->book_fav->name;
// or, find the parent by a known reference
$user = User::one([
'book_fav' => $book
]);
$user = User::one();
$id = $user->getId();
$book1 = new Book();
$book1->name = "book1";
$book1->save();
$book2 = new Book();
$book2->name = "book2";
$book2->save();
$user->books = array($book1,$book2);
//also you can
$user->books = Collection::make(array($book1,$book2));
$user->save();
//somewhere , load these books
$user = User::id($id);
$books = $user->books; // $books is a instance of Collection
$pet = new Pet();
$pet->name = "putty";
$user->pet_fav = $pet;
$user->save();
// now you can do this
$user = User::one( array('name'=>"michael" ) );
echo $user->pet_fav->name;
$user = User::one();
$id = $user->getId();
$pet_dog = new Pet();
$pet_dog->name = "puppy";
$pet_dog->save();
$pet_cat = new Pet();
$pet_cat->name = "kitty";
$pet_cat->save();
$user->pets = array($pet_cat,$pet_dog);
//also you can
$user->pets = Collection::make(array($pet_cat,$pet_dog));
$user->save();
$user = User::id($id);
$pets = $user->pets;