/** * Returns a database iterator to retrieve records. Use the scope methods and the whereRaw method to define what * exactly will be returned. * * @param integer $limitstart How many items to skip from the start, only when $overrideLimits = true * @param integer $limit How many items to return, only when $overrideLimits = true * * @return F0FDatabaseIterator The data collection */ public function get($limitstart = 0, $limit = 0) { $limitstart = max($limitstart, 0); $limit = max($limit, 0); $query = $this->buildQuery(); $db = $this->getDbo(); $db->setQuery($query, $limitstart, $limit); $cursor = $db->execute(); $dataCollection = F0FDatabaseIterator::getIterator($db->name, $cursor, null, $this->config['_table_class']); return $dataCollection; }
/** * Returns a F0FDatabaseIterator over a list of items. * * THERE BE DRAGONS. Unlike the getItemList() you have a few restrictions: * - The onProcessList event does not run when you get an iterator * - The Iterator returns F0FTable instances. By default, $this->table is used. If you have JOINs, GROUPs or a * complex query in general you will need to create a custom F0FTable subclass and pass its type in $tableType. * * The getIterator() method is a great way to sift through a large amount of records which would otherwise not fit * in memory since it only keeps one record in PHP memory at a time. It works best with simple models, returning * all the contents of a single database table. * * @param boolean $overrideLimits Should I ignore set limits? * @param string $tableClass The table class for the iterator, e.g. FoobarTableBar. Leave empty to use * the default Table class for this Model. * * @return F0FDatabaseIterator */ public function &getIterator($overrideLimits = false, $tableClass = null) { // Get the table name (required by the Iterator) if (empty($tableClass)) { $name = $this->table; if (empty($name)) { $name = F0FInflector::singularize($this->getName()); } $bareComponent = str_replace('com_', '', $this->option); $prefix = ucfirst($bareComponent) . 'Table'; $tableClass = $prefix . ucfirst($name); } // Get the query $query = $this->buildQuery($overrideLimits); // Apply limits if ($overrideLimits) { $limitStart = 0; $limit = 0; } else { $limitStart = $this->getState('limitstart'); $limit = $this->getState('limit'); } // This is required to prevent one relation from killing the db cursor used in a different relation... $oldDb = $this->getDbo(); $oldDb->disconnect(); // YES, WE DO NEED TO DISCONNECT BEFORE WE CLONE THE DB OBJECT. ARGH! $db = clone $oldDb; // Execute the query, get a db cursor and return the iterator $db->setQuery($query, $limitStart, $limit); $cursor = $db->execute(); $iterator = F0FDatabaseIterator::getIterator($db->name, $cursor, null, $tableClass); return $iterator; }
/** * Returns a F0FDatabaseIterator based on a given relation * * @param array $relation Indexed array holding relation definition. * tableClass => name of the related table class * localKey => name of the local key * remoteKey => name of the remote key * pivotTable => name of the pivot table (optional) * theirPivotKey => name of the remote key in the pivot table (mandatory if pivotTable is set) * ourPivotKey => name of our key in the pivot table (mandatory if pivotTable is set) * * @return F0FDatabaseIterator * * @throws RuntimeException * @throws InvalidArgumentException */ protected function getIteratorFromRelation($relation) { // Sanity checks if (!isset($relation['tableClass']) || !isset($relation['remoteKey']) || !isset($relation['localKey']) || !$relation['tableClass'] || !$relation['remoteKey'] || !$relation['localKey']) { throw new InvalidArgumentException('Missing array index for the ' . __METHOD__ . ' method. Please check method signature', 500); } if (array_key_exists('pivotTable', $relation)) { if (!isset($relation['theirPivotKey']) || !isset($relation['ourPivotKey']) || !$relation['pivotTable'] || !$relation['theirPivotKey'] || !$relation['ourPivotKey']) { throw new InvalidArgumentException('Missing array index for the ' . __METHOD__ . ' method. Please check method signature', 500); } } // Get a table object from the table class name $tableClass = $relation['tableClass']; $tableClassParts = F0FInflector::explode($tableClass); if (count($tableClassParts) < 3) { throw new InvalidArgumentException('Invalid table class named. It should be something like FooTableBar'); } $table = F0FTable::getInstance($tableClassParts[2], ucfirst($tableClassParts[0]) . ucfirst($tableClassParts[1])); // Get the table name $tableName = $table->getTableName(); // Get the remote and local key names $remoteKey = $relation['remoteKey']; $localKey = $relation['localKey']; // Get the local key's value $value = $this->table->{$localKey}; // If there's no value for the primary key, let's stop here if (!$value) { throw new RuntimeException('Missing value for the primary key of the table ' . $this->table->getTableName(), 500); } // This is required to prevent one relation from killing the db cursor used in a different relation... $oldDb = $this->table->getDbo(); $oldDb->disconnect(); // YES, WE DO NEED TO DISCONNECT BEFORE WE CLONE THE DB OBJECT. ARGH! $db = clone $oldDb; // Begin the query $query = $db->getQuery(true)->select('*')->from($db->qn($tableName)); // Do we have a pivot table? $hasPivot = array_key_exists('pivotTable', $relation); // If we don't have pivot it's a straightforward query if (!$hasPivot) { $query->where($db->qn($remoteKey) . ' = ' . $db->q($value)); } else { $subQuery = $db->getQuery(true)->select($db->qn($relation['theirPivotKey']))->from($db->qn($relation['pivotTable']))->where($db->qn($relation['ourPivotKey']) . ' = ' . $db->q($value)); $query->where($db->qn($remoteKey) . ' IN (' . $subQuery . ')'); } $db->setQuery($query); $cursor = $db->execute(); $iterator = F0FDatabaseIterator::getIterator($db->name, $cursor, null, $tableClass); return $iterator; }