Example #1
0
 /**
  * Populate the given collection from the array of identifiers and the uri base
  * 
  * @param array $ids
  * @param \t41\ObjectModel\Collection $collection
  * @param \t41\ObjectModel\ObjectUri $uriBase
  */
 protected function _populateCollection(array $ids, ObjectModel\Collection $collection, ObjectModel\ObjectUri $uriBase)
 {
     if (count($ids) == 0) {
         return array();
     }
     if (count($ids[0]) > 1) {
         $do = clone $collection->getDataObject();
     }
     $class = $collection->getDataObject()->getClass();
     // populate array with relevant objects type
     $array = array();
     /**
      * If data object has been modified (meta property added) we need to use it to populate objects
      */
     if ($collection->getParameter('memberType') != ObjectModel::URI && $collection->getDataObject()->getDna('custom')) {
         $do = clone $collection->getDataObject();
     }
     foreach ($ids as $key => $id) {
         $uri = clone $uriBase;
         if (is_array($id)) {
             $uri->setUrl($uri->getUrl() . $id['id'])->setIdentifier($id['id']);
             unset($id['id']);
         } else {
             $uri->setUrl($uri->getUrl() . $id)->setIdentifier($id);
         }
         if (isset($do)) {
             $obj = clone $do;
             $obj->setUri($uri);
         } else {
             unset($obj);
         }
         switch ($collection->getParameter('memberType')) {
             case ObjectModel::URI:
                 $obj = $uri;
                 break;
             case ObjectModel::MODEL:
                 if (isset($obj) || is_array($ids)) {
                     Backend::read($obj, null, $id);
                 } else {
                     $obj = Core::_($uri, $class);
                 }
                 break;
             case ObjectModel::DATA:
             default:
                 if (isset($obj) || is_array($ids)) {
                     Backend::read($obj, null, $id);
                 } else {
                     $obj = Core::_($uri, $class);
                 }
                 $obj = $obj->getDataObject();
                 break;
         }
         $array[$key] = $obj;
     }
     return $array;
 }
Example #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;
 }
Example #3
0
 public function returnsDistinct(ObjectModel\Collection $collection, ObjectModel\Property\PropertyAbstract $property)
 {
     $class = $collection->getDataObject()->getClass();
     $mode = $collection->getParameter('memberType');
     // set database to use
     $this->_selectDatabase();
     // get collection to use, from mapper if available, else from data object
     $collec = $this->_mapper instanceof Backend\Mapper ? $this->_mapper->getDatastore($class) : $class;
     //$collec = $this->_db->selectCollection($collec);
     $conditions = array();
     /* @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();
         }
         $conditions += $this->_buildConditionStatement($field, $condition->getClauses(), $conditions);
         switch ($conditionArray[1]) {
             case 'OR':
                 //$select->orWhere($statement);
                 break;
             case 'AND':
             default:
                 //$select->where($statement);
                 break;
         }
     }
     $params = array();
     $params['distinct'] = $collec;
     $params['key'] = $property->getId();
     $params['query'] = $conditions;
     $this->_setLastQuery('command', $params);
     $ids = $this->_db->command($params);
     /* @todo if property is an object, we should get all values from the list of ids */
     return isset($ids['values']) ? $ids['values'] : array();
 }
Example #4
0
 /**
  * Populate the given collection from the array of identifiers and the uri base
  * 
  * @param array $ids
  * @param \t41\ObjectModel\Collection $collection
  * @param \t41\ObjectModel\ObjectUri $uriBase
  */
 protected function _populateCollection(array $ids, ObjectModel\Collection $collection, ObjectModel\ObjectUri $uriBase)
 {
     $class = $collection->getDataObject()->getClass();
     // populate array with relevant objects type
     $array = array();
     if ($collection->getParameter('memberType') != ObjectModel::URI) {
         $do = clone $collection->getDataObject();
     }
     foreach ($ids as $id) {
         $uri = clone $uriBase;
         $uri->setUrl($uri->getUrl() . $id)->setIdentifier($id);
         switch ($collection->getParameter('memberType')) {
             case ObjectModel::URI:
                 $obj = $uri;
                 break;
             case ObjectModel::MODEL:
                 $obj = clone $do;
                 $obj->setUri($uri);
                 $this->read($obj);
                 $obj = new $class(null, null, $obj);
                 break;
             case ObjectModel::DATA:
             default:
                 $obj = clone $do;
                 $obj->setUri($uri);
                 $this->read($obj);
                 break;
         }
         $array[] = $obj;
     }
     return $array;
 }