2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implements a "lazy loading" DataObjectSet.
|
|
|
|
* Uses {@link DataQuery} to do the actual query generation.
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* todo 3.1: In 3.0 the below is not currently true for backwards compatible reasons, but code should not rely on
|
|
|
|
* current behaviour.
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
|
|
|
* DataLists have two sets of methods.
|
|
|
|
*
|
|
|
|
* 1). Selection methods (SS_Filterable, SS_Sortable, SS_Limitable) change the way the list is built, but does not
|
2012-09-26 23:34:00 +02:00
|
|
|
* alter underlying data. There are no external affects from selection methods once this list instance is
|
|
|
|
* destructed.
|
2012-07-20 05:58:18 +02:00
|
|
|
*
|
|
|
|
* 2). Mutation methods change the underlying data. The change persists into the underlying data storage layer.
|
|
|
|
*
|
|
|
|
* DataLists are _immutable_ as far as selection methods go - they all return new instances of DataList, rather
|
|
|
|
* than change the current list.
|
|
|
|
*
|
|
|
|
* DataLists are _mutable_ as far as mutation methods go - they all act on the existing DataList instance.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-12-07 02:35:30 +01:00
|
|
|
* @subpackage model
|
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 {
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* The DataObject class name that this data list is querying
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @var string
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected $dataClass;
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* The {@link DataQuery} object responsible for getting this DataList's records
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @var DataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected $dataQuery;
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* The DataModel from which this DataList comes.
|
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);
|
2012-04-15 10:34:10 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the DataModel
|
|
|
|
*
|
|
|
|
* @param DataModel $model
|
|
|
|
*/
|
2012-05-01 04:43:52 +02:00
|
|
|
public function setDataModel(DataModel $model) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = $model;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a copy of the internal {@link DataQuery} object
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* todo 3.1: In 3.0 the below is not currently true for backwards compatible reasons, but code should not rely on
|
|
|
|
* this
|
|
|
|
*
|
2012-07-20 05:58:18 +02:00
|
|
|
* 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-07-20 05:58:18 +02:00
|
|
|
// TODO 3.1: This method potentially mutates self
|
|
|
|
return /* clone */ $this->dataQuery;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @param $callback
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function alterDataQuery($callback) {
|
|
|
|
if ($this->inAlterDataQueryCall) {
|
|
|
|
$list = $this;
|
|
|
|
|
|
|
|
$res = $callback($list->dataQuery, $list);
|
|
|
|
if ($res) $list->dataQuery = $res;
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$list = clone $this;
|
|
|
|
$list->inAlterDataQueryCall = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$res = $callback($list->dataQuery, $list);
|
|
|
|
if ($res) $list->dataQuery = $res;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$list->inAlterDataQueryCall = false;
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$list->inAlterDataQueryCall = false;
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In 3.0.0 some methods in DataList mutate their list. We don't want to change that in the 3.0.x
|
|
|
|
* line, but we don't want people relying on it either. This does the same as alterDataQuery, but
|
|
|
|
* _does_ mutate the existing list.
|
|
|
|
*
|
|
|
|
* todo 3.1: All methods that call this need to call alterDataQuery instead
|
|
|
|
*/
|
|
|
|
protected function alterDataQuery_30($callback) {
|
|
|
|
Deprecation::notice('3.1', 'DataList will become immutable in 3.1');
|
|
|
|
|
|
|
|
if ($this->inAlterDataQueryCall) {
|
|
|
|
$res = $callback($this->dataQuery, $this);
|
|
|
|
if ($res) $this->dataQuery = $res;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->inAlterDataQueryCall = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$res = $callback($this->dataQuery, $this);
|
|
|
|
if ($res) $this->dataQuery = $res;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->inAlterDataQueryCall = false;
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->inAlterDataQueryCall = false;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new DataList instance with the underlying {@link DataQuery} object changed
|
|
|
|
*
|
|
|
|
* @param DataQuery $dataQuery
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
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. :-)
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @return SQLQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function sql() {
|
|
|
|
return $this->dataQuery->query()->sql();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new DataList instance with a WHERE clause added to this list's query.
|
2011-04-05 13:01:57 +02:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param string $filter Escaped SQL statement
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return DataList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-04-05 13:01:57 +02:00
|
|
|
public function where($filter) {
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery_30(function($query) use ($filter){
|
|
|
|
$query->where($filter);
|
|
|
|
});
|
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.
|
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
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canFilterBy($fieldName) {
|
|
|
|
if($t = singleton($this->dataClass)->hasDatabaseField($fieldName)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new DataList instance with a join clause added to this list's query.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param type $join Escaped SQL statement
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return DataList
|
|
|
|
* @deprecated 3.0
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function join($join) {
|
2011-10-29 06:11:27 +02:00
|
|
|
Deprecation::notice('3.0', 'Use innerJoin() or leftJoin() instead.');
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery_30(function($query) use ($join){
|
|
|
|
$query->join($join);
|
|
|
|
});
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-20 05:58:18 +02:00
|
|
|
* Return a new DataList instance with the records returned in this query restricted by a limit clause
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-03-09 02:02:37 +01:00
|
|
|
public function limit($limit, $offset = 0) {
|
2012-03-09 03:15:55 +01:00
|
|
|
if(!$limit && !$offset) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
if($limit && !is_numeric($limit)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
Deprecation::notice('3.0', 'Please pass limits as 2 arguments, rather than an array or SQL fragment.',
|
|
|
|
Deprecation::SCOPE_GLOBAL);
|
2012-03-09 02:02:37 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery_30(function($query) use ($limit, $offset){
|
|
|
|
$query->limit($limit, $offset);
|
|
|
|
});
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
|
|
|
|
/**
|
2012-07-20 05:58:18 +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()
|
2012-04-15 10:34:10 +02:00
|
|
|
* @see SQLQuery::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');
|
|
|
|
* @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.
|
2012-04-15 10:34:10 +02:00
|
|
|
* @return DataList
|
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;
|
|
|
|
}
|
2012-04-15 10:34:10 +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
|
|
|
$sort = $col = $dir = null;
|
|
|
|
|
|
|
|
if ($count == 2) {
|
|
|
|
list($col, $dir) = func_get_args();
|
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
|
|
|
|
|
|
|
return $this->alterDataQuery_30(function($query, $list) use ($sort, $col, $dir){
|
|
|
|
|
|
|
|
if ($col) {
|
|
|
|
// sort('Name','Desc')
|
|
|
|
if(!in_array(strtolower($dir),array('desc','asc'))){
|
|
|
|
user_error('Second argument to sort must be either ASC or DESC');
|
2012-05-01 07:09:57 +02:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
$query->sort($col, $dir);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
else if(is_string($sort) && $sort){
|
|
|
|
// sort('Name ASC')
|
|
|
|
if(stristr($sort, ' asc') || stristr($sort, ' desc')) {
|
|
|
|
$query->sort($sort);
|
|
|
|
} else {
|
|
|
|
$query->sort($sort, 'ASC');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(is_array($sort)) {
|
|
|
|
// sort(array('Name'=>'desc'));
|
|
|
|
$query->sort(null, null); // wipe the sort
|
|
|
|
|
|
|
|
foreach($sort as $col => $dir) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// Convert column expressions to SQL fragment, while still allowing the passing of raw SQL
|
|
|
|
// fragments.
|
2012-07-20 05:58:18 +02:00
|
|
|
try {
|
|
|
|
$relCol = $list->getRelationName($col);
|
|
|
|
} catch(InvalidArgumentException $e) {
|
|
|
|
$relCol = $col;
|
|
|
|
}
|
|
|
|
$query->sort($relCol, $dir, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
* @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
|
|
|
*
|
|
|
|
* @todo extract the sql from $customQuery into a SQLGenerator class
|
2012-04-15 10:34:10 +02:00
|
|
|
*
|
2012-05-29 10:09:36 +02:00
|
|
|
* @param string|array Escaped SQL statement. If passed as array, all keys and values are assumed to be escaped.
|
2012-04-15 10:34:10 +02:00
|
|
|
* @return DataList
|
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
|
|
|
}
|
2012-05-30 01:44:20 +02:00
|
|
|
|
2012-07-20 05:58:18 +02:00
|
|
|
// TODO 3.1: Once addFilter doesn't mutate self, this results in a double clone
|
2012-05-30 01:44:20 +02:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->addFilter($filters);
|
|
|
|
return $clone;
|
|
|
|
}
|
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
|
2012-05-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
public function addFilter($filterArray) {
|
2011-12-08 22:08:46 +01:00
|
|
|
$SQL_Statements = array();
|
2012-05-30 01:44:20 +02:00
|
|
|
foreach($filterArray as $field => $value) {
|
2011-12-08 22:08:46 +01:00
|
|
|
if(is_array($value)) {
|
|
|
|
$customQuery = 'IN (\''.implode('\',\'',Convert::raw2sql($value)).'\')';
|
|
|
|
} else {
|
|
|
|
$customQuery = '= \''.Convert::raw2sql($value).'\'';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stristr($field,':')) {
|
|
|
|
$fieldArgs = explode(':',$field);
|
|
|
|
$field = array_shift($fieldArgs);
|
|
|
|
foreach($fieldArgs as $fieldArg){
|
2012-12-14 00:06:25 +01:00
|
|
|
$this->applyFilterContext($field, $fieldArg, $value);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-24 01:57:04 +02:00
|
|
|
if($field == 'ID') {
|
|
|
|
$field = sprintf('"%s"."ID"', ClassInfo::baseDataClass($this->dataClass));
|
|
|
|
} else {
|
|
|
|
$field = '"' . Convert::raw2sql($field) . '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$SQL_Statements[] = $field . ' ' . $customQuery;
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
if(!count($SQL_Statements)) return $this;
|
|
|
|
|
|
|
|
return $this->alterDataQuery_30(function($query) use ($SQL_Statements){
|
2011-12-08 22:08:46 +01:00
|
|
|
foreach($SQL_Statements as $SQL_Statement){
|
2012-07-20 05:58:18 +02:00
|
|
|
$query->where($SQL_Statement);
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
});
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2012-05-29 00:07:09 +02:00
|
|
|
/**
|
|
|
|
* Filter this DataList by a callback function.
|
2012-09-26 23:34:00 +02:00
|
|
|
* The function will be passed each record of the DataList in turn, and must return true for the record to be
|
|
|
|
* included. Returns the filtered list.
|
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.
|
2012-05-29 00:07:09 +02:00
|
|
|
*/
|
|
|
|
public function filterByCallback($callback) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!is_callable($callback)) {
|
|
|
|
throw new LogicException("DataList::filterByCallback() must be passed something callable.");
|
|
|
|
}
|
2012-05-29 00:07:09 +02:00
|
|
|
|
|
|
|
$output = new ArrayList;
|
|
|
|
foreach($this as $item) {
|
|
|
|
if($callback($item)) $output->push($item);
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2011-12-18 04:28:09 +01:00
|
|
|
/**
|
|
|
|
* Translates a Object relation name to a Database name and apply the relation join to
|
2012-05-01 07:09:57 +02:00
|
|
|
* the query. Throws an InvalidArgumentException if the $field doesn't correspond to a relation
|
2011-12-18 04:28:09 +01:00
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRelationName($field) {
|
2012-05-01 07:09:57 +02:00
|
|
|
if(!preg_match('/^[A-Z0-9._]+$/i', $field)) {
|
|
|
|
throw new InvalidArgumentException("Bad field expression $field");
|
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
if (!$this->inAlterDataQueryCall) {
|
2012-09-26 23:34:00 +02:00
|
|
|
Deprecation::notice('3.1',
|
|
|
|
'getRelationName is mutating, and must be called inside an alterDataQuery block');
|
2012-07-20 05:58:18 +02:00
|
|
|
}
|
|
|
|
|
2011-12-18 04:28:09 +01:00
|
|
|
if(strpos($field,'.') === false) {
|
|
|
|
return '"'.$field.'"';
|
|
|
|
}
|
|
|
|
$relations = explode('.', $field);
|
|
|
|
$fieldName = array_pop($relations);
|
|
|
|
$relationModelName = $this->dataQuery->applyRelation($field);
|
|
|
|
return '"'.$relationModelName.'"."'.$fieldName.'"';
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2012-12-14 00:06:25 +01:00
|
|
|
* Translates a filter type to a SQL query.
|
2011-12-08 22:08:46 +01:00
|
|
|
*
|
|
|
|
* @param string $field - the fieldname in the db
|
2012-12-14 00:06:25 +01:00
|
|
|
* @param string $filter - a {@link SearchFilter} class, e.g. PartialMatch or StartsWith
|
2011-12-08 22:08:46 +01:00
|
|
|
* @param string $value - the value that the filtercontext will use for matching
|
|
|
|
* @todo Deprecated SearchContexts and pull their functionality into the core of the ORM
|
|
|
|
*/
|
2012-12-14 00:06:25 +01:00
|
|
|
private function applyFilterContext($field, $filter, $value) {
|
2011-12-08 22:08:46 +01:00
|
|
|
$t = singleton($this->dataClass())->dbObject($field);
|
2012-12-14 00:06:25 +01:00
|
|
|
$className = sprintf('%sFilter', $filter);
|
|
|
|
if(!class_exists($className)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Filter class "%s" does not exist', $className));
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-12-14 00:06:25 +01:00
|
|
|
$t = new $className($field, $value);
|
2011-12-08 22:08:46 +01:00
|
|
|
$t->apply($this->dataQuery());
|
|
|
|
}
|
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
|
|
|
*
|
2012-05-29 10:09:36 +02:00
|
|
|
* @param string|array Escaped SQL statement. If passed as array, all keys and values are assumed to be escaped.
|
2012-04-15 10:34:10 +02:00
|
|
|
* @return DataList
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$SQL_Statements = array();
|
|
|
|
foreach($whereArguments as $fieldName => $value) {
|
2012-06-24 01:57:04 +02:00
|
|
|
if($fieldName == 'ID') {
|
|
|
|
$fieldName = sprintf('"%s"."ID"', ClassInfo::baseDataClass($this->dataClass));
|
|
|
|
} else {
|
|
|
|
$fieldName = '"' . Convert::raw2sql($fieldName) . '"';
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
if(is_array($value)){
|
2012-06-24 01:57:04 +02:00
|
|
|
$SQL_Statements[] = ($fieldName . ' NOT IN (\''.implode('\',\'', Convert::raw2sql($value)).'\')');
|
2011-12-08 22:08:46 +01:00
|
|
|
} else {
|
2012-06-24 01:57:04 +02:00
|
|
|
$SQL_Statements[] = ($fieldName . ' != \''.Convert::raw2sql($value).'\'');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
if(!count($SQL_Statements)) return $this;
|
|
|
|
|
|
|
|
return $this->alterDataQuery_30(function($query) use ($SQL_Statements){
|
|
|
|
$query->whereAny($SQL_Statements);
|
|
|
|
});
|
2011-12-08 22:08:46 +01: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
|
2012-01-25 23:53:12 +01:00
|
|
|
*
|
|
|
|
* The $list passed needs to contain the same dataclass as $this
|
|
|
|
*
|
|
|
|
* @param SS_List $list
|
|
|
|
* @return DataList
|
|
|
|
* @throws BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function subtract(SS_List $list) {
|
|
|
|
if($this->dataclass() != $list->dataclass()) {
|
|
|
|
throw new InvalidArgumentException('The list passed must have the same dataclass as this class');
|
|
|
|
}
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
return $this->alterDataQuery(function($query) use ($list){
|
|
|
|
$query->subtract($list->dataQuery());
|
|
|
|
});
|
2012-01-25 23:53:12 +01: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-05-16 11:29:49 +02:00
|
|
|
* @param string $table Table name (unquoted)
|
|
|
|
* @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
|
|
|
|
* @return DataList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function innerJoin($table, $onClause, $alias = null) {
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery_30(function($query) use ($table, $onClause, $alias){
|
|
|
|
$query->innerJoin($table, $onClause, $alias);
|
|
|
|
});
|
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-05-16 11:29:49 +02:00
|
|
|
* @param string $table Table name (unquoted)
|
|
|
|
* @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
|
|
|
|
* @return DataList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function leftJoin($table, $onClause, $alias = null) {
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery_30(function($query) use ($table, $onClause, $alias){
|
|
|
|
$query->leftJoin($table, $onClause, $alias);
|
|
|
|
});
|
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();
|
2012-04-15 10:34:10 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($rows as $row) {
|
|
|
|
$results[] = $this->createDataObject($row);
|
|
|
|
}
|
2012-04-15 10:34:10 +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
|
|
|
|
*
|
|
|
|
* @return type
|
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
public function toNestedArray() {
|
|
|
|
$result = array();
|
2012-04-15 10:34:10 +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;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:45:45 +01:00
|
|
|
public function debug() {
|
|
|
|
$val = "<h2>" . $this->class . "</h2><ul>";
|
2012-04-15 10:34:10 +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
|
|
|
|
* @return SS_Map
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @param array $row
|
|
|
|
* @return DataObject
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
protected function createDataObject($row) {
|
|
|
|
$defaultClass = $this->dataClass;
|
|
|
|
|
|
|
|
// Failover from RecordClassName to ClassName
|
2011-12-07 02:35:30 +01:00
|
|
|
if(empty($row['RecordClassName'])) {
|
|
|
|
$row['RecordClassName'] = $row['ClassName'];
|
|
|
|
}
|
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'])) {
|
2012-06-04 14:01:26 +02:00
|
|
|
$item = Injector::inst()->create($row['RecordClassName'], $row, false, $this->model);
|
2011-12-07 02:35:30 +01:00
|
|
|
} else {
|
2012-06-04 14:01:26 +02:00
|
|
|
$item = Injector::inst()->create($defaultClass, $row, false, $this->model);
|
2011-12-07 02:35:30 +01:00
|
|
|
}
|
2011-05-01 07:33:02 +02:00
|
|
|
|
|
|
|
return $item;
|
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
|
2011-12-07 02:35:30 +01: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
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the average 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 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
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first 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 first() {
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($this->dataQuery->firstRow()->execute() as $row) {
|
|
|
|
return $this->createDataObject($row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this DataList has items
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a sub-range of this dataobjectset as an array
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $length
|
|
|
|
* @return DataList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function getRange($offset, $length) {
|
2012-07-13 11:37:35 +02:00
|
|
|
Deprecation::notice("3.0", 'Use limit($length, $offset) instead. Note the new argument order.');
|
2012-03-09 02:02:37 +01:00
|
|
|
return $this->limit($length, $offset);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
2011-10-29 06:12:02 +02:00
|
|
|
if($key == 'ID') {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2012-05-16 11:29:49 +02:00
|
|
|
$SQL_col = sprintf('"%s"."%s"', $baseClass, Convert::raw2sql($key));
|
2011-10-29 06:12:02 +02:00
|
|
|
} else {
|
2012-05-16 11:29:49 +02:00
|
|
|
$SQL_col = sprintf('"%s"', Convert::raw2sql($key));
|
2011-10-29 06:12:02 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 05:58:18 +02:00
|
|
|
// todo 3.1: In 3.1 where won't be mutating, so this can be on $this directly
|
|
|
|
$clone = clone $this;
|
2011-10-29 06:12:02 +02:00
|
|
|
return $clone->where("$SQL_col = '" . Convert::raw2sql($value) . "'")->First();
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
/**
|
|
|
|
* Restrict the columns to fetch into this DataList
|
|
|
|
*
|
|
|
|
* @param array $queriedColumns
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function setQueriedColumns($queriedColumns) {
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery(function($query) use ($queriedColumns){
|
|
|
|
$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
|
|
|
|
*
|
2012-05-16 11:29:49 +02:00
|
|
|
* @param array $ids Array of integers, will be automatically cast/escaped.
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return DataList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function byIDs(array $ids) {
|
2012-05-16 11:29:49 +02:00
|
|
|
$ids = array_map('intval', $ids); // sanitize
|
2009-11-22 06:16:38 +01:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2011-04-05 13:01:57 +02:00
|
|
|
$this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
|
2012-04-15 10:34:10 +02:00
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2011-03-30 03:19:27 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Return the first DataObject with the given ID
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return DataObject
|
2011-03-30 03:19:27 +02:00
|
|
|
*/
|
|
|
|
public function byID($id) {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2012-07-20 05:58:18 +02:00
|
|
|
|
|
|
|
// todo 3.1: In 3.1 where won't be mutating, so this can be on $this directly
|
2011-12-06 01:56:24 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
return $clone->where("\"$baseClass\".\"ID\" = " . (int)$id)->First();
|
2011-03-30 03:19:27 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Member altering methods
|
2011-12-07 02:35:30 +01: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.
|
2011-12-07 02:35:30 +01: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();
|
2011-10-29 06:27:21 +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
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of items to delete
|
|
|
|
$itemsToDelete = $has;
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with both the keys and values set to the IDs of the records in this list.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Example: Get members from all Groups:
|
|
|
|
*
|
2012-03-19 03:27:52 +01:00
|
|
|
* DataList::Create("Group")->relation("Members")
|
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.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2009-11-22 06:16:38 +01:00
|
|
|
* @param array $items Items to add, as either DataObjects or IDs.
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return DataList
|
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
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param array $idList
|
|
|
|
* @return DataList
|
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.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $filter - a sql type where filter
|
|
|
|
* @return DataList
|
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
|
|
|
*
|
|
|
|
* @return DataList
|
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
|
|
|
/**
|
|
|
|
* This method are overloaded by HasManyList and ManyMany list to perform more sophisticated
|
|
|
|
* list manipulation
|
|
|
|
*
|
|
|
|
* @param type $item
|
|
|
|
*/
|
|
|
|
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.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
2011-05-01 07:33:02 +02:00
|
|
|
* @todo This doesn't factor in filters.
|
|
|
|
*/
|
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
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Remove this item by deleting it
|
|
|
|
*
|
|
|
|
* @param DataClass $item
|
|
|
|
* @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.
|
|
|
|
if($item instanceof $this->dataClass) $item->delete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* Remove an item from this DataList by ID
|
2011-12-07 02:35:30 +01: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);
|
|
|
|
if($item) return $item->delete();
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
2012-04-15 10:34:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverses a list of items.
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function reverse() {
|
2012-07-20 05:58:18 +02:00
|
|
|
return $this->alterDataQuery_30(function($query){
|
|
|
|
$query->reverseSort();
|
|
|
|
});
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* This method won't function on DataLists due to the specific query that it represent
|
|
|
|
*
|
|
|
|
* @param mixed $item
|
|
|
|
*/
|
|
|
|
public function push($item) {
|
2009-11-22 06:16:38 +01:00
|
|
|
user_error("Can't call DataList::push() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method won't function on DataLists due to the specific query that it represent
|
|
|
|
*
|
|
|
|
* @param mixed $item
|
|
|
|
*/
|
|
|
|
public function insertFirst($item) {
|
2009-11-22 06:16:38 +01:00
|
|
|
user_error("Can't call DataList::insertFirst() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method won't function on DataLists due to the specific query that it represent
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function shift() {
|
2009-11-22 06:16:38 +01:00
|
|
|
user_error("Can't call DataList::shift() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method won't function on DataLists due to the specific query that it represent
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function replace() {
|
2009-11-22 06:16:38 +01:00
|
|
|
user_error("Can't call DataList::replace() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method won't function on DataLists due to the specific query that it represent
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function merge() {
|
2009-11-22 06:16:38 +01:00
|
|
|
user_error("Can't call DataList::merge() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method won't function on DataLists due to the specific query that it represent
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function removeDuplicates() {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.",
|
|
|
|
E_USER_ERROR);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2011-03-30 05:51:45 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Returns whether an item with $key exists
|
|
|
|
*
|
2011-03-30 05:51:45 +02:00
|
|
|
* @param mixed $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists($key) {
|
2012-12-08 12:20:20 +01: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
|
|
|
|
*
|
2011-03-30 05:51:45 +02:00
|
|
|
* @param mixed $key
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function offsetGet($key) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->limit(1, $key)->First();
|
2011-03-30 05:51:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Set an item with the key in $key
|
|
|
|
*
|
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
|
|
|
|
*
|
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
|
|
|
|
|
|
|
}
|