Esempio n. 1
0
 /**
  * Save new set of data from a t41_Data_Object object using INSERT 
  *
  * @param t41_Data_Object $do
  * @return boolean
  * @throws t41_Backend_Exception
  */
 public function create(ObjectModel\DataObject $do)
 {
     // set database to use
     $this->_selectDatabase($do->getUri());
     // get collection to use, from mapper if available, else from data object
     $collection = $this->_mapper instanceof Backend\Mapper ? $this->_mapper->getDatastore($do->getClass()) : $do->getClass();
     $collec = $this->_db->selectCollection($collection);
     // get a valid data array passing mapper if any
     if ($this->_mapper) {
         $recordSet = $do->map($this->_mapper, 'backend', $this->_uri->getAlias());
     } else {
         $recordSet = $do->toArray($this->_uri->getAlias());
     }
     /* @todo check wether... */
     try {
         $collec->insert($recordSet);
     } catch (\Exception $e) {
         // @todo decide whether to throw an exception or just save last message in a property
         die($e->getMessage());
         return false;
     }
     // inject new t41_Object_Uri object in data object
     $uri = Backend::PREFIX . $this->_uri->getAlias() . '/' . $this->_database . '/' . $collection . '/' . $recordSet['_id']->__toString();
     $uri = new ObjectModel\ObjectUri($uri, $this->_uri);
     $do->setUri($uri);
     return true;
 }
Esempio n. 2
0
 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;
 }
Esempio n. 3
0
 /**
  * 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;
 }
Esempio n. 4
0
 /**
  * Save new set of data from a t41_Data_Object object using INSERT 
  *
  * @param \t41\ObjectModel\DataObject $do
  * @return boolean
  * @throws \t41\Backend\Adapter\Exception
  */
 public function create(ObjectModel\DataObject $do)
 {
     $table = $this->_getTableFromClass($do->getClass());
     if (!$table) {
         throw new Exception('BACKEND_MISSING_DBTABLE_PARAM');
     }
     // get a valid data array passing mapper if any
     if ($this->_mapper) {
         $recordSet = $do->map($this->_mapper, $this);
     } else {
         $recordSet = $do->toArray($this);
     }
     //			Zend_Debug::dump($recordSet);
     $this->_setLastQuery('insert', $recordSet);
     try {
         $this->_connect();
         $this->_ressource->insert(isset($table) ? $table : $do->getClass(), $recordSet['data']);
     } catch (Exception $e) {
         if (true) {
             throw new Exception("Error Creating Record: " . $e->getMessage);
         } else {
             return false;
         }
     }
     // inject new t41_Object_Uri object in data object
     // @todo provide support for primary keys that are not generated by DB (not AUTO INCREMENTED INTEGER)
     $id = $this->_ressource->lastInsertId();
     $uri = $id;
     if (!$this->_mapper instanceof Backend\Mapper) {
         $uri = $table . '/' . $uri;
     }
     $uri = new ObjectModel\ObjectUri($uri);
     $do->setUri($uri);
     /* get collection handling properties (if any) and process them */
     foreach ($do->getProperties() as $property) {
         if (!$property instanceof Property\CollectionProperty) {
             continue;
         }
         $collection = $property->getValue();
         //var_dump($collection->getMembers());
         /* @var $member t41_Object_Model */
         foreach ($collection->getMembers() as $member) {
             $member->setProperty($property->getParameter('keyprop'), $uri);
             $member->save();
         }
     }
     return true;
 }