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 $modelClass classe da ritornare (DaoSaveable) * @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($modelClass, array $filter = array(), array $options = array()){ if (!is_null($modelClass)){ if (!is_subclass_of($modelClass, "DaoSaveable", true)){ throw new CoreException("Invalid return class passed to GenericDao::query ($modelClass)"); } } $collectionName = $modelClass::getCollectionName(); $collection = $this->database->$collectionName; $useFilter = $filter; if (!array_key_exists("deleted",$useFilter)){ $useFilter["deleted"] = false; } if (array_key_exists("id",$useFilter)){ $useFilter["_id"] = $useFilter["id"]; unset($useFilter["id"]); } if (array_key_exists("_id",$useFilter) && is_string($useFilter["_id"])){ $useFilter["_id"] = new MongoId($useFilter["_id"]); } $cursor = $collection->find($useFilter); 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){ $rval[] = $modelClass::buildFromSaveableObj($element); } return $rval; } /** * Come query ma ritorna il primo o null se non ci sono risultati * @param string $modelClass * @param array $filter * @param array $options * @return DaoSaveable */ public function getFirst($modelClass, array $filter = array(), array $options = array()){ $rval = $this->query($modelClass,$filter,$options); if (sizeof($rval)>0){ $rval = reset($rval); } else { $rval = null; } return $rval; } /** * Salva... * @param DaoSaveable $model */ public function save(DaoSaveable $model){ $collectionName = $model->getCollectionName(); $collection = $this->database->$collectionName; $saveObj = $model->getSaveableObj(); if ( array_key_exists("_id", $saveObj) && !is_null($saveObj["_id"]) ){ $collection->update( array("_id"=>$saveObj["_id"] ), $saveObj ); } else { $saveObj["_id"] = new MongoId(); $collection->insert($saveObj); $model->updateFromSaveableObj($saveObj); } } /** * Cancella... la cancellazione non è reale, viene solo settato il campo deleted a true. * @param DaoSaveable $model */ public function delete(DaoSaveable $model){ $collectionName = $model->getCollectionName(); $collection = $this->database->$collectionName; $saveObj = $model->getSaveableObj(); if ( array_key_exists("_id", $saveObj) && !is_null($saveObj["_id"]) ){ $saveObj["deleted"] = true; $collection->update( array("_id"=>$saveObj["_id"] ), $saveObj ); $model->updateFromSaveableObj($saveObj); } } // public static function registerModel($model){ // if(is_subclass_of($model, "DaoSaveable",true)){ // $collectionName = $model::getCollectionName(); // self::$MODEL_REGISTRY[$collectionName] = $model; // } // else{ // throw new CoreException("Invalid model passed to GenericDao::registerModel ($model)"); // } // } } ?>