mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
27113f82c3
In 3.0 there was some confusion about whether DataLists and ArrayLists were mutable or not. If DataLists were immutable, they'd return the result, and your code would look like $list = $list->filter(....); If DataLists were mutable, they'd operate on themselves, returning nothing, and your code would look like $list->filter(....); This makes all DataLists and ArrayList immutable for all _searching_ operations. Operations on DataList that modify the underlying SQL data store remain mutating. - These functions no longer mutate the existing object, and if you do not capture the value returned by them will have no effect: ArrayList#reverse ArrayList#sort ArrayList#filter ArrayList#exclude DataList#dataQuery (use DataList#alterDataQuery to modify dataQuery in a safe manner) DataList#where DataList#limit DataList#sort DataList#addFilter DataList#applyFilterContext DataList#innerJoin DataList#leftJoin DataList#find DataList#byIDs DataList#reverse - DataList#setDataQueryParam has been added as syntactic sugar around the most common cause of accessing the dataQuery directly - setting query parameters - RelationList#setForeignID has been removed. Always use RelationList#forForeignID when querying, and overload RelationList#foreignIDList when subclassing. - Relatedly,the protected variable RelationList->foreignID has been removed, as the ID is now stored on a query parameter. Use RelationList#getForeignID to read it.
51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* A DataList that represents a relation.
|
|
* Adds the notion of a foreign ID that can be optionally set.
|
|
*
|
|
* @todo Is this additional class really necessary?
|
|
*/
|
|
abstract class RelationList extends DataList {
|
|
|
|
public function getForeignID() {
|
|
return $this->dataQuery->getQueryParam('Foreign.ID');
|
|
}
|
|
|
|
/**
|
|
* Returns a copy of this list with the ManyMany relationship linked to the given foreign ID.
|
|
* @param $id An ID or an array of IDs.
|
|
*/
|
|
public function forForeignID($id) {
|
|
// Turn a 1-element array into a simple value
|
|
if(is_array($id) && sizeof($id) == 1) $id = reset($id);
|
|
|
|
// Calculate the new filter
|
|
$filter = $this->foreignIDFilter($id);
|
|
|
|
$list = $this->alterDataQuery(function($query, $list) use ($id, $filter){
|
|
// Check if there is an existing filter, remove if there is
|
|
$currentFilter = $query->getQueryParam('Foreign.Filter');
|
|
if($currentFilter) {
|
|
try {
|
|
$query->removeFilterOn($currentFilter);
|
|
}
|
|
catch (Exception $e) { /* NOP */ }
|
|
}
|
|
|
|
// Add the new filter
|
|
$query->setQueryParam('Foreign.ID', $id);
|
|
$query->setQueryParam('Foreign.Filter', $filter);
|
|
$query->where($filter);
|
|
});
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* Returns a where clause that filters the members of this relationship to just the related items
|
|
* @param $id (optional) An ID or an array of IDs - if not provided, will use the current ids as per getForeignID
|
|
*/
|
|
abstract protected function foreignIDFilter($id = null);
|
|
}
|