PHP code example of kuzdo / mongoyii-php7

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

    

kuzdo / mongoyii-php7 example snippets


use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;

use sammaye\mongoyii\Document;

/**
 * Represents the article itself, and all of its data
 */
class Article extends Document
{
}

'session' => array(
	'class' => 'sammaye\mongoyii\util\Session',
),
'cache' => array(
	'class' => 'sammaye\mongoyii\util\Cache',
),

public function behaviors()
{
	return [
		'TimestampBehavior' => [
			'class' => 'sammaye\mongoyii\behaviors\TimestampBehavior' 
			// adds a nice create_time and update_time Mongodate to our docs
		]
	];
}

namespace sammaye\mongoyii\validators;

'mongodb' => [
	'class' => 'sammaye\mongoyii\Client',
	'uri' => 'mongodb://sam:blah@localhost:27017/admin',
	'options' => [],
	'driverOptions' => [],
	'db' => [
		'super_test' => [
			'writeConcern' => new WriteConcern(1),
			'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
		]
	],
	'enableProfiling' => true
],

'super_test' => [
	'writeConcern' => new WriteConcern(1),
	'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
	'active' => true
]

Yii::$app->mongodb->selectDatabase()

public function getCollection()
{
	return $this
		->getDbConnection()
		->selectDatabase('my_other_db_not_default')
		->{$this->collectionName()};
}

Yii::$app->mongodb->selectDatabase('my_other_db', ['active' => true]);

Article::find(
	[
		'title' => 'Test'
	],  
	[
		'sort' => ['date_created' => -1],
		'limit' => 2
		// etc
	]
)

[
	'condition' => ['deleted' => 0],
	'select' => ['_id' => 1],
	'sort' => ['date' => -1],
	'limit' => 2
	'skip' => 1
]

$docs = new Query([
	'from' => 'colllection',
	'condition' => ['what' => 'ever'],
	'limit' => 1
])->all()

public function authenticate()
{
	$record=User::model()->findOne(array('username' => $this->username));
	if ($record === null) {
		$this->errorCode = self::ERROR_USERNAME_INVALID;
	} else if ($record->password !== crypt($this->password, $record->password)) { // check crypted password against the one provided
		$this->errorCode = self::ERROR_PASSWORD_INVALID;
	} else {
		$this->_id = (String)$record->_id;
		$this->errorCode = self::ERROR_NONE;
	}
	return !$this->errorCode;
}