Collection.php 4.38 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 15/6/13
 * Time: 下午4:50
 */

namespace Hood\Dao\MongoDB;

class Collection
{

    /**
     * Enter description here...
     *
     * @var MongoCore
     */
    private $_db;

    /**
     * @var string
     */
    private $_dbName;

    /**
     * @var \MongoCollection
     */
    private $_collection;

    /**
     * 条件
     * @var array
     */
    private $_attrs = array();

    /**
     * 返回的结果集
     * @var array
     */
    private $fields = array();

    /**
     * @var string
     */
    private $_collectionName;

    private $_set = false;

    public function __construct(\MongoClient $mongo, $dbName, $collection)
    {
        $this->_dbName = $dbName;
        $this->_collectionName = $collection;
        $this->_db = $mongo->selectDB($this->_dbName);
        $this->_collection = $mongo->selectCollection($this->_dbName, $this->_collectionName);
    }

    /**
     * 插入数据
     * @param $attrs
     * @param array $options
     * @return MongoResult
     */
    public function insert($attrs, array $options = array())
    {
        $result = new MongoResult();
        $result->status = $bool = $this->_collection->insert($attrs, $options);
        $result->attrs = $attrs;
        if ($bool) {
            if ($attrs["_id"] instanceof \MongoId) {
                $result->_id = $attrs["_id"]->__toString();
            } else {
                $result->_id = $attrs["_id"];
            }
        }
        return $result;
    }

    /**
     * 查询所有数据
     * @param array $query
     * @param array $fields
     * @return array
     */
    public function find(array $query = array(), array $fields = array())
    {
        $_results = $this->_collection->find($query, $fields);
        $rets = array();
        foreach ($_results as $value) {
            if (isset($value["_id"]) && ($value["_id"] instanceof \MongoId)) {
                $value["_id"] = $value["_id"]->__toString();
            }
            $rets[] = $value;
        }
        return $rets;
    }

    /**
     * 查找一行数据
     * @param array $query
     * @param array $fields
     * @return array|null
     */
    public function findOne(array $query = array(), array $fields = array())
    {
        $_fields = empty($fields) ? $this->fields : $fields;
        $_results = $this->_collection->findOne($query, $_fields);
        if (isset($_results["_id"]) && ($_results["_id"] instanceof \MongoId)) {
            $_results["_id"] = $_results["_id"]->__toString();
        }
        return $_results;
    }

    /**
     * 设置返回字段
     * @param array $fields
     * @return $this
     */
    public function fields(array $fields)
    {
        $this->fields = $fields;
        return $this;
    }

    /**
     * 组合查询条件
     * @return array
     */
    private function criteria()
    {
        $attrs = $this->_attrs;
        foreach ($this->_attrs as $attr => $values) {
            if (!empty($values)) {
                if (count($values) == 1) {
                    $attrs[$attr] = $values[0];
                } else {
                    $attrs[$attr]['$in'] = $values;
                }
            }
        }
        foreach ($this->_conds as $key => $value) {
            $attrs[$key] = $value;
        }
        return $attrs;
    }

    /**
     * 更新
     * @param array $criteria
     * @param array $newobj
     * @param array $options
     * @return bool
     */
    public function update(array $criteria, array $newobj, array $options = array())
    {
        if ($this->_set && !isset($newobj['$set'])) {
            $newobj = array('$set' => $newobj);
        }
        return $this->_collection->update($criteria, $newobj, $options);
    }

    /**
     * 从集合中删除记录
     * @param array $criteria
     * @param array $options
     * @return array|bool
     */
    public function remove(array $criteria = array(), array $options = array())
    {
        return $this->_collection->remove($criteria, $options);
    }


    public function set()
    {
        $this->_set = true;
        return $this;
    }

    /**
     * 查找一行数据,但只返回ID数据
     * @param $id
     * @return array|null
     */
    function findID($id, array $fields = array())
    {
        $_fields = empty($fields) ? $this->fields : $fields;
        return $this->_collection->findOne(array('_id' => new \MongoId($id)), $_fields);
    }
}