PHP code example of jrschumacher / modomo

1. Go to this page and download the library: Download jrschumacher/modomo 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/ */

    

jrschumacher / modomo example snippets



    namespace Collections;

    class Person {}


    namespace Documents;

    class Person {}


    use Modomo\MongoClient;
    use Documents\Person;

    $m = new MongoClient();
    $db = $m->test;
    $coll = $db->person;

    $bob = new Person(array(), $coll);
    $bob->name = "Bob";
    $bob->save();

    $people = $coll->find();
    $bob = $people->getNext();
    $bob->getDoc; // array('name' => 'Bob', '_id' => array('$id' => '12345....'));


    // Assuming MongoDB collection is "people"

    // For collections
    Modomo\Config::$collectionNS;                                   // \Collections\People.php
    Modomo\Config::$collectionNS = 'App\\Collection';               // \App\Collection\People.php
    Modomo\Config::$collectionClass = '{{mongo.coll}}Collection';   // \App\Collection\PeopleCollection.php

    // For documents
    Modomo\Config::$documentNS;                                     // \Documents\People.php
    Modomo\Config::$documentNS = 'App\\Document';                   // \App\Document\People.php
    Modomo\Config::$documentClass = '{{mongo.coll}}Document';       // \App\Document\PeopleDocument.php


    // To change the name space to \XYZ
    Modomo\Config::$collectionNS = 'XYZ';
    // To change the namespace to \XYZ\ABC
    Modomo\Config::$collectionNS = 'XYZ\\ABC';


    // To change the class name to XYZ (not a good idea)
    Modomo\Config::$collectionClass = 'XYZ';
    // To change the class name to AwesomePeopleCollection
    Modomo\Config::$collectionClass = 'Awesome{{mongo.coll}}Collection';


    namespace Collections;

    class Person {}

    $beforeSaveCallback = function() {};

    Person::registerEvent('beforeSave', $beforeSaveCallback, array('param1', 'param2'));