PHP code example of rogerthomas84 / ohdm

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

    

rogerthomas84 / ohdm example snippets



Config::init(
    new \MongoClient(
        'mongodb://127.0.0.1',
        array('connect' => false)
    ),
    'pets' // The database name
);


namespace My\Namespace\Name;

use OhDM\Collection;

class Cats extends Collection
{
    public $name = null;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}



$record = new Cats();
$record->setName('fuzzy');
$record->save();


$record = Cats::findById(
    new \MongoId('53466f2978f7a8bc5c1ae933')
);
if ($record) {
    // $record is instance of Cats
    echo 'My cats name is: ' . $record->getName();
} else {
    // couldn't find it!
}


$command = array(
    'query' => array('name' => 'kitty'),
    'fields' => array(),
    'skip' => 0,
    'limit' => 100,
    'sort' => array(
        'name' => 1
    )
);
$ohDmCursor = Cats::find($command);
while ($ohDmCursor->hasNext()) {
    $thisCat = $ohDmCursor->getNext(); // it's an instance of Cats
}


$record = Cats::findById(
    new \MongoId('53466f2978f7a8bc5c1ae933')
);
$record->delete();


$record = Cats::findById(
    new \MongoId('53466f2978f7a8bc5c1ae933')
);
if ($record) {
    // $record is instance of Cats
    echo 'My cats name was: ' . $record->getName();

    $record->setName('kitty');
    $record->save();

    echo 'My cats name is now: ' . $record->getName();

} else {
    // couldn't find it!
}