PHP code example of yiicod / mongoyii

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

    

yiicod / mongoyii example snippets

 
composer.phar 
 
	'mongodb' => array(
		'class' => 'EMongoClient',
		'server' => 'mongodb://localhost:27017',
		'db' => 'super_test'
	),
 
	Yii::app()->mongodb->getSomething();
 
	Yii::app()->mongodb->getConnection()->getSomething();
 
		'log'=>array(
			'class'=>'CLogRouter',
			'routes'=>array(
				array(
					'class'=>'CFileLogRoute',
					'levels'=>'error, warning',
				),

				[ ... ]

				array(
					'class'=>'EMongoLogRoute',
					'connectionId'=>'my_connection_id', // optional, defaults to 'mongodb'
					'logCollectionName'=>'my_log_collection', // optional, defaults to 'YiiLog'
				),
				
			),
		),
 
	public function getMongoComponent()
	{
		return Yii::app()->someweirddbconnectionINIT;
	}
 
	{
	    "config": {
	        "vendor-dir": "protected/extensions"
	    }
	}
 
	Yii::app()->mongodb->collection->find(array('name' => 'sammaye'));
 
	class User extends EMongoModel{
	    /** @virtual */
	    public $somevar;
	}
 
	function relations(){
		return array(
			'others' => array('many', 'Other', 'otherId', 'sort' => array('_id'=>-1), 'skip' => 1, 'limit' => 10)
		);
	}
 
	function relations(){
		return array(
			'others' => array('many', 'Other', 'otherId', 'cache' => false)
		);
	}
 
	public function getPrimaryKey($value=null){
		if($value===null)
			$value=$this->{$this->primaryKey()};
		return (string)$value;
	}
 
	array(
		'condition' => array('deleted' => array('$ne' => 1)),
		'sort' => array('date' => -1),
		'skip' => 1,
		'limit' => 11
	)
 
	function someScope(){
		$this->mergeDbCriteria(array(
			'condition'=>array('scoped' => true),
			'sort'=>array('date'=>-1),
			'skip'=>1,
			'limit'=>11
		));
	}
 
	function someScope(){

		$criteria = array(
			'condition'=>array('scoped' => true),
			'sort'=>array('date'=>-1),
			'skip'=>1,
			'limit'=>11
		);

		if($this->deleted)
			$criteria = $this->mergeCriteria($criteria,array('condition'=>array('deleted'=>1)));

		$this->mergeDbCriteria($criteria);
		reutrn $this;
	}
 
    public function init()
    {
        if(YII_DEBUG){
            $this->ensureIndexes(array(
                array('username' => 1),
                array(array('email' => 1), array('unique' => true)),
                array(array('description' => 1))
            ));
        }
    }
 
	class User extends EMongoDocument{
		function collectionName(){
			return 'users';
		}

		public static function model($className=__CLASS__){
			return parent::model($className);
		}
	}
 
	class User extends EMongoDocument{

		/** @virtual */
		public $agree = 1;

		public $addresses = array();

		function collectionName(){
			return 'users';
		}

		public static function model($className=__CLASS__){
			return parent::model($className);
		}
	}
 
	User::model()->deleteByPk($_id[, array('deleted' => 1)[, array('w' => 2)]]);
	User::model()->updateByPk($_id, array('$set' => array('d' => 1)[, array('deleted' => 1)[, array('w' => 2)]]);
 
	array('username', 'EMongoUniqueValidator', 'className' => 'User', 'attributeName' => 'username')
 
	array('user_id', 'EMongoExistValidator', 'className' => 'User', 'attributeName' => '_id')
 
	array('ids,id', 'EMongoIdValidator'), // ids is an array while id is a single string value
 
	function behaviors(){
		return array(
			'EMongoTimestampBehaviour'
		);
	}
 
	function rules(){
		return array(
			array('addresses', 'subdocument', 'type' => 'many', 'rules' => array(
				array('road,town,county,post_code', 'safe'),
				array('telephone', 'integer')
			)),
		);
	}
 
	array(
		'addresses' => array(
			0 => array(
				'telephone' => array(
					0 => 'Some error here'
				)
			)
		)
	)
 
	if(isset($_POST['Comment'])){
		$comment=new Comment;
		$comment->attributes=$_POST['Comment'];
		if($comment->validate())
			$response = Post::model()->updateAll(array('_id' => $someId), array('$push' => $comment->getRawDocument()));
	}
 
	$valid=true;
	$user=User::model()->findBy_id($uid);
	if(isset($_POST['numbers'])){
		foreach($_POST['numbers'] as $row){
			$d=new Model();
			$d->attributes = $row;
			$valid=$d->validate()&&$valid;
			$user->numbers[] = $d;
		}
	}
	if($valid) $user->save();
 
	$m=new Something();
	$m->name='thing';
	$parentClass->things[6] = $m;
	$parentClass->save();
 
	new EMongoDataProvider(array(
		'criteria' => array(
			'condition' => array(),
			'sort' => array(),
			'skip' => 1,
			'limit' => 1
		),
		/* All other options */
	));
 
	$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'user-grid',
		'dataProvider'=>$model->search(),
		'filter'=>$model,
		'columns'=>array(
			'_id',
			'username',
			'addresses',
			'create_time',
			array(
				'class'=>'CButtonColumn',
				'template'=>'{update}{delete}',
			),
		),
	));
 
	$c = new EMongoCriteria();
	User::model()->find($c
					->addCondition(array('name' => 'sammaye')) // This is basically a select
					->addOrCondition(array(array('interest' => 'Drinking'), array('interest' => 'Clubbing'))) // This is adding a $or condition to our select
					->skip(2) // This skips a number of rows
					->limit(3) // This limits by a number of rows
						);
 
	$c = new EMongoCriteria(array(
		'condition' => array('name'=>'sammaye'),
		'limit' => 10
	));
 
	$c->compare('name', 'sammaye');

	$c->compare('i', '<4');
 
	array(
		'condition' => array(),
		'skip' => 1,
		'limit' => 1,
		'sort' => array(),
		'project' => array()
	)
 
	$c->project=array('_id'=>0,'d'=>1);
 
	functions scope(){
		return array(
			'project' => array('_id'=>0,'d'=>1)
		);
	}
 
	User::model()->find(array(),array('_id'=>0,'d'=>1));
 
	class versioned extends EMongoDocument{
		public function versioned(){
			return true;
		}
		
		public function versionField(){
			return '_v'; // This is actually the default value in EMongoDocument
		}
		
		public static function model($className=__CLASS__){
			return parent::model($className);
	    }	
	}
 
    'commandMap' => array(
        'migratemongo' => array(
            'class' => 'application.extensions.MongoYii.util.EMigrateMongoCommand'
        )
    )
 
	'components'=>array(
		...
		'cache' => array(
			'class'=>'application.extensions.MongoYii.util.EMongoCache',
			// 'ensureIndex' => true, //set to false after first use of the cache
			// 'mongoConnectionId' => 'mongodb',
			// 'collectionName' => 'mongodb_cache',		
		),
	}
 
	// flush cache
	Yii::app()->cache->flush();
	
	// add data to cache
	Yii::app()->cache->set('apple', 'fruit');
	Yii::app()->cache->set('onion', 'vegetables');
	Yii::app()->cache->set(1, 'one');
	Yii::app()->cache->set(2, 'two');
	Yii::app()->cache->set('one', 1);
	Yii::app()->cache->set('two', 2);
	
	// delete from cache
	Yii::app()->cache->delete(1);
	Yii::app()->cache->delete('two');
	
	// read from cache
	echo Yii::app()->cache->get(2);
	
	// multiple read from cache
	$arr = Yii::app()->cache->mget(array('apple', 1, 'two'));

  	print_r($arr); // Array( [apple] => fruit [1] => [two] => )
 
	'components' => array(
		...
		'messages' => array(
			'class' => 'application.extensions.MongoYii.util.EMongoMessageSource',
			// 'mongoConnectionId' => 'mongodb', 
			// 'collectionName' => 'YiiMessages',               
		)        
	)
 
	'session' => array(
		'class' => 'application.extensions.MongoYii.util.EMongoSession',
	)
 
	'authManager' => array(
    	'class' => 'EMongoAuthManager',
    )

	$cache = Yii::app()->cache;
	$cache->set(
		'12', 
		'dfgdfgf', 
		30,
		new EMongoCacheDependency('t', [
			[],
			'limit' => 5
		])
	);
	var_dump($cache->get('12'));
 
	$cache = Yii::app()->cache;
	Yii::app()->mongodb->t->insert(['g' => 1]);
	var_dump($cache->get('12'));
 
	$query = array();
	if(isset($this->query[0])){
		$query = $this->query[0];
	}
		
	$cursor = $this->getDbConnection()->{$this->collection}->find($query);
		
	if(isset($this->query['sort'])){
		$cursor->sort($this->query['sort']);
	}
		
	if(isset($this->query['skip'])){
		$cursor->limit($this->query['skip']);
	}
		
	if(isset($this->query['limit'])){
		$cursor->limit($this->query['limit']);
	}