116 lines
3.6 KiB
PHP
116 lines
3.6 KiB
PHP
<?php
|
|
|
|
|
|
class App_Model_MongoResource_Abstract extends Zend_Db_Table_Abstract
|
|
{
|
|
|
|
protected $_defaultOrder; // Formato classico '<NOMECAMPO> <DESC | ASC>'
|
|
protected $_defaultTipoOrder;
|
|
|
|
public $_defaultProjection; // array di projection mongo db
|
|
public $_defaultAddFields; // array di addFileds con el regole di mongoDb
|
|
|
|
public function __construct()
|
|
{
|
|
if(!$this->_defaultOrder){
|
|
throw new App_Controller_Errors_WebServiceError("'_defaultOrder' non definito per la risorsa ". get_class($this));
|
|
}
|
|
|
|
if(!$this->_defaultTipoOrder){
|
|
throw new App_Controller_Errors_WebServiceError("'_defaultTipoOrder' non definito per la risorsa " . get_class($this));
|
|
}
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
//Funzione che restituisce il nome della collezione
|
|
public function getCollectionName()
|
|
{
|
|
return $this->_name;
|
|
}
|
|
|
|
//Funzione che restituisce la chiave primaria sicura
|
|
public function getPrimaryKey()
|
|
{
|
|
if (is_array($this->_primary)) return $this->_primary[1];
|
|
return $this->_primary;
|
|
}
|
|
|
|
//Funzione che restituisce il campo ordinamento di default
|
|
public function getDefaultOrder($order)
|
|
{
|
|
$order = (!$order) ? $this->_defaultOrder : $order;
|
|
|
|
if (!is_array($order)) {
|
|
$order = explode(',', $order);
|
|
}
|
|
|
|
$sort = array();
|
|
if (count($order) > 0)
|
|
foreach ($order as $val) {
|
|
$tmp = explode(' ', trim($val));
|
|
$orderType = strtoupper(trim($tmp[1]));
|
|
$orderType = ($tmp[1] && ($orderType == 'ASC' || $orderType == 'DESC')) ? $orderType : 'ASC';
|
|
$sort[trim($tmp[0])] = ($orderType == 'ASC') ? 1 : -1;
|
|
}
|
|
return $sort;
|
|
}
|
|
|
|
//Funzione che restituisce il tipo di ordinamento di default
|
|
public function getDefaultTipoOrder()
|
|
{
|
|
if (isset($this->_defaultTipoOrder) && $this->_defaultTipoOrder) return $this->_defaultTipoOrder;
|
|
return "ASC";
|
|
}
|
|
|
|
public function getDefaultAddFields()
|
|
{
|
|
if($this->_defaultAddFields) return $this->_defaultAddFields;
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getDefaultProjection($projection, $defaultProjection = null, $hideObjectId = true)
|
|
{
|
|
$defaultProjection = ($this->_defaultProjection === null) ? (($hideObjectId) ? ['_id'=>0] : []) : $this->_defaultProjection;
|
|
$projectionArray = ($hideObjectId) ? ['_id'=>0] : [];
|
|
|
|
if (is_null($projection) || $projection === '*' || (is_array($projection) && count($projection) <= 0)) {
|
|
return $defaultProjection;
|
|
}
|
|
|
|
if (is_string($projection)) {
|
|
$projection = explode(',', $projection);
|
|
}
|
|
|
|
// aggiungendo '#0' tra i valori della proiezione eseguo la proiezione negativa
|
|
$projectionType = in_array('#0', $projection) ? 0 : 1;
|
|
|
|
if (is_array($projection) && count($projection) > 0) {
|
|
foreach ($projection as $column) {
|
|
$projectionArray[$column] = $projectionType;
|
|
}
|
|
}
|
|
|
|
return $projectionArray;
|
|
}
|
|
|
|
public function getDefaultOrderString()
|
|
{
|
|
return $this->getDefaultOrder() . ' ' . $this->getDefaultTipoOrder();
|
|
}
|
|
|
|
//--------------- FUNZIONI PER LA GESTIONE DI DATABASE MULTIPLI ------------------------------------------
|
|
|
|
public function _setDbName($nomedb)
|
|
{
|
|
Zend_Registry::set(App_Model_Mongo_Adapter::MONGO_DB_NAME, $nomedb);
|
|
}
|
|
|
|
public function getDbName()
|
|
{
|
|
return Zend_Registry::get(App_Model_Mongo_Adapter::MONGO_DB_NAME);
|
|
}
|
|
|
|
}
|