/** * Retourne l'objet lu depuis le backend * * @param t41_Data_Object $do Data object to populate * @return t41_Data_Object populated data object */ public function read(ObjectModel\DataObject $do) { $uri = $do->getUri(); // set database to use $this->_selectDatabase($uri->getBackendUri()); // get table to use, from mapper if available, else from data object $collection = $this->_mapper instanceof Backend\Mapper ? $this->_mapper->getDatastore($uri->getClass()) : $uri->getClass(); $collec = $this->_db->selectCollection($collection); // get data from backend $data = array('_id' => new \MongoId($uri->getIdentifier())); $this->_setLastQuery('findOne', $data); $data = $collec->findOne($data); if (empty($data)) { return null; } /* complete url part of the object uri */ $do->getUri()->setUrl($this->_database . '/' . $collection . '/' . $do->getUri()->getIdentifier()); /* populate data object */ $do->populate($data, $this->_mapper); }
/** * Populate the given data object * * @param t41\ObjectModel\DataObject $do data object instance * @return boolean */ public function read(ObjectModel\DataObject $do) { // get table to use $table = $this->_getTableFromUri($do->getUri()); if (!$table) { \Zend_Debug::dump($do->getUri()); //die; require_once 't41/Backend/Exception.php'; throw new Exception('MISSING_DBTABLE_PARAM'); } // primary key is either part of the mapper configuration or 'id' $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($do->getUri()->getClass()) : 'id'; $this->_connect(); // get data from backend $select = $this->_ressource->select()->from($table)->where("{$pkey} = ?", $do->getUri()->getIdentifier())->limit(1); $data = $this->_ressource->fetchRow($select); if (empty($data)) { return false; } /* complete url part of the object uri */ $do->getUri()->setUrl($this->_database . '/' . $table . '/' . $do->getUri()->getIdentifier()); /* populate data object */ $do->populate($data, $this->_mapper); return true; }
public function find(ObjectModel\Collection $collection) { $class = $collection->getDataObject()->getClass(); $mode = $collection->getParameter('memberType'); $expr = ''; $this->_setRessource($class); /* @var $condition t41_Condition */ foreach ($collection->getConditions() as $conditionArray) { $condition = $conditionArray[0]; // map property to field if ($this->_mapper) { $field = $this->_mapper->propertyToDatastoreName($class, $condition->getProperty()->getId()); } else { $field = $condition->getProperty()->getId(); } if ($expr) { switch ($conditionArray[1]) { case 'OR': $expr .= ' or '; break; case 'AND': default: $expr .= ' and '; break; } } $expr .= sprintf("%s %s '%s'", $field, is_numeric($condition->getOperator()) ? $this->_operators[$condition->getOperator()] : $condition->getOperator(), $condition->getValue()); } // get all nodes id $result = $this->_findNodes($expr, '@id'); $dataSet = array(); foreach ($result as $node) { $dataSet[] = $node->nodeValue; } if (count($collection->getSortings()) > 0) { $sort = array(); foreach ($collection->getSortings() as $key => $sorting) { if ($this->_mapper) { $field = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId()); } else { $field = $sorting[0]->getId(); } $sort = $this->_sortNodes($sort, $field, $sorting[1], $key == 0 ? $dataSet : null); } } // Flatten array $sort = $this->_arrayflat($sort); //Zend_Debug::dump($sort); $array = array(); $uri = new ObjectModel\ObjectUri(); $uri->setBackendUri($this->_uri); $uri->setClass($class); if ($mode != 'uri') { $do = new ObjectModel\DataObject($class); } $count = $collection->getBoundaryOffset(); $limit = $count + $collection->getBoundaryBatch(); /* iterate over result set as long as requested */ while ($count < $limit) { if (!isset($dataSet[$count])) { // if end of result data set has been reached, return array return $array; } $id = $dataSet[$count]; $uri->setUrl($this->_alias . '/' . $id); switch ($mode) { case 'uri': $data = clone $uri; break; case 'data': $do->setUri(clone $uri); $do->populate(); $data = clone $do; break; case 'model': $do->setUri(clone $uri); $do->populate(); /* @var $obj t41_Object_Model */ $data = new $class(null, null, clone $do); break; } $array[] = $data; $count++; } return $array; }
/** * Returns an array of objects queried from the given t41_Object_Collection instance parameters * * The given collection is populated if it comes empty of members. * * In any other case, this method doesn't directly populate the collection. This action is under the responsability of * the caller. For example, the t41_Object_Collection::find() method takes care of it. * * @param t41_Object_Collection $collection * @return array */ public function find(t41_Object_Collection $collection) { $class = $collection->getDataObject()->getClass(); $filters = $sortings = array(); $searchMode = '&'; // primary key is either part of the mapper configuration or 'dn' $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : 'dn'; /* @var $condition t41_Condition */ foreach ($collection->getConditions() as $conditionArray) { $condition = $conditionArray[0]; /* does condition contain another condition object ? */ if ($condition->isRecursive()) { // not supported with LDAP continue; } $property = $condition->getProperty(); if ($property instanceof Property\ObjectProperty) { } else { $field = $property->getId(); if ($this->_mapper) { $field = $this->_mapper->propertyToDatastoreName($class, $field); } } $filters[] = $this->_buildConditionStatement($field, $condition->getClauses()); switch ($conditionArray[1]) { case 'OR': // $select->orWhere($statement); break; case 'AND': default: // $select->where($statement); break; } } foreach ($collection->getSortings() as $sorting) { if ($this->_mapper) { $class = $sorting[0]->getParent() ? $sorting[0]->getParent()->getId() : $collection->getDataObject()->getClass(); $field = $this->_mapper->propertyToDatastoreName($class, $sorting[0]->getId()); } else { $field = $sorting[0]->getId(); } $sortings[] = $field; } $filter = implode($filters); if (count($sortings) > 0) { $filter .= sprintf('(sort=%s)', implode(',', $sortings)); } if ($filter) { $filter = sprintf('%s%s', $searchMode, $filter); } $filter = $filter ? '(' . $filter . ')' : "(objectClass=*)"; try { if ($this->_mapper) { $this->_connect($this->_mapper->getDatastore($collection->getDataObject()->getClass())); } else { $this->_connect(); } /* @var $result Zend_Ldap_Collection */ /* $result = $this->_ressource->search( $filter , $this->_currentDn // Base DN , null //Zend_Ldap::SEARCH_SCOPE_ONE // Scope , null//array('sizeLimit' => $collection->getBoundaryBatch()) ); // query result */ $search = ldap_search($this->_ressource, $this->_currentDn, $filter); $result = ldap_get_entries($this->_ressource, $search); } catch (Exception $e) { throw new Exception("LDAP Query error: " . $e->getMessage()); } // first result is total records unset($result['count']); // populate array with relevant objects type $array = array(); $uri = new ObjectModel\ObjectUri(); $uri->setBackendUri($this->_uri); $uri->setClass($class); if ($collection->getParameter('memberType') != 'uri') { $do = new ObjectModel\DataObject($class); } foreach ($result as $entry) { $entry = $this->_flattenArray($entry, true); $uri->setUrl($this->_uri->getAlias() . '/' . $entry['dn']); switch ($collection->getParameter('memberType')) { case 'uri': $data = clone $uri; break; case 'data': default: $do->setUri(clone $uri); $do->populate($entry, $this->_mapper); $data = clone $do; break; case 'model': $do->setUri(clone $uri); $do->populate($entry, $this->_mapper); /* @var $obj t41_Object_Model */ $data = new $class(null, null, clone $do); break; } $array[] = $data; } return $array; }
/** * Populate the given data object * * @param t41\ObjectModel\DataObject $do data object instance * @return boolean */ public function read(ObjectModel\DataObject $do, $data = null) { if (!$do->getUri() instanceof ObjectUri) { throw new Exception('MISSING_URI_IN_DATAOBJECT'); } if (is_array($data) && count($data) > 0) { /* populate data object */ $do->populate($data, $this->_mapper); $do->resetChangedState(); return true; } // get table to use $table = $this->_getTableFromUri($do->getUri()); if (!$table) { \Zend_Debug::dump($do->getUri()); throw new Exception('MISSING_DBTABLE_PARAM'); } // primary key is either part of the mapper configuration or 'id' $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($do->getUri()->getClass()) : Backend::DEFAULT_PKEY; $this->_connect(); // get data from backend $select = $this->_ressource->select()->from($table, $this->_getColumns($do))->limit(1); /* add clause for primary key(s) */ foreach ($this->_preparePrimaryKeyClauses($do) as $key => $val) { $select->where("{$key} = ?", $val); } try { $data = $this->_ressource->fetchRow($select); } catch (\Exception $e) { echo $e->getMessage(); \Zend_Debug::dump($e->getTrace()); die; } if (empty($data)) { $do->resetUri(); return false; } /* complete url part of the object uri */ $do->getUri()->setUrl($table . '/' . $do->getUri()->getIdentifier()); /* populate data object */ $do->populate($data, $this->_mapper); $do->resetChangedState(); return true; }