Initial Commit
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
class App_Model_Mongo_Client
|
||||
{
|
||||
|
||||
const WHERE_MONGO_REG_NAME = 'WHERE_MONGO_REG_NAME';
|
||||
|
||||
protected $_config;
|
||||
protected $_mongoClient = null;
|
||||
protected $_db = null;
|
||||
|
||||
public function __construct($connectionUrl = 'mongodb://127.0.0.1:27017')
|
||||
{
|
||||
if (Zend_Registry::isRegistered(App_Model_Mongo_Adapter::MONGO_DB_ADAPTER_REG_NAME)) {
|
||||
$mongoAdapter = Zend_Registry::get(App_Model_Mongo_Adapter::MONGO_DB_ADAPTER_REG_NAME);
|
||||
} else {
|
||||
$mongoAdapter = new App_Model_Mongo_Adapter();
|
||||
$mongoAdapter->setDbname(Zend_Registry::get(App_Model_Mongo_Adapter::MONGO_DB_NAME));
|
||||
}
|
||||
|
||||
$this->_mongoClient = $mongoAdapter->getMongoClient();
|
||||
$dbName = $mongoAdapter->getDbname();
|
||||
|
||||
$this->_db = $this->_mongoClient->selectDatabase($dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return App_Model_MongoResource_Abstract
|
||||
* @throws App_Controller_Errors_WebServiceError
|
||||
*/
|
||||
public function getResource($name)
|
||||
{
|
||||
|
||||
if (!isset($this->_resources[$name])) {
|
||||
$class = implode('_', array(
|
||||
'Application',
|
||||
'Model',
|
||||
'MongoResources',
|
||||
ucfirst($name)));
|
||||
|
||||
//Controllo l'esistenza della classe
|
||||
if (!class_exists($class)) {
|
||||
throw new App_Controller_Errors_WebServiceError("Errore generazione la classe risorsa mongo '$class' potrebbe non esistere");
|
||||
}
|
||||
|
||||
$this->_resources[$name] = new $class();
|
||||
}
|
||||
|
||||
return $this->_resources[$name];
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
return $this->_mongoClient->test;
|
||||
}
|
||||
|
||||
public function setDatabase($dbName)
|
||||
{
|
||||
return $this->_db = $this->_mongoClient->selectDatabase($dbName);
|
||||
}
|
||||
|
||||
public function listDatabases()
|
||||
{
|
||||
return $this->_mongoClient->listDatabases();
|
||||
}
|
||||
|
||||
public function getDatabase()
|
||||
{
|
||||
return $this->_db;
|
||||
}
|
||||
|
||||
public function deleteDatabase()
|
||||
{
|
||||
return $this->_mongoClient->dropDatabase($this->getDatabase());
|
||||
}
|
||||
|
||||
public function getCollection($resourceName)
|
||||
{
|
||||
$collection = $this->getResource($resourceName)->getCollectionName();
|
||||
return $this->getDatabase()->$collection;
|
||||
}
|
||||
|
||||
public function emptyCollection($resourceName)
|
||||
{
|
||||
return $this->getCollection($resourceName)->deleteMany(['id' => array('$gt'=>0)]);
|
||||
}
|
||||
|
||||
// FUNZIONI PER INTERROGARE IL DB
|
||||
|
||||
public function getById($name, $id)
|
||||
{
|
||||
$optionsParams['projection'] = ['_id' => 0];
|
||||
return $this->getCollection($name)->findOne([$this->getResource($name)->getPrimaryKey() => $id], $optionsParams);
|
||||
}
|
||||
|
||||
public function insert($name, $data)
|
||||
{
|
||||
$id = uniqid();
|
||||
$this->getCollection($name)->insertOne(array_merge([$this->getResource($name)->getPrimaryKey() => $id], $data));
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function update($name, $id, $data)
|
||||
{
|
||||
return $this->getCollection($name)->updateOne([$this->getResource($name)->getPrimaryKey() => $id], ['$set' => $data]);
|
||||
}
|
||||
|
||||
public function delete($name, $id)
|
||||
{
|
||||
return $this->getCollection($name)->deleteOne([$this->getResource($name)->getPrimaryKey() => $id]);
|
||||
}
|
||||
|
||||
public function getCountTotal($name)
|
||||
{
|
||||
return $this->getCollection($name)->countDocuments();
|
||||
}
|
||||
|
||||
public function getCountFilteredTotal($name, $filter)
|
||||
{
|
||||
return $this->getCollection($name)->countDocuments($filter);
|
||||
}
|
||||
|
||||
public function getList($name, $paged = null, $count = null, $where = null, $order = null, $projection = null)
|
||||
{
|
||||
$findParams = array();
|
||||
|
||||
if (null !== $paged && null !== $count && $count > 0 && $paged > 0) {
|
||||
if ($paged > 1) $findParams['skip'] = (($paged - 1) * $count);
|
||||
$findParams['limit'] = $count;
|
||||
}
|
||||
|
||||
$order = (!$order) ? $this->getResource($name)->getDefaultOrder($order) : $order;
|
||||
if ($order && is_array($order)) {
|
||||
$findParams['sort'] = $order;
|
||||
}
|
||||
|
||||
$projection = (!$projection) ? $this->getResource($name)->getDefaultProjection($projection) : $projection;
|
||||
if ($projection && is_array($projection)) {
|
||||
$findParams['projection'] = $projection;
|
||||
}
|
||||
|
||||
if ($where === null) $where = array();
|
||||
|
||||
$cursor = $this->getCollection($name)->find($where, $findParams);
|
||||
|
||||
return $cursor->toArray();
|
||||
}
|
||||
|
||||
public function getListAggregation($name, $paged = null, $count = null, $where = null, $order = null, $projection = null, $addFields = null, $extraAggregation = null)
|
||||
{
|
||||
$aggregations = array();
|
||||
|
||||
if ($where === null) $where = array();
|
||||
if (count($where) > 0) {
|
||||
array_push($aggregations, ['$match' => $where]);
|
||||
}
|
||||
|
||||
$order = $this->getResource($name)->getDefaultOrder($order);
|
||||
if ($order && is_array($order)) {
|
||||
array_push($aggregations, ['$sort' => $order]);
|
||||
}
|
||||
|
||||
if (null !== $paged && null !== $count && $count > 0 && $paged > 0) {
|
||||
if ($paged > 1)
|
||||
array_push($aggregations, ['$skip' => (($paged - 1) * $count)]);
|
||||
array_push($aggregations, ['$limit' => $count]);
|
||||
}
|
||||
|
||||
$projection = $this->getResource($name)->getDefaultProjection($projection);
|
||||
if ($projection && is_array($projection)) {
|
||||
array_push($aggregations, ['$project' => $projection]);
|
||||
}
|
||||
|
||||
//$totElements = $this->getCountTotal($name);
|
||||
$addFields = $this->getResource($name)->getDefaultAddFields();
|
||||
if ($addFields && is_array($addFields)) {
|
||||
|
||||
/*if($totElements <= 0){
|
||||
unset($addFields['Data']);
|
||||
}*/
|
||||
|
||||
array_push($aggregations, ['$addFields' => $addFields]);
|
||||
}
|
||||
|
||||
if ($extraAggregation && is_array($extraAggregation)) {
|
||||
if(count($extraAggregation) > 1){
|
||||
$aggregations = array_merge($aggregations, $extraAggregation);
|
||||
}else{
|
||||
array_push($aggregations, $extraAggregation);
|
||||
}
|
||||
}
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($aggregations,true));
|
||||
|
||||
$cursor = $this->getCollection($name)->aggregate($aggregations);
|
||||
|
||||
return $cursor->toArray();
|
||||
}
|
||||
|
||||
public function getByColumnName($name, $columnName, $value, $op = "=", $all = false)
|
||||
{
|
||||
|
||||
switch ($op) {
|
||||
case'=':
|
||||
default:
|
||||
$params = array($columnName => $value);
|
||||
break;
|
||||
case 'LIKE':
|
||||
$params = array($columnName => ['$regex' => $value, '$options' => 'i']);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($all) {
|
||||
return $this->getCollection($name)->find($params)->toArray();
|
||||
} else {
|
||||
return $this->getCollection($name)->findOne($params);
|
||||
}
|
||||
}
|
||||
|
||||
public function find($name, $params = array())
|
||||
{
|
||||
return $this->getCollection($name)->find($params)->toArray();
|
||||
}
|
||||
|
||||
public function aggregate($name, $aggregate)
|
||||
{
|
||||
return $this->getCollection($name)->aggregate($aggregate);
|
||||
}
|
||||
|
||||
// CRUD FUNCTION WITH OBJECTID
|
||||
|
||||
public function getByObjectId($name, $id)
|
||||
{
|
||||
return $this->getCollection($name)->find([$this->getResource($name)->getPrimaryKey() => new MongoDB\BSON\ObjectID($id)])->toArray();
|
||||
}
|
||||
|
||||
public function insertWithObjectId($name, $data)
|
||||
{
|
||||
unset($data['_id']);
|
||||
return $this->getCollection($name)->insertOne($data);
|
||||
}
|
||||
|
||||
public function updateByObjectId($name, $id, $data)
|
||||
{
|
||||
return $this->getCollection($name)->updateOne([$this->getResource($name)->getPrimaryKey() => new MongoDB\BSON\ObjectID($id)], ['$set' => $data]);
|
||||
}
|
||||
|
||||
public function deleteByObjectId($name, $id)
|
||||
{
|
||||
return $this->getCollection($name)->deleteOne([$this->getResource($name)->getPrimaryKey() => new MongoDB\BSON\ObjectID($id)]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user