PHP code example of martynbiz / php-mongo

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

    

martynbiz / php-mongo example snippets


\MartynBiz\Mongo\Connection::getInstance()->init(array(
    'db' => 'mydb',
    'username' => 'myuser',
    'password' => '89dD7HH7di!89',
    'classmap' => array(
        'users' => '\\App\\Model\\User',
    ),
));



use MartynBiz\Mongo;

class User extends Mongo
{
    //  define on the fields that can be saved
    protected static $whitelist = array(
        'name',
        'email',
        'username',
        'password',
    );
}

Connection::getInstance('conn1')->init(array(
    ...
));

// also, checking if an instance exists
if (! Connection::hasInstance('conn2')) {
    Connection::getInstance('conn2')->init(array(
        ...
    ));
}



use MartynBiz\Mongo;

class User extends mongo
{
    // optional - if using multiple connections/databases
    protected static $conn = 'conn1';

    .
    .
    .
}

// statically
$users = User::find(array(
    'status' => 1,
));

// dynamically
$col = new User();
$users = $col->find(array(
    'status' => 1,
));

// statically
$user = User::findOne(array(
    'email' => '[email protected]',
));

// dynamically
$col = new User();
$user = $col->findOne(array(
    'email' => '[email protected]',
));

$friend = User::findOne(array(
    //...
));

$user = User::find(array(
    'friend' => $friend,
));

// setting

// on instantiation -- will be filtered against $whitelist
$article = new Article(array(
    'title' => 'My title',
));

$author = User::findOne(array(
    //...
));

// single value properties -- no param filtering
$article->status = 2;
$article->author = $author;

// AND/OR set() method, with Mongo instance -- suited for unit testing, no param filtering
$user = User::findOne(array(
    //...
));
$article->set('author', $author);

// set value as query result (will be stored as an array of dbrefs)
$tags = Tag::find(array(
    //...
));
$article->tags = $tags;

// lastly, params can be passed with save() -- will be filtered against $whitelist
$article->save(array(
    'content' => $text,
))

// getting

$article = Article::findOne(array(
    //...
));

// single value properties -- no param filtering
echo $article->status;
echo $article->author->name;
echo $article->tags[0]->name;
echo $article->get('author');

$user->name = 'Jim';
$user->save();

$user->save(array(
    'name' => 'Jim',
));

// statically
$user = User::create(array(
    'name' => 'Jim',
));

// dynamically (e.g. service locator pattern, or DI)
$col = new User();
$user = $col->create(array(
    'name' => 'Jim',
));

// statically
$user = User::factory(array(
    'name' => 'Jim',
));

// dynamically
$col = new User();
$user = $col->factory(array(
    'name' => 'Jim',
));

$user->save();


// push one object, will convert to DBRef
$user->push(array(
    'friends' => $friend,
));

// push multi object, will convert to DBRef
$user->push(array(
    'friends' => array(
        $friend,
        $friend2,
    ),
));

// push MongoIterator object (from find() call)
$user->push(array(
    'friends' => $friends,
));

// push multiple properties at once
$user->push(array(
    'friends' => $friends,
    'enemies' => $enemies,
));

// push without $each setting, will push the whole array as a single element
$user->push(array(
    'friends' => array(
        $friend,
        $friend2,
    ),
), array('each' => false));

$user->delete();

User::remove(array(
    'type' => 'boring',
), $options);

$user->toArray(3); // convert nested 3 deep to array (optional)

class User extends mongo
{
    .
    .
    .
    public function validate()
    {
        $this->resetErrors();

        if (empty($this->data['name'])) {
            $this->setError('Name is missing.');
        }

        return empty( $this->getErrors() ); // true if none
    }
}



use MartynBiz\Validator;

class User extends mongo
{
    .
    .
    .
    public function validate()
    {
        $this->resetErrors();

        $validator = new Validator($this->data);

        $validator->check('name')
            ->isNotEmpty('Name is missing');

        $validator->check('email')
            ->isNotEmpty('Email address is missing')
            ->isEmail('Invalid email address');

        $message = 'Password must contain upper and lower case characters, and have more than 8 characters';
        $validator->check('password')
            ->isNotEmpty($message)
            ->hasLowerCase($message)
            ->hasUpperCase($message)
            ->hasNumber($message)
            ->isMinimumLength($message, 8);

        // update the model's errors with the validators
        $this->setError( $validator->getErrors() );

        return empty($this->getErrors());
    }
}



use MartynBiz\Mongo;

class User extends Mongo
{
    .
    .
    .

    public function getCreatedAt($value)
    {
        return date('Y-M-d h:i:s', $this->data['created_at']->sec);
    }

    public function setPassword($value)
    {
        return password_hash($value, PASSWORD_BCRYPT);
    }
}

$ composer