2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
2016-08-29 07:00:27 +02:00
|
|
|
use BadMethodCallException;
|
|
|
|
use SearchFilter;
|
|
|
|
use SilverStripe\ORM\Queries\SQLConditionGroup;
|
2016-06-15 06:03:16 +02:00
|
|
|
use ViewableData;
|
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Injector;
|
|
|
|
use LogicException;
|
|
|
|
use Debug;
|
|
|
|
use ArrayIterator;
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Implements a "lazy loading" DataObjectSet.
|
|
|
|
* Uses {@link DataQuery} to do the actual query generation.
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
2012-12-12 05:22:45 +01:00
|
|
|
* DataLists are _immutable_ as far as the query they represent is concerned. When you call a method that
|
|
|
|
* alters the query, a new DataList instance is returned, rather than modifying the existing instance
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
2012-12-12 05:22:45 +01:00
|
|
|
* When you add or remove an element to the list the query remains the same, but because you have modified
|
|
|
|
* the underlying data the contents of the list changes. These are some of those methods:
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
2012-12-12 05:22:45 +01:00
|
|
|
* - add
|
|
|
|
* - addMany
|
|
|
|
* - remove
|
|
|
|
* - removeMany
|
|
|
|
* - removeByID
|
|
|
|
* - removeByFilter
|
|
|
|
* - removeAll
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
2012-12-12 05:22:45 +01:00
|
|
|
* Subclasses of DataList may add other methods that have the same effect.
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-03-09 02:28:14 +01:00
|
|
|
class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortable, SS_Limitable {
|
2016-08-29 07:00:27 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* The DataObject class name that this data list is querying
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @var string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected $dataClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* The {@link DataQuery} object responsible for getting this DataList's records
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @var DataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected $dataQuery;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* The DataModel from which this DataList comes.
|
2013-10-30 14:08:55 +01:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @var DataModel
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
|
|
|
protected $model;
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new DataList.
|
|
|
|
* No querying is done on construction, but the initial query schema is set up.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $dataClass - The DataObject class to query.
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function __construct($dataClass) {
|
|
|
|
$this->dataClass = $dataClass;
|
|
|
|
$this->dataQuery = new DataQuery($this->dataClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the DataModel
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param DataModel $model
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2012-05-01 04:43:52 +02:00
|
|
|
public function setDataModel(DataModel $model) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = $model;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Get the dataClass name for this DataList, ie the DataObject ClassName
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-11-22 06:16:38 +01:00
|
|
|
public function dataClass() {
|
|
|
|
return $this->dataClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* When cloning this object, clone the dataQuery object as well
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function __clone() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->dataQuery = clone $this->dataQuery;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a copy of the internal {@link DataQuery} object
|
|
|
|
*
|
|
|
|
* Because the returned value is a copy, modifying it won't affect this list's contents. If
|
|
|
|
* you want to alter the data query directly, use the alterDataQuery method
|
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return DataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function dataQuery() {
|
2012-12-12 05:22:45 +01:00
|
|
|
return clone $this->dataQuery;
|
2012-07-20 05:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool - Indicates if we are in an alterDataQueryCall already, so alterDataQuery can be re-entrant
|
|
|
|
*/
|
|
|
|
protected $inAlterDataQueryCall = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new DataList instance with the underlying {@link DataQuery} object altered
|
|
|
|
*
|
|
|
|
* If you want to alter the underlying dataQuery for this list, this wrapper method
|
|
|
|
* will ensure that you can do so without mutating the existing List object.
|
|
|
|
*
|
|
|
|
* It clones this list, calls the passed callback function with the dataQuery of the new
|
|
|
|
* list as it's first parameter (and the list as it's second), then returns the list
|
|
|
|
*
|
|
|
|
* Note that this function is re-entrant - it's safe to call this inside a callback passed to
|
|
|
|
* alterDataQuery
|
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param callable $callback
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2016-05-25 07:30:01 +02:00
|
|
|
* @throws Exception
|
2012-07-20 05:58:18 +02:00
|
|
|
*/
|
|
|
|
public function alterDataQuery($callback) {
|
|
|
|
if ($this->inAlterDataQueryCall) {
|
|
|
|
$list = $this;
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$res = call_user_func($callback, $list->dataQuery, $list);
|
2012-07-20 05:58:18 +02:00
|
|
|
if ($res) $list->dataQuery = $res;
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$list = clone $this;
|
|
|
|
$list->inAlterDataQueryCall = true;
|
|
|
|
|
|
|
|
try {
|
2012-12-12 05:22:45 +01:00
|
|
|
$res = call_user_func($callback, $list->dataQuery, $list);
|
2012-07-20 05:58:18 +02:00
|
|
|
if ($res) $list->dataQuery = $res;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$list->inAlterDataQueryCall = false;
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$list->inAlterDataQueryCall = false;
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new DataList instance with the underlying {@link DataQuery} object changed
|
|
|
|
*
|
|
|
|
* @param DataQuery $dataQuery
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2012-07-20 05:58:18 +02:00
|
|
|
*/
|
|
|
|
public function setDataQuery(DataQuery $dataQuery) {
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->dataQuery = $dataQuery;
|
|
|
|
return $clone;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Returns a new DataList instance with the specified query parameter assigned
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array $keyOrArray Either the single key to set, or an array of key value pairs to set
|
|
|
|
* @param mixed $val If $keyOrArray is not an array, this is the value to set
|
2016-02-25 05:32:41 +01:00
|
|
|
* @return static
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
2012-12-12 05:22:45 +01:00
|
|
|
public function setDataQueryParam($keyOrArray, $val = null) {
|
|
|
|
$clone = clone $this;
|
|
|
|
|
|
|
|
if(is_array($keyOrArray)) {
|
|
|
|
foreach($keyOrArray as $key => $val) {
|
|
|
|
$clone->dataQuery->setQueryParam($key, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$clone->dataQuery->setQueryParam($keyOrArray, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Returns the SQL query that will be used to get this DataList's records. Good for debugging. :-)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array $parameters Out variable for parameters required for this query
|
2016-02-25 05:32:41 +01:00
|
|
|
* @return string The resulting SQL query (may be paramaterised)
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function sql(&$parameters = array()) {
|
|
|
|
return $this->dataQuery->query()->sql($parameters);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new DataList instance with a WHERE clause added to this list's query.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Supports parameterised queries.
|
|
|
|
* See SQLSelect::addWhere() for syntax examples, although DataList
|
|
|
|
* won't expand multiple method arguments as SQLSelect does.
|
2011-04-05 13:01:57 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
|
|
|
* paramaterised queries
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-04-05 13:01:57 +02:00
|
|
|
public function where($filter) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($filter){
|
2012-07-20 05:58:18 +02:00
|
|
|
$query->where($filter);
|
|
|
|
});
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Return a new DataList instance with a WHERE clause added to this list's query.
|
|
|
|
* All conditions provided in the filter will be joined with an OR
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Supports parameterised queries.
|
|
|
|
* See SQLSelect::addWhere() for syntax examples, although DataList
|
|
|
|
* won't expand multiple method arguments as SQLSelect does.
|
|
|
|
*
|
|
|
|
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
|
|
|
* paramaterised queries
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
|
|
|
public function whereAny($filter) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($filter){
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->whereAny($filter);
|
|
|
|
});
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2011-03-30 03:19:27 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this DataList can be sorted by the given field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $fieldName
|
|
|
|
* @return boolean
|
2011-03-30 03:19:27 +02:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function canSortBy($fieldName) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->dataQuery()->query()->canSortBy($fieldName);
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2016-08-11 19:19:32 +02:00
|
|
|
* Returns true if this DataList can be filtered by the given field.
|
2011-12-06 01:56:24 +01:00
|
|
|
*
|
2016-08-11 19:19:32 +02:00
|
|
|
* @param string $fieldName (May be a related field in dot notation like Member.FirstName)
|
2011-12-06 01:56:24 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canFilterBy($fieldName) {
|
2016-08-11 19:19:32 +02:00
|
|
|
$model = singleton($this->dataClass);
|
|
|
|
$relations = explode(".", $fieldName);
|
|
|
|
// First validate the relationships
|
|
|
|
$fieldName = array_pop($relations);
|
|
|
|
foreach ($relations as $r) {
|
|
|
|
$relationClass = $model->getRelationClass($r);
|
|
|
|
if (!$relationClass) return false;
|
|
|
|
$model = singleton($relationClass);
|
|
|
|
if (!$model) return false;
|
|
|
|
}
|
|
|
|
// Then check field
|
|
|
|
if ($model->hasDatabaseField($fieldName)){
|
2011-12-06 01:56:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
2013-08-05 09:59:12 +02:00
|
|
|
* Return a new DataList instance with the records returned in this query
|
|
|
|
* restricted by a limit clause.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-03-09 02:02:37 +01:00
|
|
|
public function limit($limit, $offset = 0) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($limit, $offset){
|
2012-07-20 05:58:18 +02:00
|
|
|
$query->limit($limit, $offset);
|
|
|
|
});
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-09-03 08:53:47 +02:00
|
|
|
/**
|
|
|
|
* Return a new DataList instance with distinct records or not
|
|
|
|
*
|
|
|
|
* @param bool $value
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2014-09-03 08:53:47 +02:00
|
|
|
*/
|
|
|
|
public function distinct($value) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($value){
|
2014-09-03 08:53:47 +02:00
|
|
|
$query->distinct($value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2013-08-05 09:59:12 +02:00
|
|
|
* Return a new DataList instance as a copy of this data list with the sort
|
|
|
|
* order set.
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
|
|
|
* @see SS_List::sort()
|
2013-06-21 00:32:08 +02:00
|
|
|
* @see SQLSelect::orderby
|
2012-07-20 05:58:18 +02:00
|
|
|
* @example $list = $list->sort('Name'); // default ASC sorting
|
|
|
|
* @example $list = $list->sort('Name DESC'); // DESC sorting
|
|
|
|
* @example $list = $list->sort('Name', 'ASC');
|
2015-02-12 05:09:54 +01:00
|
|
|
* @example $list = $list->sort(array('Name'=>'ASC', 'Age'=>'DESC'));
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param String|array Escaped SQL statement. If passed as array, all keys and values are assumed to be escaped.
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
|
|
|
public function sort() {
|
2012-07-20 05:58:18 +02:00
|
|
|
$count = func_num_args();
|
|
|
|
|
|
|
|
if($count == 0) {
|
2011-12-08 22:08:46 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-20 05:58:18 +02:00
|
|
|
if($count > 2) {
|
2011-12-08 22:08:46 +01:00
|
|
|
throw new InvalidArgumentException('This method takes zero, one or two arguments');
|
|
|
|
}
|
|
|
|
|
2012-07-20 05:58:18 +02:00
|
|
|
if ($count == 2) {
|
2016-05-25 07:30:01 +02:00
|
|
|
$col = null;
|
|
|
|
$dir = null;
|
2012-07-20 05:58:18 +02:00
|
|
|
list($col, $dir) = func_get_args();
|
2015-09-17 00:19:03 +02:00
|
|
|
|
|
|
|
// Validate direction
|
|
|
|
if(!in_array(strtolower($dir),array('desc','asc'))){
|
|
|
|
user_error('Second argument to sort must be either ASC or DESC');
|
|
|
|
}
|
|
|
|
|
|
|
|
$sort = array($col => $dir);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
else {
|
|
|
|
$sort = func_get_arg(0);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query, DataList $list) use ($sort){
|
2012-07-20 05:58:18 +02:00
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
if(is_string($sort) && $sort){
|
2012-07-20 05:58:18 +02:00
|
|
|
if(stristr($sort, ' asc') || stristr($sort, ' desc')) {
|
|
|
|
$query->sort($sort);
|
|
|
|
} else {
|
2015-10-29 06:37:25 +01:00
|
|
|
$list->applyRelation($sort, $column, true);
|
2015-09-17 00:19:03 +02:00
|
|
|
$query->sort($column, 'ASC');
|
2012-07-20 05:58:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(is_array($sort)) {
|
|
|
|
// sort(array('Name'=>'desc'));
|
|
|
|
$query->sort(null, null); // wipe the sort
|
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
foreach($sort as $column => $direction) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// Convert column expressions to SQL fragment, while still allowing the passing of raw SQL
|
|
|
|
// fragments.
|
2015-10-29 06:37:25 +01:00
|
|
|
$list->applyRelation($column, $relationColumn, true);
|
2015-09-17 00:19:03 +02:00
|
|
|
$query->sort($relationColumn, $direction, false);
|
2012-07-20 05:58:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a copy of this list which only includes items with these charactaristics
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
|
|
|
* @see SS_List::filter()
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2012-07-20 05:58:18 +02:00
|
|
|
* @example $list = $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
* @example $list = $list->filter('Name', array('aziz', 'bob'); // aziz and bob in list
|
2016-03-24 22:32:49 +01:00
|
|
|
* @example $list = $list->filter(array('Name'=>'bob', 'Age'=>21)); // bob with the age 21
|
|
|
|
* @example $list = $list->filter(array('Name'=>'bob', 'Age'=>array(21, 43))); // bob with the Age 21 or 43
|
2012-09-26 23:34:00 +02:00
|
|
|
* @example $list = $list->filter(array('Name'=>array('aziz','bob'), 'Age'=>array(21, 43)));
|
|
|
|
* // aziz with the age 21 or 43 and bob with the Age 21 or 43
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
2015-05-19 03:39:39 +02:00
|
|
|
* Note: When filtering on nullable columns, null checks will be automatically added.
|
|
|
|
* E.g. ->filter('Field:not', 'value) will generate '... OR "Field" IS NULL', and
|
|
|
|
* ->filter('Field:not', null) will generate '"Field" IS NOT NULL'
|
|
|
|
*
|
2011-12-08 22:08:46 +01:00
|
|
|
* @todo extract the sql from $customQuery into a SQLGenerator class
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array Escaped SQL statement. If passed as array, all keys and values will be escaped internally
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
|
|
|
public function filter() {
|
2012-05-30 01:44:20 +02:00
|
|
|
// Validate and process arguments
|
|
|
|
$arguments = func_get_args();
|
|
|
|
switch(sizeof($arguments)) {
|
|
|
|
case 1: $filters = $arguments[0]; break;
|
|
|
|
case 2: $filters = array($arguments[0] => $arguments[1]); break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException('Incorrect number of arguments passed to filter()');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
return $this->addFilter($filters);
|
2012-05-30 01:44:20 +02:00
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
|
2012-05-30 01:44:20 +02:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new instance of the list with an added filter
|
2015-07-22 12:35:58 +02:00
|
|
|
*
|
2015-07-23 12:28:59 +02:00
|
|
|
* @param array $filterArray
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2012-05-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
public function addFilter($filterArray) {
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $this;
|
|
|
|
|
2016-08-29 07:00:27 +02:00
|
|
|
foreach($filterArray as $expression => $value) {
|
|
|
|
$filter = $this->createSearchFilter($expression, $value);
|
|
|
|
$list = $list->alterDataQuery(array($filter, 'apply'));
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
return $list;
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
/**
|
2014-11-13 21:04:29 +01:00
|
|
|
* Return a copy of this list which contains items matching any of these charactaristics.
|
2012-10-12 12:24:03 +02:00
|
|
|
*
|
2014-11-13 21:04:29 +01:00
|
|
|
* @example // only bob in the list
|
2014-08-15 08:53:05 +02:00
|
|
|
* $list = $list->filterAny('Name', 'bob');
|
2012-10-12 12:24:03 +02:00
|
|
|
* // SQL: WHERE "Name" = 'bob'
|
2014-11-13 21:04:29 +01:00
|
|
|
* @example // azis or bob in the list
|
2014-08-15 08:53:05 +02:00
|
|
|
* $list = $list->filterAny('Name', array('aziz', 'bob');
|
2012-10-12 12:24:03 +02:00
|
|
|
* // SQL: WHERE ("Name" IN ('aziz','bob'))
|
2014-11-13 21:04:29 +01:00
|
|
|
* @example // bob or anyone aged 21 in the list
|
2014-08-15 08:53:05 +02:00
|
|
|
* $list = $list->filterAny(array('Name'=>'bob, 'Age'=>21));
|
2012-10-12 12:24:03 +02:00
|
|
|
* // SQL: WHERE ("Name" = 'bob' OR "Age" = '21')
|
2014-11-13 21:04:29 +01:00
|
|
|
* @example // bob or anyone aged 21 or 43 in the list
|
2014-08-15 08:53:05 +02:00
|
|
|
* $list = $list->filterAny(array('Name'=>'bob, 'Age'=>array(21, 43)));
|
2012-10-12 12:24:03 +02:00
|
|
|
* // SQL: WHERE ("Name" = 'bob' OR ("Age" IN ('21', '43'))
|
2014-11-13 21:04:29 +01:00
|
|
|
* @example // all bobs, phils or anyone aged 21 or 43 in the list
|
2012-10-12 12:24:03 +02:00
|
|
|
* $list = $list->filterAny(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
* // SQL: WHERE (("Name" IN ('bob', 'phil')) OR ("Age" IN ('21', '43'))
|
|
|
|
*
|
|
|
|
* @todo extract the sql from this method into a SQLGenerator class
|
|
|
|
*
|
|
|
|
* @param string|array See {@link filter()}
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2012-10-12 12:24:03 +02:00
|
|
|
*/
|
|
|
|
public function filterAny() {
|
|
|
|
$numberFuncArgs = count(func_get_args());
|
|
|
|
$whereArguments = array();
|
|
|
|
|
|
|
|
if($numberFuncArgs == 1 && is_array(func_get_arg(0))) {
|
|
|
|
$whereArguments = func_get_arg(0);
|
|
|
|
} elseif($numberFuncArgs == 2) {
|
|
|
|
$whereArguments[func_get_arg(0)] = func_get_arg(1);
|
2015-07-08 16:34:21 +02:00
|
|
|
} else {
|
2012-10-12 12:24:03 +02:00
|
|
|
throw new InvalidArgumentException('Incorrect number of arguments passed to exclude()');
|
2015-07-08 16:34:21 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($whereArguments) {
|
2012-10-12 12:24:03 +02:00
|
|
|
$subquery = $query->disjunctiveGroup();
|
|
|
|
|
|
|
|
foreach($whereArguments as $field => $value) {
|
2016-08-29 07:00:27 +02:00
|
|
|
$filter = $this->createSearchFilter($field, $value);
|
2016-05-25 07:30:01 +02:00
|
|
|
$filter->apply($subquery);
|
2015-07-08 16:34:21 +02:00
|
|
|
}
|
2012-10-12 12:24:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-05-29 00:07:09 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Note that, in the current implementation, the filtered list will be an ArrayList, but this may change in a
|
|
|
|
* future implementation.
|
2013-10-30 14:08:55 +01:00
|
|
|
* @see SS_Filterable::filterByCallback()
|
|
|
|
*
|
|
|
|
* @example $list = $list->filterByCallback(function($item, $list) { return $item->Age == 9; })
|
|
|
|
* @param callable $callback
|
|
|
|
* @return ArrayList (this may change in future implementations)
|
2012-05-29 00:07:09 +02:00
|
|
|
*/
|
|
|
|
public function filterByCallback($callback) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!is_callable($callback)) {
|
2013-10-30 14:08:55 +01:00
|
|
|
throw new LogicException(sprintf(
|
|
|
|
"SS_Filterable::filterByCallback() passed callback must be callable, '%s' given",
|
|
|
|
gettype($callback)
|
|
|
|
));
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2016-05-25 07:30:01 +02:00
|
|
|
/** @var ArrayList $output */
|
2013-10-30 14:08:55 +01:00
|
|
|
$output = ArrayList::create();
|
2012-05-29 00:07:09 +02:00
|
|
|
foreach($this as $item) {
|
2016-05-25 07:30:01 +02:00
|
|
|
if(call_user_func($callback, $item, $this)) {
|
|
|
|
$output->push($item);
|
|
|
|
}
|
2012-05-29 00:07:09 +02:00
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
/**
|
|
|
|
* Given a field or relation name, apply it safely to this datalist.
|
|
|
|
*
|
|
|
|
* Unlike getRelationName, this is immutable and will fallback to the quoted field
|
|
|
|
* name if not a relation.
|
|
|
|
*
|
|
|
|
* @param string $field Name of field or relation to apply
|
|
|
|
* @param string &$columnName Quoted column name
|
2015-10-29 06:37:25 +01:00
|
|
|
* @param bool $linearOnly Set to true to restrict to linear relations only. Set this
|
|
|
|
* if this relation will be used for sorting, and should not include duplicate rows.
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this DataList with this relation applied
|
2015-09-17 00:19:03 +02:00
|
|
|
*/
|
2015-10-29 06:37:25 +01:00
|
|
|
public function applyRelation($field, &$columnName = null, $linearOnly = false) {
|
2015-09-17 00:19:03 +02:00
|
|
|
// If field is invalid, return it without modification
|
|
|
|
if(!$this->isValidRelationName($field)) {
|
|
|
|
$columnName = $field;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple fields without relations are mapped directly
|
|
|
|
if(strpos($field,'.') === false) {
|
|
|
|
$columnName = '"'.$field.'"';
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-10-29 06:37:25 +01:00
|
|
|
return $this->alterDataQuery(
|
2016-05-25 07:30:01 +02:00
|
|
|
function(DataQuery $query) use ($field, &$columnName, $linearOnly) {
|
2015-10-29 06:37:25 +01:00
|
|
|
$relations = explode('.', $field);
|
|
|
|
$fieldName = array_pop($relations);
|
|
|
|
|
|
|
|
// Apply
|
2016-05-02 06:41:30 +02:00
|
|
|
$relationModelName = $query->applyRelation($relations, $linearOnly);
|
2015-10-29 06:37:25 +01:00
|
|
|
|
|
|
|
// Find the db field the relation belongs to
|
2016-05-25 07:09:29 +02:00
|
|
|
$columnName = DataObject::getSchema()->sqlColumnForField($relationModelName, $fieldName);
|
2015-10-29 06:37:25 +01:00
|
|
|
}
|
|
|
|
);
|
2015-09-17 00:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given field specification could be interpreted as an unquoted relation name
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isValidRelationName($field) {
|
|
|
|
return preg_match('/^[A-Z0-9._]+$/i', $field);
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2016-08-29 07:00:27 +02:00
|
|
|
* Given a filter expression and value construct a {@see SearchFilter} instance
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
2016-08-29 07:00:27 +02:00
|
|
|
* @param string $filter E.g. `Name:ExactMatch:not`, `Name:ExactMatch`, `Name:not`, `Name`
|
|
|
|
* @param mixed $value Value of the filter
|
|
|
|
* @return SearchFilter
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
2016-08-29 07:00:27 +02:00
|
|
|
protected function createSearchFilter($filter, $value) {
|
|
|
|
// Field name is always the first component
|
|
|
|
$fieldArgs = explode(':', $filter);
|
|
|
|
$fieldName = array_shift($fieldArgs);
|
|
|
|
|
|
|
|
// Inspect type of second argument to determine context
|
|
|
|
$secondArg = array_shift($fieldArgs);
|
|
|
|
$modifiers = $fieldArgs;
|
|
|
|
if(!$secondArg) {
|
|
|
|
// Use default filter if none specified. E.g. `->filter(['Name' => $myname])`
|
|
|
|
$filterServiceName = 'DataListFilter.default';
|
2012-09-20 09:26:05 +02:00
|
|
|
} else {
|
2016-08-29 07:00:27 +02:00
|
|
|
// The presence of a second argument is by default ambiguous; We need to query
|
|
|
|
// Whether this is a valid modifier on the default filter, or a filter itself.
|
|
|
|
/** @var SearchFilter $defaultFilterInstance */
|
|
|
|
$defaultFilterInstance = Injector::inst()->get('DataListFilter.default');
|
|
|
|
if (in_array(strtolower($secondArg), $defaultFilterInstance->getSupportedModifiers())) {
|
|
|
|
// Treat second (and any subsequent) argument as modifiers, using default filter
|
|
|
|
$filterServiceName = 'DataListFilter.default';
|
|
|
|
array_unshift($modifiers, $secondArg);
|
|
|
|
} else {
|
|
|
|
// Second argument isn't a valid modifier, so assume is filter identifier
|
|
|
|
$filterServiceName = "DataListFilter.{$secondArg}";
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2016-08-29 07:00:27 +02:00
|
|
|
// Build instance
|
|
|
|
return Injector::inst()->create($filterServiceName, $fieldName, $value, $modifiers);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-12-14 00:06:25 +01:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a copy of this list which does not contain any items with these charactaristics
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
|
|
|
* @see SS_List::exclude()
|
2012-07-20 05:58:18 +02:00
|
|
|
* @example $list = $list->exclude('Name', 'bob'); // exclude bob from list
|
|
|
|
* @example $list = $list->exclude('Name', array('aziz', 'bob'); // exclude aziz and bob from list
|
|
|
|
* @example $list = $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude bob that has Age 21
|
|
|
|
* @example $list = $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43
|
2012-09-26 23:34:00 +02:00
|
|
|
* @example $list = $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
* // bob age 21 or 43, phil age 21 or 43 would be excluded
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
|
|
|
* @todo extract the sql from this method into a SQLGenerator class
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array Escaped SQL statement. If passed as array, all keys and values will be escaped internally
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
2012-05-29 10:09:36 +02:00
|
|
|
public function exclude() {
|
2011-12-08 22:08:46 +01:00
|
|
|
$numberFuncArgs = count(func_get_args());
|
|
|
|
$whereArguments = array();
|
2012-05-29 10:09:36 +02:00
|
|
|
|
|
|
|
if($numberFuncArgs == 1 && is_array(func_get_arg(0))) {
|
2011-12-08 22:08:46 +01:00
|
|
|
$whereArguments = func_get_arg(0);
|
|
|
|
} elseif($numberFuncArgs == 2) {
|
|
|
|
$whereArguments[func_get_arg(0)] = func_get_arg(1);
|
|
|
|
} else {
|
2012-05-29 10:10:34 +02:00
|
|
|
throw new InvalidArgumentException('Incorrect number of arguments passed to exclude()');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($whereArguments) {
|
2012-09-06 09:54:15 +02:00
|
|
|
$subquery = $query->disjunctiveGroup();
|
2012-06-24 01:57:04 +02:00
|
|
|
|
2012-09-06 09:54:15 +02:00
|
|
|
foreach($whereArguments as $field => $value) {
|
2016-08-29 07:00:27 +02:00
|
|
|
$filter = $this->createSearchFilter($field, $value);
|
2016-05-25 07:30:01 +02:00
|
|
|
$filter->exclude($subquery);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
});
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 23:53:12 +01:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* This method returns a copy of this list that does not contain any DataObjects that exists in $list
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-25 23:53:12 +01:00
|
|
|
* The $list passed needs to contain the same dataclass as $this
|
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param DataList $list
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2012-01-25 23:53:12 +01:00
|
|
|
* @throws BadMethodCallException
|
|
|
|
*/
|
2016-05-25 07:30:01 +02:00
|
|
|
public function subtract(DataList $list) {
|
|
|
|
if($this->dataClass() != $list->dataClass()) {
|
2012-01-25 23:53:12 +01:00
|
|
|
throw new InvalidArgumentException('The list passed must have the same dataclass as this class');
|
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($list){
|
2012-07-20 05:58:18 +02:00
|
|
|
$query->subtract($list->dataQuery());
|
|
|
|
});
|
2012-01-25 23:53:12 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new DataList instance with an inner join clause added to this list's query.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2012-06-30 12:37:58 +02:00
|
|
|
* @param string $table Table name (unquoted and as escaped SQL)
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $onClause Escaped SQL statement, e.g. '"Table1"."ID" = "Table2"."ID"'
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $alias - if you want this table to be aliased under another name
|
2014-09-15 02:27:55 +02:00
|
|
|
* @param int $order A numerical index to control the order that joins are added to the query; lower order values
|
|
|
|
* will cause the query to appear first. The default is 20, and joins created automatically by the
|
|
|
|
* ORM have a value of 10.
|
|
|
|
* @param array $parameters Any additional parameters if the join is a parameterised subquery
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2014-09-15 02:27:55 +02:00
|
|
|
public function innerJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($table, $onClause, $alias, $order, $parameters){
|
2014-09-15 02:27:55 +02:00
|
|
|
$query->innerJoin($table, $onClause, $alias, $order, $parameters);
|
2012-07-20 05:58:18 +02:00
|
|
|
});
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new DataList instance with a left join clause added to this list's query.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2012-06-30 12:37:58 +02:00
|
|
|
* @param string $table Table name (unquoted and as escaped SQL)
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $onClause Escaped SQL statement, e.g. '"Table1"."ID" = "Table2"."ID"'
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $alias - if you want this table to be aliased under another name
|
2014-09-15 02:27:55 +02:00
|
|
|
* @param int $order A numerical index to control the order that joins are added to the query; lower order values
|
|
|
|
* will cause the query to appear first. The default is 20, and joins created automatically by the
|
|
|
|
* ORM have a value of 10.
|
|
|
|
* @param array $parameters Any additional parameters if the join is a parameterised subquery
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2014-09-15 02:27:55 +02:00
|
|
|
public function leftJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($table, $onClause, $alias, $order, $parameters){
|
2014-09-15 02:27:55 +02:00
|
|
|
$query->leftJoin($table, $onClause, $alias, $order, $parameters);
|
2012-07-20 05:58:18 +02:00
|
|
|
});
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array of the actual items that this DataList contains at this stage.
|
|
|
|
* This is when the query is actually executed.
|
2011-05-02 10:14:55 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
public function toArray() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$query = $this->dataQuery->query();
|
|
|
|
$rows = $query->execute();
|
|
|
|
$results = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($rows as $row) {
|
|
|
|
$results[] = $this->createDataObject($row);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
return $results;
|
|
|
|
}
|
2011-05-02 10:14:55 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Return this list as an array and every object it as an sub array as well
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return array
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
public function toNestedArray() {
|
|
|
|
$result = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
foreach($this as $item) {
|
2011-05-02 10:14:55 +02:00
|
|
|
$result[] = $item->toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
/**
|
|
|
|
* Walks the list using the specified callback
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2012-11-03 02:14:04 +01:00
|
|
|
*/
|
|
|
|
public function each($callback) {
|
|
|
|
foreach($this as $row) {
|
|
|
|
$callback($row);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2011-05-02 10:14:55 +02:00
|
|
|
|
2011-12-17 00:45:45 +01:00
|
|
|
public function debug() {
|
|
|
|
$val = "<h2>" . $this->class . "</h2><ul>";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-17 00:45:45 +01:00
|
|
|
foreach($this->toNestedArray() as $item) {
|
|
|
|
$val .= "<li style=\"list-style-type: disc; margin-left: 20px\">" . Debug::text($item) . "</li>";
|
|
|
|
}
|
|
|
|
$val .= "</ul>";
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns a map of this list
|
|
|
|
*
|
|
|
|
* @param string $keyField - the 'key' field of the result array
|
|
|
|
* @param string $titleField - the value field of the result array
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return SS_Map
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-10-29 03:04:17 +02:00
|
|
|
public function map($keyField = 'ID', $titleField = 'Title') {
|
|
|
|
return new SS_Map($this, $keyField, $titleField);
|
2011-05-02 10:14:55 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Create a DataObject from the given SQL row
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param array $row
|
|
|
|
* @return DataObject
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected function createDataObject($row) {
|
2016-02-25 05:32:41 +01:00
|
|
|
$class = $this->dataClass;
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
// Failover from RecordClassName to ClassName
|
2011-12-07 02:35:30 +01:00
|
|
|
if(empty($row['RecordClassName'])) {
|
|
|
|
$row['RecordClassName'] = $row['ClassName'];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Instantiate the class mentioned in RecordClassName only if it exists, otherwise default to $this->dataClass
|
2011-12-07 02:35:30 +01:00
|
|
|
if(class_exists($row['RecordClassName'])) {
|
2016-02-25 05:32:41 +01:00
|
|
|
$class = $row['RecordClassName'];
|
2011-12-07 02:35:30 +01:00
|
|
|
}
|
2013-02-12 04:44:32 +01:00
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
$item = Injector::inst()->create($class, $row, false, $this->model, $this->getQueryParams());
|
2013-02-12 04:44:32 +01:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
return $item;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* Get query parameters for this list.
|
|
|
|
* These values will be assigned as query parameters to newly created objects from this list.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getQueryParams() {
|
|
|
|
return $this->dataQuery()->getQueryParams();
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Returns an Iterator for this DataList.
|
|
|
|
* This function allows you to use DataLists in foreach loops
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return ArrayIterator
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function getIterator() {
|
2011-05-02 10:14:55 +02:00
|
|
|
return new ArrayIterator($this->toArray());
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of items in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return int
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function count() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->dataQuery->count();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Return the maximum value of the given field in this DataList
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return mixed
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function max($fieldName) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->dataQuery->max($fieldName);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the minimum value of the given field in this DataList
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return mixed
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function min($fieldName) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->dataQuery->min($fieldName);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Return the average value of the given field in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $fieldName
|
|
|
|
* @return mixed
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function avg($fieldName) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->dataQuery->avg($fieldName);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the sum of the values of the given field in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $fieldName
|
|
|
|
* @return mixed
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function sum($fieldName) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->dataQuery->sum($fieldName);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Returns the first item in this DataList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return DataObject
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function first() {
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($this->dataQuery->firstRow()->execute() as $row) {
|
|
|
|
return $this->createDataObject($row);
|
|
|
|
}
|
2016-05-25 07:30:01 +02:00
|
|
|
return null;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last item in this DataList
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @return DataObject
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function last() {
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($this->dataQuery->lastRow()->execute() as $row) {
|
|
|
|
return $this->createDataObject($row);
|
|
|
|
}
|
2016-05-25 07:30:01 +02:00
|
|
|
return null;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this DataList has items
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return bool
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function exists() {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->count() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Find the first DataObject of this DataList where the given key = value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @return DataObject|null
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function find($key, $value) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return $this->filter($key, $value)->first();
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
/**
|
|
|
|
* Restrict the columns to fetch into this DataList
|
|
|
|
*
|
|
|
|
* @param array $queriedColumns
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2012-04-20 00:08:17 +02:00
|
|
|
*/
|
|
|
|
public function setQueriedColumns($queriedColumns) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query) use ($queriedColumns){
|
2012-07-20 05:58:18 +02:00
|
|
|
$query->setQueriedColumns($queriedColumns);
|
|
|
|
});
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Filter this list to only contain the given Primary IDs
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array $ids Array of integers
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2015-08-28 17:55:23 +02:00
|
|
|
public function byIDs($ids) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return $this->filter('ID', $ids);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-03-30 03:19:27 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Return the first DataObject with the given ID
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param int $id
|
|
|
|
* @return DataObject
|
2011-03-30 03:19:27 +02:00
|
|
|
*/
|
|
|
|
public function byID($id) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return $this->filter('ID', $id)->first();
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Returns an array of a single field value for all items in the list.
|
|
|
|
*
|
|
|
|
* @param string $colName
|
|
|
|
* @return array
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function column($colName = "ID") {
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this->dataQuery->column($colName);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Member altering methods
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Sets the ComponentSet to be the given ID list.
|
|
|
|
* Records will be added and deleted as appropriate.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-11-22 06:16:38 +01:00
|
|
|
* @param array $idList List of IDs.
|
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function setByIDList($idList) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$has = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Index current data
|
|
|
|
foreach($this->column() as $id) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$has[$id] = true;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// Keep track of items to delete
|
|
|
|
$itemsToDelete = $has;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
// add items in the list
|
|
|
|
// $id is the database ID of the record
|
|
|
|
if($idList) foreach($idList as $id) {
|
|
|
|
unset($itemsToDelete[$id]);
|
2011-10-29 06:27:21 +02:00
|
|
|
if($id && !isset($has[$id])) {
|
|
|
|
$this->add($id);
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any items that haven't been mentioned
|
|
|
|
$this->removeMany(array_keys($itemsToDelete));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Returns an array with both the keys and values set to the IDs of the records in this list.
|
2016-01-24 23:09:37 +01:00
|
|
|
* Does not respect sort order. Use ->column("ID") to get an ID list with the current sort.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-01-24 23:09:37 +01:00
|
|
|
* @return array
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function getIDList() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$ids = $this->column("ID");
|
|
|
|
return $ids ? array_combine($ids, $ids) : array();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Returns a HasManyList or ManyMany list representing the querying of a relation across all
|
|
|
|
* objects in this data list. For it to work, the relation must be defined on the data class
|
|
|
|
* that you used to create this DataList.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-11-22 06:16:38 +01:00
|
|
|
* Example: Get members from all Groups:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-19 03:27:52 +01:00
|
|
|
* DataList::Create("Group")->relation("Members")
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $relationName
|
|
|
|
* @return HasManyList|ManyManyList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function relation($relationName) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$ids = $this->column('ID');
|
|
|
|
return singleton($this->dataClass)->$relationName()->forForeignID($ids);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dbObject($fieldName) {
|
2012-03-30 04:59:47 +02:00
|
|
|
return singleton($this->dataClass)->dbObject($fieldName);
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Add a number of items to the component set.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-11-22 06:16:38 +01:00
|
|
|
* @param array $items Items to add, as either DataObjects or IDs.
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function addMany($items) {
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($items as $item) {
|
|
|
|
$this->add($item);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the items from this list with the given IDs
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param array $idList
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function removeMany($idList) {
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($idList as $id) {
|
2011-03-30 03:19:27 +02:00
|
|
|
$this->removeByID($id);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove every element in this DataList matching the given $filter.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param string $filter - a sql type where filter
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function removeByFilter($filter) {
|
2011-04-05 13:01:57 +02:00
|
|
|
foreach($this->where($filter) as $item) {
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->remove($item);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove every element in this DataList.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return $this
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function removeAll() {
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($this as $item) {
|
|
|
|
$this->remove($item);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
return $this;
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This method are overloaded by HasManyList and ManyMany list to perform more sophisticated
|
2011-12-07 02:35:30 +01:00
|
|
|
* list manipulation
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param mixed $item
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
|
|
|
public function add($item) {
|
2009-11-22 06:16:38 +01:00
|
|
|
// Nothing needs to happen by default
|
|
|
|
// TO DO: If a filter is given to this data list then
|
|
|
|
}
|
2011-05-01 07:33:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new item to add to this DataList.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-01 07:33:02 +02:00
|
|
|
* @todo This doesn't factor in filters.
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param array $initialFields
|
|
|
|
* @return DataObject
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function newObject($initialFields = null) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$class = $this->dataClass;
|
2012-12-08 12:20:20 +01:00
|
|
|
return Injector::inst()->create($class, $initialFields, false, $this->model);
|
2011-05-01 07:33:02 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Remove this item by deleting it
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param DataObject $item
|
2011-12-07 02:35:30 +01:00
|
|
|
* @todo Allow for amendment of this behaviour - for example, we can remove an item from
|
|
|
|
* an "ActiveItems" DataList by chaning the status to inactive.
|
|
|
|
*/
|
|
|
|
public function remove($item) {
|
2009-11-22 06:16:38 +01:00
|
|
|
// By default, we remove an item from a DataList by deleting it.
|
2012-12-12 22:18:21 +01:00
|
|
|
$this->removeByID($item->ID);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* Remove an item from this DataList by ID
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param int $itemID The primary ID
|
2012-12-08 12:20:20 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function removeByID($itemID) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$item = $this->byID($itemID);
|
2013-05-20 12:18:07 +02:00
|
|
|
|
|
|
|
if($item) {
|
2016-05-25 07:30:01 +02:00
|
|
|
$item->delete();
|
2013-05-20 12:18:07 +02:00
|
|
|
}
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
|
|
|
* Reverses a list of items.
|
|
|
|
*
|
2016-08-22 17:39:11 +02:00
|
|
|
* @return static
|
2012-04-15 10:34:10 +02:00
|
|
|
*/
|
|
|
|
public function reverse() {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->alterDataQuery(function(DataQuery $query){
|
2012-07-20 05:58:18 +02:00
|
|
|
$query->reverseSort();
|
|
|
|
});
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 05:51:45 +02:00
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Returns whether an item with $key exists
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-30 05:51:45 +02:00
|
|
|
* @param mixed $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists($key) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return ($this->limit(1,$key)->first() != null);
|
2011-03-30 05:51:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Returns item stored in list with index $key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-30 05:51:45 +02:00
|
|
|
* @param mixed $key
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function offsetGet($key) {
|
2016-05-25 07:30:01 +02:00
|
|
|
return $this->limit(1, $key)->first();
|
2011-03-30 05:51:45 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 05:51:45 +02:00
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Set an item with the key in $key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-30 05:51:45 +02:00
|
|
|
* @param mixed $key
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function offsetSet($key, $value) {
|
2012-12-08 12:20:20 +01:00
|
|
|
user_error("Can't alter items in a DataList using array-access", E_USER_ERROR);
|
2011-03-30 05:51:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Unset an item with the key in $key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-30 05:51:45 +02:00
|
|
|
* @param mixed $key
|
|
|
|
*/
|
|
|
|
public function offsetUnset($key) {
|
2012-12-08 12:20:20 +01:00
|
|
|
user_error("Can't alter items in a DataList using array-access", E_USER_ERROR);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
}
|