/** * Remove a filter from the query * * @param string|array $fieldExpression The predicate of the condition to remove * (ignoring parameters). The expression will be considered a match if it's * contained within any other predicate. * @return DataQuery Self reference */ public function removeFilterOn($fieldExpression) { $matched = false; // If given a parameterised condition extract only the condition if (is_array($fieldExpression)) { reset($fieldExpression); $fieldExpression = key($fieldExpression); } $where = $this->query->getWhere(); // Iterate through each condition foreach ($where as $i => $condition) { // Rewrite condition groups as plain conditions before comparison if ($condition instanceof SQLConditionGroup) { $predicate = $condition->conditionSQL($parameters); $condition = array($predicate => $parameters); } // As each condition is a single length array, do a single // iteration to extract the predicate and parameters foreach ($condition as $predicate => $parameters) { // @see SQLSelect::addWhere for why this is required here if (strpos($predicate, $fieldExpression) !== false) { unset($where[$i]); $matched = true; } // Enforce single-item condition predicate => parameters structure break; } } // set the entire where clause back, but clear the original one first if ($matched) { $this->query->setWhere($where); } else { throw new InvalidArgumentException("Couldn't find {$fieldExpression} in the query filter."); } return $this; }
/** * Find the extra field data for a single row of the relationship join * table, given the known child ID. * * @param string $componentName The name of the component * @param int $itemID The ID of the child for the relationship * * @return array Map of fieldName => fieldValue */ public function getExtraData($componentName, $itemID) { $result = array(); // Skip if no extrafields or unsaved record if (empty($this->extraFields) || empty($itemID)) { return $result; } if (!is_numeric($itemID)) { user_error('ComponentSet::getExtraData() passed a non-numeric child ID', E_USER_ERROR); } $cleanExtraFields = array(); foreach ($this->extraFields as $fieldName => $dbFieldSpec) { $cleanExtraFields[] = "\"{$fieldName}\""; } $query = new SQLSelect($cleanExtraFields, "\"{$this->joinTable}\""); $filter = $this->foreignIDWriteFilter($this->getForeignID()); if ($filter) { $query->setWhere($filter); } else { user_error("Can't call ManyManyList::getExtraData() until a foreign ID is set", E_USER_WARNING); } $query->addWhere(array("\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID)); $queryResult = $query->execute()->current(); if ($queryResult) { foreach ($queryResult as $fieldName => $value) { $result[$fieldName] = $value; } } return $result; }
public function testSelectLast() { // Test last in sequence $query = new SQLSelect(); $query->setFrom('"SQLSelectTest_DO"'); $query->setOrderBy('"Name"'); $result = $query->lastRow()->execute(); $records = array(); foreach ($result as $row) { $records[] = $row; } $this->assertCount(1, $records); $this->assertEquals('Object 2', $records[0]['Name']); // Test last from empty sequence $query = new SQLSelect(); $query->setFrom('"SQLSelectTest_DO"'); $query->setOrderBy('"Name"'); $query->setWhere(array("\"Name\" = 'Nonexistent Object'")); $result = $query->lastRow()->execute(); $records = array(); foreach ($result as $row) { $records[] = $row; } $this->assertCount(0, $records); // Test that given the first item, the 'last' in this list matches the first $query = new SQLSelect(); $query->setFrom('"SQLSelectTest_DO"'); $query->setOrderBy('"Name"'); $query->setLimit(1); $result = $query->lastRow()->execute(); $records = array(); foreach ($result as $row) { $records[] = $row; } $this->assertCount(1, $records); $this->assertEquals('Object 1', $records[0]['Name']); }