public function listTopCategories() { $cm = $this->_mappers->create('Dolcore_Rdo_CategoryMapper'); $query = new Horde_Rdo_Query($cm); $query->addTest('id', 'LIKE', '_'); return $cm->find($query); }
/** * Turn any of the acceptable query shorthands into a full * Horde_Rdo_Query object. If you pass an existing Horde_Rdo_Query * object in, it will be cloned before it's returned so that it * can be safely modified. * * @todo Make $mapper non-optional in H6 * * @param mixed $query The query to convert to an object. * @param Horde_Rdo_Mapper $mapper The Mapper object governing this query. * * @return Horde_Rdo_Query The full Horde_Rdo_Query object. */ public static function create($query, $mapper = null) { if ($query instanceof Horde_Rdo_Query) { $query = clone $query; if (!is_null($mapper)) { $query->setMapper($mapper); } return $query; } $q = new Horde_Rdo_Query($mapper); if (is_scalar($query)) { $q->addTest($mapper->tableDefinition->getPrimaryKey(), '=', $query); } elseif ($query) { $q->combineWith('AND'); foreach ($query as $key => $value) { $q->addTest($key, '=', $value); } } return $q; }
/** * List discussions, filtered by various criteria * @params array $filters The array of filters allowed keys * 'state' => array('Y', 'D', 'R') * * Allowed states * Y => OK, * D => Declined * R => awaiting approval * defaults to 'R' - passing an empty array means 'all' * 'category' => The category id to filter for * 'user' => A user's numeric id to filter for */ public function listDiscussions(array $filters = array()) { $filters = array_merge(array('state' => array('Y'), 'limit' => array()), $filters); $dm = $this->_mappers->create('Dolcore_Rdo_DiscussionMapper'); $query = new Horde_Rdo_Query($dm); if ($filters['category']) { $query->addTest('kategorie_id', '=', $filters['category']); } if ($filters['user']) { $query->addTest('benutzer_id', '=', $filters['user']); } if (count($filters['state'])) { $query->addTest('checked', 'IN', $filters['state']); } $query->sortBy('erstelldatum DESC'); return $dm->find($query); }
/** * Fetch fields that haven't yet been loaded. Lazy-loaded fields * and lazy-loaded relationships are handled this way. Once a * field is retrieved, it is cached in the $_fields array so it * doesn't need to be fetched again. * * @param string $field The name of the field to access. * * @return mixed The value of $field or null. */ public function __get($field) { // Honor any explicit getters. $fieldMethod = 'get' . Horde_String::ucfirst($field); // If an Rdo_Base subclass has a __call() method, is_callable // returns true on every method name, so use method_exists // instead. if (method_exists($this, $fieldMethod)) { return call_user_func(array($this, $fieldMethod)); } if (isset($this->_fields[$field])) { return $this->_fields[$field]; } $mapper = $this->getMapper(); // Look for lazy fields first, then relationships. if (in_array($field, $mapper->lazyFields)) { // @TODO Support composite primary keys $query = new Horde_Rdo_Query($mapper); $query->setFields($field)->addTest($mapper->primaryKey, '=', $this->{$mapper->primaryKey}); list($sql, $params) = $query->getQuery(); $this->_fields[$field] = $mapper->adapter->selectValue($sql, $params); return $this->_fields[$field]; } elseif (isset($mapper->lazyRelationships[$field])) { $rel = $mapper->lazyRelationships[$field]; } else { return null; } // Try to find the Mapper class for the object the // relationship is with, and fail if we can't. if (isset($rel['mapper'])) { if ($mapper->factory) { $m = $mapper->factory->create($rel['mapper']); } else { // @TODO - should be getting this instance from somewhere // else external, and not passing the adapter along // automatically. $m = new $rel['mapper']($mapper->adapter); } } else { $m = $mapper->tableToMapper($field); if (is_null($m)) { return null; } } // Based on the kind of relationship, fetch the appropriate // objects and fill the cache. switch ($rel['type']) { case Horde_Rdo::ONE_TO_ONE: case Horde_Rdo::MANY_TO_ONE: if (isset($rel['query'])) { $query = $this->_fillPlaceholders($rel['query']); $this->_fields[$field] = $m->findOne($query); } elseif (!empty($this->{$rel['foreignKey']})) { $this->_fields[$field] = $m->findOne($this->{$rel['foreignKey']}); if (empty($this->_fields[$field])) { throw new Horde_Rdo_Exception('The referenced object with key ' . $this->{$rel['foreignKey']} . ' does not exist. Your data is inconsistent'); } } else { $this->_fields[$field] = null; } break; case Horde_Rdo::ONE_TO_MANY: $this->_fields[$field] = $m->find(array($rel['foreignKey'] => $this->{$rel['foreignKey']})); break; case Horde_Rdo::MANY_TO_MANY: $key = $mapper->primaryKey; $query = new Horde_Rdo_Query(); $on = isset($rel['on']) ? $rel['on'] : $m->primaryKey; $query->addRelationship($field, array('mapper' => $mapper, 'table' => $rel['through'], 'type' => Horde_Rdo::MANY_TO_MANY, 'query' => array("{$m->table}.{$on}" => new Horde_Rdo_Query_Literal($rel['through'] . '.' . $on), $key => $this->{$key}))); $this->_fields[$field] = $m->find($query); break; } return $this->_fields[$field]; }
/** * findOne can be called in several ways. * * Primary key mode: pass find() a single primary key, and it will return a * single object matching that primary key. * * If you pass findOne() no arguments, the first object of this type will be * returned. * * If you pass findOne() an associative array, it will be turned into a * Horde_Rdo_Query object. * * If you pass findOne() a Horde_Rdo_Query, it will return the first object * matching that query. */ public function findOne($arg = null) { if (is_null($arg)) { $query = null; } elseif (is_scalar($arg)) { $query = array($this->primaryKey => $arg); } else { $query = $arg; } // Build a full Query object, and limit it to one result. $query = Horde_Rdo_Query::create($query, $this); $query->limit(1); $list = new Horde_Rdo_List($query); return $list->current(); }
/** * Inventory search * @param array filters a list of filter hashes, each having keys * string type ('note', 'stock_name', 'stock_id', 'categories', 'values') * string test * boolean exact (only search for full words, default to null) * mixed value (string for note, stock_name) * For the 'values' structure, value, value is a map of [values] and optional [property]} * @return array List of Stock items */ public function findStock($filters = array()) { $sm = $this->_mappers->create('Sesha_Entity_StockMapper'); if (empty($filters)) { return iterator_to_array($sm->find()); } $query = new Horde_Rdo_Query($sm); $query->combineWith('OR'); foreach ($filters as $filter) { switch ($filter['type']) { case 'note': case 'stock_name': case 'stock_id': $filter_values = is_array($filter['value']) ? $filter['value'] : array($filter['value']); $filter_test = empty($filter['test']) ? 'LIKE' : $filter['test']; $filter_field = $filter['type']; if ($filter_field == 'stock_id') { $filter_test = '='; } foreach ($filter_values as $filter_value) { if (strlen($filter_value)) { if ($filter_test == 'LIKE' && empty($filter['exact'])) { $filter_value = '%' . $filter_value . '%'; } $query->addTest($filter_field, $filter_test, $filter_value); } } break; case 'categories': $cm = $this->_mappers->create('Sesha_Entity_CategoryMapper'); $categories = is_array($filter['value']) ? $filter['value'] : array($filter['value']); $items = array(); foreach ($categories as $category) { if ($category instanceof Sesha_Entity_Category) { $category_id = $category->category_id; } else { $category_id = $category; $category = $cm->findOne($category_id); } foreach ($category->stock as $item) { /* prevent duplicates when an item has several * categories */ $items[$item->stock_id] = $item; } } if (count($filters == 1)) { return $items; } $query->addTest('stock_id', $filter['test'] ? $filter['test'] : 'IN', array_keys($items)); break; case 'values': $vm = $this->_mappers->create('Sesha_Entity_ValueMapper'); $items = array(); foreach ($filter['value'] as $propTest) { $values = is_array($propTest['values']) ? $propTest['values'] : array($propTest['values']); // Find all Value objects which match any of the $value[values] foreach ($values as $filter_value) { $valueQuery = new Horde_Rdo_Query($vm); if ($propTest['property']) { $valueQuery->addTest('property_id', '=', $propTest['property']); } if (empty($filter['exact'])) { $filter_value = '%' . $filter_value . '%'; } $valueQuery->addTest('txt_datavalue', 'LIKE', $filter_value); foreach ($vm->find($valueQuery) as $value) { // prevent doubles $items[$value->stock_id] = $value->stock; } } } if (count($filters == 1)) { return $items; } $query->addTest('stock_id', $filter['test'] ? $filter['test'] : 'IN', array_keys($items)); break; } } return iterator_to_array($sm->find($query)); }
public function testLimit() { $query = Horde_Rdo_Query::create(4, $this->mapper); $query->limit(10); $this->assertEquals(array('SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? LIMIT 10', array(4)), $query->getQuery()); $query->limit(10, 20); $this->assertEquals(array('SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? LIMIT 20, 10', array(4)), $query->getQuery()); }
/** * get any number of pastes from a bin, ordered by date, narrowed by limit and offset * @param string $bin A paste bin to query * @param integer $limit a maximum of pastes to retrieve (optional, default to null = all) * @param integer start a number of pastes to skip before retrieving (optional, default to null = begin with first) * @return array a list of pastes */ public function getPastes($bin, $limit = null, $start = null) { $pm = $this->_mappers->create('Pastie_Entity_PasteMapper'); $query = new Horde_Rdo_Query($pm); $query->sortBy('paste_timestamp DESC'); if ($limit !== null) { if ($start === null) { $start = 0; } $query->limit($limit, $start); } $pastes = array(); foreach ($pm->find($query) as $paste) { $pastes[$paste['paste_uuid']] = $this->_fromBackend($paste); } return $pastes; }
/** * Implementation of the offsetGet() method for ArrayAccess * This method is executed when using isset() or empty() on Horde_Rdo_List objects * @param integer $offset The offset to retrieve. * * @return Horde_Rdo_Base An entity object at the offset position or null */ public function offsetGet($offset) { $query = Horde_Rdo_Query::create($this->_query); $query->limit(1, $offset); return $this->_mapper->find($query)->current(); }