You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					140 lines
				
				3.7 KiB
			
		
		
			
		
	
	
					140 lines
				
				3.7 KiB
			| 
											12 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\mongo;
 | ||
|  | 
 | ||
| 
											12 years ago
										 | use yii\base\Object;
 | ||
| 
											12 years ago
										 | use Yii;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | /**
 | ||
| 
											12 years ago
										 |  * Collection represents the Mongo collection information.
 | ||
| 
											12 years ago
										 |  *
 | ||
|  |  * @author Paul Klimov <klimov.paul@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											12 years ago
										 | class Collection extends Object
 | ||
| 
											12 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @var \MongoCollection Mongo collection instance.
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public $mongoCollection;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Drops this collection.
 | ||
| 
											12 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public function drop()
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$this->mongoCollection->drop();
 | ||
| 
											12 years ago
										 | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param array $query
 | ||
|  | 	 * @param array $fields
 | ||
|  | 	 * @return \MongoCursor
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function find($query = [], $fields = [])
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return $this->mongoCollection->find($query, $fields);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param array $query
 | ||
|  | 	 * @param array $fields
 | ||
|  | 	 * @return array
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function findAll($query = [], $fields = [])
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$cursor = $this->find($query, $fields);
 | ||
| 
											12 years ago
										 | 		$result = [];
 | ||
|  | 		foreach ($cursor as $data) {
 | ||
|  | 			$result[] = $data;
 | ||
|  | 		}
 | ||
|  | 		return $result;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Inserts new data into collection.
 | ||
|  | 	 * @param array|object $data data to be inserted.
 | ||
|  | 	 * @param array $options list of options in format: optionName => optionValue.
 | ||
|  | 	 * @return \MongoId new record id instance.
 | ||
|  | 	 * @throws Exception on failure.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function insert($data, $options = [])
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$token = 'Inserting data into ' . $this->mongoCollection->getName();
 | ||
| 
											12 years ago
										 | 		Yii::info($token, __METHOD__);
 | ||
|  | 		try {
 | ||
|  | 			Yii::beginProfile($token, __METHOD__);
 | ||
| 
											12 years ago
										 | 			$this->tryResultError($this->mongoCollection->insert($data, $options));
 | ||
| 
											12 years ago
										 | 			Yii::endProfile($token, __METHOD__);
 | ||
|  | 			return is_array($data) ? $data['_id'] : $data->_id;
 | ||
|  | 		} catch (\Exception $e) {
 | ||
|  | 			Yii::endProfile($token, __METHOD__);
 | ||
|  | 			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Update the existing database data, otherwise insert this data
 | ||
|  | 	 * @param array|object $data data to be updated/inserted.
 | ||
|  | 	 * @param array $options list of options in format: optionName => optionValue.
 | ||
|  | 	 * @return \MongoId updated/new record id instance.
 | ||
|  | 	 * @throws Exception on failure.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function save($data, $options = [])
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$token = 'Saving data into ' . $this->mongoCollection->getName();
 | ||
| 
											12 years ago
										 | 		Yii::info($token, __METHOD__);
 | ||
|  | 		try {
 | ||
|  | 			Yii::beginProfile($token, __METHOD__);
 | ||
| 
											12 years ago
										 | 			$this->tryResultError($this->mongoCollection->save($data, $options));
 | ||
| 
											12 years ago
										 | 			Yii::endProfile($token, __METHOD__);
 | ||
|  | 			return is_array($data) ? $data['_id'] : $data->_id;
 | ||
|  | 		} catch (\Exception $e) {
 | ||
|  | 			Yii::endProfile($token, __METHOD__);
 | ||
|  | 			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Removes data from the collection.
 | ||
|  | 	 * @param array $criteria description of records to remove.
 | ||
|  | 	 * @param array $options list of options in format: optionName => optionValue.
 | ||
|  | 	 * @return boolean whether operation was successful.
 | ||
|  | 	 * @throws Exception on failure.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function remove($criteria = [], $options = [])
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$token = 'Removing data from ' . $this->mongoCollection->getName();
 | ||
| 
											12 years ago
										 | 		Yii::info($token, __METHOD__);
 | ||
|  | 		try {
 | ||
|  | 			Yii::beginProfile($token, __METHOD__);
 | ||
| 
											12 years ago
										 | 			$this->tryResultError($this->mongoCollection->remove($criteria, $options));
 | ||
| 
											12 years ago
										 | 			Yii::endProfile($token, __METHOD__);
 | ||
|  | 			return true;
 | ||
|  | 		} catch (\Exception $e) {
 | ||
|  | 			Yii::endProfile($token, __METHOD__);
 | ||
|  | 			throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Checks if command execution result ended with an error.
 | ||
|  | 	 * @param mixed $result raw command execution result.
 | ||
|  | 	 * @throws Exception if an error occurred.
 | ||
|  | 	 */
 | ||
|  | 	protected function tryResultError($result)
 | ||
|  | 	{
 | ||
|  | 		if (is_array($result)) {
 | ||
|  | 			if (!empty($result['err'])) {
 | ||
|  | 				throw new Exception($result['errmsg'], (int)$result['code']);
 | ||
|  | 			}
 | ||
|  | 		} elseif (!$result) {
 | ||
|  | 			throw new Exception('Unknown error, use "w=1" option to enable error tracking');
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | }
 |