mongoObj = new MongoClient(); } else { if (!preg_match("~^mongo[:][/][/]\w+[:]\w+$~",$connectionString)){ throw new CoreException("Invalid connectionString parameter passed to 'GenericDao::__construct()' ('".print_r($connectionString,true)."')"); } $this->mongoObj = new MongoClient($connectionString); } $this->database = $this->mongoObj->$dbName; } /** * Ritorna un array di model * @param string $collectionName nome della collezione * @param array $filter array associativo della select formato mongo * @param array $options (al momento supporta limit e sort) * @param string $customReturnClass * @throws CoreException se la CustomReturnClass non รจ valida * @return DaoSaveable[] */ public function query($collectionName, array $filter = array(), array $options = array(), $customReturnClass = null){ $retClass = self::$defaultReturnClass; if (!is_null($customReturnClass)){ if (!is_subclass_of($customReturnClass, "DaoSaveable", true)){ throw new CoreException("Invalid return class passed to GenericDao::query ($customReturnClass)"); } $retClass = $customReturnClass; } $collection = $this->database->$collectionName; if (!array_key_exists("deleted",$filter)){ $filter["deleted"] = false; } $cursor = $collection->find($filter); if (array_key_exists("sort",$options)){ $cursor->sort($options["sort"]); } if (array_key_exists("limit",$options)){ $cursor->limit($options["limit"]); } $rval = array(); foreach ($cursor as $element){ var_dump($element); $rval[] = $retClass::buildFromSaveableObj($collectionName, $element); } return $rval; } public function save(DaoSaveable $model){ $collectionName = $model->getCollectionName(); $collection = $this->database->$collectionName; $saveObj = $model->getSaveableObj(); if ( !is_null($saveObj["_id"]) ){ $collection->update( array("_id"=>$saveObj["_id"] ), $saveObj ); } else { $saveObj["_id"] = new MongoId(); $collection->insert($saveObj); $model->updateFromSaveableObj($saveObj); } } public function delete(DaoSaveable $model){ $collectionName = $model->getCollectionName(); $collection = $this->database->$collectionName; $saveObj = $model->getSaveableObj(); if ( !is_null($saveObj->_id) ){ $saveObj["deleted"] = true; $collection->update( array("_id"=>$id), $saveObj ); } } /** * Imposta il tipo di oggetto restituito dalle query; la classe deve implementare l'interfaccia DaoSaveable. * Default DaoSaveableDefaultImplementation * @param string $retClass nome della classe * @throws CoreException se la classe non implementa l'interfaccia DaoSaveable */ public static function setDefaultReturnClass($retClass){ if (!is_subclass_of($retClass, "DaoSaveable", true)){ throw new CoreException("Invalid return class passed to GenericDao::setDefaultReturnClass ($retClass)"); } self::$defaultReturnClass = $retClass; } } ?>