示例#1
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, $returnCount = false)
 {
     $class = $collection->getDataObject()->getClass();
     $table = $this->_getTableFromClass($class);
     if (!$table) {
         throw new Exception('BACKEND_MISSING_DBTABLE_PARAM');
     }
     // primary key is either part of the mapper configuration or 'id'
     //$pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : 'id';
     //$pkey = $table . '.' . $pkey;
     // primary key is either part of the mapper configuration or 'id'
     $pkey = $this->_mapper ? $this->_mapper->getPrimaryKey($class) : \t41\Backend::DEFAULT_PKEY;
     if (is_array($pkey)) {
         $composite = array();
         /* @var $obj t41_Backend_Key */
         foreach ($pkey as $obj) {
             $composite[] = sprintf('TRIM(%s)', $table . '.' . $obj->getName());
         }
         $pkey = sprintf("%s", implode(',', $composite));
     } else {
         $pkey = $table . '.' . $pkey;
     }
     $this->_connect();
     /* @var $select Zend_Db_Select */
     $select = $this->_ressource->select();
     //$select->from($table, $returnCount ? new Zend_Db_Expr("COUNT($pkey)") : $pkey);
     $select->from($table, $returnCount ? new \Zend_Db_Expr("COUNT(*) AS " . \t41\Backend::MAX_ROWS_IDENTIFIER) : $pkey);
     /* @var $condition t41_Condition */
     foreach ($collection->getConditions() as $conditionArray) {
         $jtable = '';
         $class = $collection->getDataObject()->getClass();
         $condition = $conditionArray[0];
         /* does condition contain another condition object ? */
         if ($condition->isRecursive()) {
             while ($condition->isRecursive()) {
                 $property = $condition->getProperty();
                 $parent = $property->getParent() ? $property->getParent()->getId() : $table;
                 $condition = $condition->getCondition();
                 $jtable = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $property->getParameter('instanceof');
                 $jpkey = $this->_mapper ? $this->_mapper->getPrimaryKey($property->getParameter('instanceof')) : 'id';
                 $parentTable = $this->_mapper ? $this->_mapper->getDatastore($parent) : $parent;
                 $join = sprintf("%s.%s = %s.%s", $parentTable, $jpkey, $jtable, $jpkey);
                 $select->joinLeft($jtable, $join, array());
                 $class = $property->getParameter('instanceof');
             }
         }
         $property = $condition->getProperty();
         if ($property instanceof Property\ObjectProperty) {
             $jtable = $this->_mapper ? $this->_mapper->getDatastore($property->getParameter('instanceof')) : $property->getParameter('instanceof');
             $leftkey = $this->_mapper ? $this->_mapper->propertyToDatastoreName($class, $property->getId()) : $property->getId();
             $field = $rightkey = $this->_mapper ? $this->_mapper->getPrimaryKey($property->getParameter('instanceof')) : 'id';
             $join = sprintf("%s.%s = %s.%s", $table, $leftkey, $jtable, $rightkey);
             $select->joinLeft($jtable, $join, array());
         } else {
             $field = $property->getId();
             if ($this->_mapper) {
                 $field = $this->_mapper->propertyToDatastoreName($class, $field);
             }
         }
         /* if a join was performed, prefix current field with table name */
         if ($jtable) {
             $field = $jtable . '.' . $field;
         }
         $statement = $this->_buildConditionStatement($field, $condition->getClauses());
         switch ($conditionArray[1]) {
             case 'OR':
                 $select->orWhere($statement);
                 break;
             case 'AND':
             default:
                 $select->where($statement);
                 break;
         }
     }
     if ($returnCount != true) {
         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();
             }
             $select->order($field, $sorting[1]);
         }
         $select->limit($collection->getBoundaryBatch(), $collection->getBoundaryOffset());
     }
     //		echo $select; die;
     $result = array();
     $context = array('table' => $table);
     try {
         $result = $this->_ressource->fetchAll($select);
     } catch (\Zend_Db_Exception $e) {
         $context['error'] = $e->getMessage();
     }
     $this->_setLastQuery($select->__toString(), $select->getPart('where'), $context);
     if ($returnCount == true) {
         return $result[0][\t41\Backend::MAX_ROWS_IDENTIFIER];
     }
     // convert array of primary keys to strings
     foreach ($result as $key => $val) {
         $result[$key] = implode(\t41\Mapper::VALUES_SEPARATOR, $val);
     }
     /* prepare base of object uri */
     $uri = new ObjectModel\ObjectUri();
     $uri->setBackendUri($this->_uri);
     $uri->setClass($collection->getDataObject()->getClass());
     $uri->setUrl($this->_database . '/' . $table . '/');
     return $this->_populateCollection($result, $collection, $uri);
 }
示例#2
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;
 }