mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MINOR Updated inline documentation and added visibility keywords to methods for SS_List, DataList and ArrayList
This commit is contained in:
parent
60ced3b167
commit
2306ec94c3
@ -8,27 +8,66 @@
|
|||||||
class ArrayList extends ViewableData implements SS_List {
|
class ArrayList extends ViewableData implements SS_List {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Holds the items in the list
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $items;
|
protected $items;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synonym of the constructor. Can be chained with literate methods.
|
||||||
|
* ArrayList::create("SiteTree")->sort("Title") is legal, but
|
||||||
|
* new ArrayList("SiteTree")->sort("Title") is not.
|
||||||
|
*
|
||||||
|
* @param array $items - an initial array to fill this object with
|
||||||
|
*/
|
||||||
|
public static function create(array $items = array()) {
|
||||||
|
return new ArrayList($items);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array $items - an initial array to fill this object with
|
||||||
|
*/
|
||||||
public function __construct(array $items = array()) {
|
public function __construct(array $items = array()) {
|
||||||
$this->items = $items;
|
$this->items = $items;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the number of items in this list
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
public function count() {
|
public function count() {
|
||||||
return count($this->items);
|
return count($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this list has items
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function exists() {
|
public function exists() {
|
||||||
return (bool) count($this);
|
return (bool) count($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an Iterator for this ArrayList.
|
||||||
|
* This function allows you to use ArrayList in foreach loops
|
||||||
|
*
|
||||||
|
* @return ArrayIterator
|
||||||
|
*/
|
||||||
public function getIterator() {
|
public function getIterator() {
|
||||||
return new ArrayIterator($this->items);
|
return new ArrayIterator($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array of the actual items that this ArrayList contains.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function toArray() {
|
public function toArray() {
|
||||||
return $this->items;
|
return $this->items;
|
||||||
}
|
}
|
||||||
@ -42,6 +81,11 @@ class ArrayList extends ViewableData implements SS_List {
|
|||||||
return $val;
|
return $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return this list as an array and every object it as an sub array as well
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function toNestedArray() {
|
public function toNestedArray() {
|
||||||
$result = array();
|
$result = array();
|
||||||
|
|
||||||
@ -60,14 +104,31 @@ class ArrayList extends ViewableData implements SS_List {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a sub-range of this dataobjectset as an array
|
||||||
|
*
|
||||||
|
* @param int $offset
|
||||||
|
* @param int $length
|
||||||
|
* @return ArrayList
|
||||||
|
*/
|
||||||
public function getRange($offset, $length) {
|
public function getRange($offset, $length) {
|
||||||
return new ArrayList(array_slice($this->items, $offset, $length));
|
return new ArrayList(array_slice($this->items, $offset, $length));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add this $item into this list
|
||||||
|
*
|
||||||
|
* @param mixed $item
|
||||||
|
*/
|
||||||
public function add($item) {
|
public function add($item) {
|
||||||
$this->push($item);
|
$this->push($item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove this item from this list
|
||||||
|
*
|
||||||
|
* @param mixed $item
|
||||||
|
*/
|
||||||
public function remove($item) {
|
public function remove($item) {
|
||||||
foreach ($this->items as $key => $value) {
|
foreach ($this->items as $key => $value) {
|
||||||
if ($item === $value) unset($this->items[$key]);
|
if ($item === $value) unset($this->items[$key]);
|
||||||
@ -139,7 +200,7 @@ class ArrayList extends ViewableData implements SS_List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unshifts an item onto the beginning of the list.
|
* Add an item onto the beginning of the list.
|
||||||
*
|
*
|
||||||
* @param array|object $item
|
* @param array|object $item
|
||||||
*/
|
*/
|
||||||
@ -156,14 +217,31 @@ class ArrayList extends ViewableData implements SS_List {
|
|||||||
return array_shift($this->items);
|
return array_shift($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first item in the list
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function first() {
|
public function first() {
|
||||||
return reset($this->items);
|
return reset($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last item in the list
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function last() {
|
public function last() {
|
||||||
return end($this->items);
|
return end($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a map of this list
|
||||||
|
*
|
||||||
|
* @param type $keyfield - the 'key' field of the result array
|
||||||
|
* @param type $titlefield - the value field of the result array
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function map($keyfield = 'ID', $titlefield = 'Title') {
|
public function map($keyfield = 'ID', $titlefield = 'Title') {
|
||||||
$map = array();
|
$map = array();
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->items as $item) {
|
||||||
@ -172,20 +250,39 @@ class ArrayList extends ViewableData implements SS_List {
|
|||||||
return $map;
|
return $map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the first item of this list where the given key = value
|
||||||
|
*
|
||||||
|
* @param type $key
|
||||||
|
* @param type $value
|
||||||
|
* @return type
|
||||||
|
*/
|
||||||
public function find($key, $value) {
|
public function find($key, $value) {
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->items as $item) {
|
||||||
if ($this->extractValue($item, $key) == $value) return $item;
|
if ($this->extractValue($item, $key) == $value) return $item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function column($field = 'ID') {
|
/**
|
||||||
|
* Returns an array of a single field value for all items in the list.
|
||||||
|
*
|
||||||
|
* @param string $colName
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function column($colName = 'ID') {
|
||||||
$result = array();
|
$result = array();
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->items as $item) {
|
||||||
$result[] = $this->extractValue($item, $field);
|
$result[] = $this->extractValue($item, $colName);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You can always sort a ArrayList
|
||||||
|
*
|
||||||
|
* @param string $by
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function canSortBy($by) {
|
public function canSortBy($by) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -221,18 +318,41 @@ class ArrayList extends ViewableData implements SS_List {
|
|||||||
call_user_func_array('array_multisort', $sorts);
|
call_user_func_array('array_multisort', $sorts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether an item with $key exists
|
||||||
|
*
|
||||||
|
* @param mixed $key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function offsetExists($offset) {
|
public function offsetExists($offset) {
|
||||||
return array_key_exists($offset, $this->items);
|
return array_key_exists($offset, $this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns item stored in list with index $key
|
||||||
|
*
|
||||||
|
* @param mixed $key
|
||||||
|
* @return DataObject
|
||||||
|
*/
|
||||||
public function offsetGet($offset) {
|
public function offsetGet($offset) {
|
||||||
if ($this->offsetExists($offset)) return $this->items[$offset];
|
if ($this->offsetExists($offset)) return $this->items[$offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set an item with the key in $key
|
||||||
|
*
|
||||||
|
* @param mixed $key
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
public function offsetSet($offset, $value) {
|
public function offsetSet($offset, $value) {
|
||||||
$this->items[$offset] = $value;
|
$this->items[$offset] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset an item with the key in $key
|
||||||
|
*
|
||||||
|
* @param mixed $key
|
||||||
|
*/
|
||||||
public function offsetUnset($offset) {
|
public function offsetUnset($offset) {
|
||||||
unset($this->items[$offset]);
|
unset($this->items[$offset]);
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,29 @@
|
|||||||
/**
|
/**
|
||||||
* Implements a "lazy loading" DataObjectSet.
|
* Implements a "lazy loading" DataObjectSet.
|
||||||
* Uses {@link DataQuery} to do the actual query generation.
|
* Uses {@link DataQuery} to do the actual query generation.
|
||||||
|
*
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage model
|
||||||
*/
|
*/
|
||||||
class DataList extends ViewableData implements SS_List {
|
class DataList extends ViewableData implements SS_List {
|
||||||
/**
|
/**
|
||||||
* The DataObject class name that this data list is querying
|
* The DataObject class name that this data list is querying
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $dataClass;
|
protected $dataClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link DataQuery} object responsible for getting this DataList's records
|
* The {@link DataQuery} object responsible for getting this DataList's records
|
||||||
|
*
|
||||||
|
* @var DataQuery
|
||||||
*/
|
*/
|
||||||
protected $dataQuery;
|
protected $dataQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The DataModel from which this DataList comes.
|
* The DataModel from which this DataList comes.
|
||||||
|
*
|
||||||
|
* @var DataModel
|
||||||
*/
|
*/
|
||||||
protected $model;
|
protected $model;
|
||||||
|
|
||||||
@ -23,15 +32,18 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
* Synonym of the constructor. Can be chained with literate methods.
|
* Synonym of the constructor. Can be chained with literate methods.
|
||||||
* DataList::create("SiteTree")->sort("Title") is legal, but
|
* DataList::create("SiteTree")->sort("Title") is legal, but
|
||||||
* new DataList("SiteTree")->sort("Title") is not.
|
* new DataList("SiteTree")->sort("Title") is not.
|
||||||
|
*
|
||||||
|
* @param string $dataClass - The DataObject class to query.
|
||||||
*/
|
*/
|
||||||
static function create($dataClass) {
|
public static function create($dataClass) {
|
||||||
return new DataList($dataClass);
|
return new DataList($dataClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new DataList.
|
* Create a new DataList.
|
||||||
* No querying is done on construction, but the initial query schema is set up.
|
* No querying is done on construction, but the initial query schema is set up.
|
||||||
* @param $dataClass The DataObject class to query.
|
*
|
||||||
|
* @param string $dataClass - The DataObject class to query.
|
||||||
*/
|
*/
|
||||||
public function __construct($dataClass) {
|
public function __construct($dataClass) {
|
||||||
$this->dataClass = $dataClass;
|
$this->dataClass = $dataClass;
|
||||||
@ -39,29 +51,44 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the DataModel
|
||||||
|
*
|
||||||
|
* @param DataModel $model
|
||||||
|
*/
|
||||||
public function setModel(DataModel $model) {
|
public function setModel(DataModel $model) {
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the dataClass name for this DataList, ie the DataObject ClassName
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function dataClass() {
|
public function dataClass() {
|
||||||
return $this->dataClass;
|
return $this->dataClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clone this object
|
* When cloning this object, clone the dataQuery object as well
|
||||||
*/
|
*/
|
||||||
function __clone() {
|
public function __clone() {
|
||||||
$this->dataQuery = clone $this->dataQuery;
|
$this->dataQuery = clone $this->dataQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the internal {@link DataQuery} object for direct manipulation
|
* Return the internal {@link DataQuery} object for direct manipulation
|
||||||
|
*
|
||||||
|
* @return DataQuery
|
||||||
*/
|
*/
|
||||||
public function dataQuery() {
|
public function dataQuery() {
|
||||||
return $this->dataQuery;
|
return $this->dataQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the SQL query that will be used to get this DataList's records. Good for debugging. :-)
|
* Returns the SQL query that will be used to get this DataList's records. Good for debugging. :-)
|
||||||
|
*
|
||||||
|
* @return SQLQuery
|
||||||
*/
|
*/
|
||||||
public function sql() {
|
public function sql() {
|
||||||
return $this->dataQuery->query()->sql();
|
return $this->dataQuery->query()->sql();
|
||||||
@ -71,6 +98,7 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
* Add a WHERE clause to the query.
|
* Add a WHERE clause to the query.
|
||||||
*
|
*
|
||||||
* @param string $filter
|
* @param string $filter
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function where($filter) {
|
public function where($filter) {
|
||||||
$this->dataQuery->where($filter);
|
$this->dataQuery->where($filter);
|
||||||
@ -79,6 +107,10 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the sort order of this data list
|
* Set the sort order of this data list
|
||||||
|
*
|
||||||
|
* @param string $sort
|
||||||
|
* @param string $direction
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function sort($sort, $direction = "ASC") {
|
public function sort($sort, $direction = "ASC") {
|
||||||
if($direction && strtoupper($direction) != 'ASC') $sort = "$sort $direction";
|
if($direction && strtoupper($direction) != 'ASC') $sort = "$sort $direction";
|
||||||
@ -88,13 +120,20 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this DataList can be sorted by the given field.
|
* Returns true if this DataList can be sorted by the given field.
|
||||||
|
*
|
||||||
|
* @param string $fieldName
|
||||||
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function canSortBy($field) {
|
public function canSortBy($fieldName) {
|
||||||
return $this->dataQuery()->query()->canSortBy($field);
|
return $this->dataQuery()->query()->canSortBy($fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an join clause to this data list's query.
|
* Add an join clause to this data list's query.
|
||||||
|
*
|
||||||
|
* @param type $join
|
||||||
|
* @return DataList
|
||||||
|
* @deprecated 3.0
|
||||||
*/
|
*/
|
||||||
public function join($join) {
|
public function join($join) {
|
||||||
Deprecation::notice('3.0', 'Use innerJoin() or leftJoin() instead.');
|
Deprecation::notice('3.0', 'Use innerJoin() or leftJoin() instead.');
|
||||||
@ -104,6 +143,8 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Restrict the records returned in this query by a limit clause
|
* Restrict the records returned in this query by a limit clause
|
||||||
|
*
|
||||||
|
* @param string $limit
|
||||||
*/
|
*/
|
||||||
public function limit($limit) {
|
public function limit($limit) {
|
||||||
$this->dataQuery->limit($limit);
|
$this->dataQuery->limit($limit);
|
||||||
@ -112,6 +153,11 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an inner join clause to this data list's query.
|
* Add an inner join clause to this data list's query.
|
||||||
|
*
|
||||||
|
* @param string $table
|
||||||
|
* @param string $onClause
|
||||||
|
* @param string $alias - if you want this table to be aliased under another name
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function innerJoin($table, $onClause, $alias = null) {
|
public function innerJoin($table, $onClause, $alias = null) {
|
||||||
$this->dataQuery->innerJoin($table, $onClause, $alias);
|
$this->dataQuery->innerJoin($table, $onClause, $alias);
|
||||||
@ -120,6 +166,11 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an left join clause to this data list's query.
|
* Add an left join clause to this data list's query.
|
||||||
|
*
|
||||||
|
* @param string $table
|
||||||
|
* @param string $onClause
|
||||||
|
* @param string $alias - if you want this table to be aliased under another name
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function leftJoin($table, $onClause, $alias = null) {
|
public function leftJoin($table, $onClause, $alias = null) {
|
||||||
$this->dataQuery->leftJoin($table, $onClause, $alias);
|
$this->dataQuery->leftJoin($table, $onClause, $alias);
|
||||||
@ -142,9 +193,13 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return this list as an array and every object it as an sub array as well
|
||||||
|
*
|
||||||
|
* @return type
|
||||||
|
*/
|
||||||
public function toNestedArray() {
|
public function toNestedArray() {
|
||||||
$result = array();
|
$result = array();
|
||||||
|
|
||||||
foreach($this as $item) {
|
foreach($this as $item) {
|
||||||
$result[] = $item->toMap();
|
$result[] = $item->toMap();
|
||||||
}
|
}
|
||||||
@ -161,22 +216,37 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
return $val;
|
return $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
public function map($keyField = 'ID', $titleField = 'Title') {
|
public function map($keyField = 'ID', $titleField = 'Title') {
|
||||||
return new SS_Map($this, $keyField, $titleField);
|
return new SS_Map($this, $keyField, $titleField);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a data object from the given SQL row
|
* Create a DataObject from the given SQL row
|
||||||
|
*
|
||||||
|
* @param array $row
|
||||||
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
protected function createDataObject($row) {
|
protected function createDataObject($row) {
|
||||||
$defaultClass = $this->dataClass;
|
$defaultClass = $this->dataClass;
|
||||||
|
|
||||||
// Failover from RecordClassName to ClassName
|
// Failover from RecordClassName to ClassName
|
||||||
if(empty($row['RecordClassName'])) $row['RecordClassName'] = $row['ClassName'];
|
if(empty($row['RecordClassName'])) {
|
||||||
|
$row['RecordClassName'] = $row['ClassName'];
|
||||||
|
}
|
||||||
|
|
||||||
// Instantiate the class mentioned in RecordClassName only if it exists, otherwise default to $this->dataClass
|
// Instantiate the class mentioned in RecordClassName only if it exists, otherwise default to $this->dataClass
|
||||||
if(class_exists($row['RecordClassName'])) $item = new $row['RecordClassName']($row, false, $this->model);
|
if(class_exists($row['RecordClassName'])) {
|
||||||
else $item = new $defaultClass($row, false, $this->model);
|
$item = new $row['RecordClassName']($row, false, $this->model);
|
||||||
|
} else {
|
||||||
|
$item = new $defaultClass($row, false, $this->model);
|
||||||
|
}
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
@ -184,6 +254,7 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
/**
|
/**
|
||||||
* Returns an Iterator for this DataList.
|
* Returns an Iterator for this DataList.
|
||||||
* This function allows you to use DataLists in foreach loops
|
* This function allows you to use DataLists in foreach loops
|
||||||
|
*
|
||||||
* @return ArrayIterator
|
* @return ArrayIterator
|
||||||
*/
|
*/
|
||||||
public function getIterator() {
|
public function getIterator() {
|
||||||
@ -192,44 +263,60 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the number of items in this DataList
|
* Return the number of items in this DataList
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
function count() {
|
public function count() {
|
||||||
return $this->dataQuery->count();
|
return $this->dataQuery->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the maximum value of the given field in this DataList
|
* Return the maximum value of the given field in this DataList
|
||||||
|
*
|
||||||
|
* @param string $fieldName
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function Max($field) {
|
public function max($fieldName) {
|
||||||
return $this->dataQuery->max($field);
|
return $this->dataQuery->max($fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the minimum value of the given field in this DataList
|
* Return the minimum value of the given field in this DataList
|
||||||
|
*
|
||||||
|
* @param string $fieldName
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function Min($field) {
|
public function min($fieldName) {
|
||||||
return $this->dataQuery->min($field);
|
return $this->dataQuery->min($fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the average value of the given field in this DataList
|
* Return the average value of the given field in this DataList
|
||||||
|
*
|
||||||
|
* @param string $fieldName
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function Avg($field) {
|
public function avg($fieldName) {
|
||||||
return $this->dataQuery->avg($field);
|
return $this->dataQuery->avg($fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the sum of the values of the given field in this DataList
|
* Return the sum of the values of the given field in this DataList
|
||||||
|
*
|
||||||
|
* @param string $fieldName
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
function Sum($field) {
|
public function sum($fieldName) {
|
||||||
return $this->dataQuery->sum($field);
|
return $this->dataQuery->sum($fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the first item in this DataList
|
* Returns the first item in this DataList
|
||||||
|
*
|
||||||
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
function First() {
|
public function first() {
|
||||||
foreach($this->dataQuery->firstRow()->execute() as $row) {
|
foreach($this->dataQuery->firstRow()->execute() as $row) {
|
||||||
return $this->createDataObject($row);
|
return $this->createDataObject($row);
|
||||||
}
|
}
|
||||||
@ -237,8 +324,10 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the last item in this DataList
|
* Returns the last item in this DataList
|
||||||
|
*
|
||||||
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
function Last() {
|
public function last() {
|
||||||
foreach($this->dataQuery->lastRow()->execute() as $row) {
|
foreach($this->dataQuery->lastRow()->execute() as $row) {
|
||||||
return $this->createDataObject($row);
|
return $this->createDataObject($row);
|
||||||
}
|
}
|
||||||
@ -246,20 +335,30 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this DataList has items
|
* Returns true if this DataList has items
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function exists() {
|
public function exists() {
|
||||||
return $this->count() > 0;
|
return $this->count() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a sub-range of this dataobjectset as an array
|
* Get a sub-range of this dataobjectset as an array
|
||||||
|
*
|
||||||
|
* @param int $offset
|
||||||
|
* @param int $length
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function getRange($offset, $length) {
|
public function getRange($offset, $length) {
|
||||||
return $this->limit(array('start' => $offset, 'limit' => $length));
|
return $this->limit(array('start' => $offset, 'limit' => $length));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find an element of this DataList where the given key = value
|
* Find the first DataObject of this DataList where the given key = value
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param string $value
|
||||||
|
* @return DataObject|null
|
||||||
*/
|
*/
|
||||||
public function find($key, $value) {
|
public function find($key, $value) {
|
||||||
$clone = clone $this;
|
$clone = clone $this;
|
||||||
@ -276,17 +375,22 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter this list to only contain the given IDs
|
* Filter this list to only contain the given Primary IDs
|
||||||
|
*
|
||||||
|
* @param array $ids
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function byIDs(array $ids) {
|
public function byIDs(array $ids) {
|
||||||
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
||||||
$this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
|
$this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the item of the given ID
|
* Return the first DataObject with the given ID
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
public function byID($id) {
|
public function byID($id) {
|
||||||
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
||||||
@ -294,21 +398,24 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a single column from this DataList.
|
* Returns an array of a single field value for all items in the list.
|
||||||
* @param $colNum The DataObject field to return.
|
*
|
||||||
|
* @param string $colName
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
function column($colName = "ID") {
|
public function column($colName = "ID") {
|
||||||
return $this->dataQuery->column($colName);
|
return $this->dataQuery->column($colName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Member altering methods
|
// Member altering methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the ComponentSet to be the given ID list.
|
* Sets the ComponentSet to be the given ID list.
|
||||||
* Records will be added and deleted as appropriate.
|
* Records will be added and deleted as appropriate.
|
||||||
|
*
|
||||||
* @param array $idList List of IDs.
|
* @param array $idList List of IDs.
|
||||||
*/
|
*/
|
||||||
function setByIDList($idList) {
|
public function setByIDList($idList) {
|
||||||
$has = array();
|
$has = array();
|
||||||
|
|
||||||
// Index current data
|
// Index current data
|
||||||
@ -334,8 +441,9 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array with both the keys and values set to the IDs of the records in this list.
|
* Returns an array with both the keys and values set to the IDs of the records in this list.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
function getIDList() {
|
public function getIDList() {
|
||||||
$ids = $this->column("ID");
|
$ids = $this->column("ID");
|
||||||
return $ids ? array_combine($ids, $ids) : array();
|
return $ids ? array_combine($ids, $ids) : array();
|
||||||
}
|
}
|
||||||
@ -348,71 +456,95 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
* Example: Get members from all Groups:
|
* Example: Get members from all Groups:
|
||||||
*
|
*
|
||||||
* DataObject::get("Group")->relation("Members")
|
* DataObject::get("Group")->relation("Members")
|
||||||
|
*
|
||||||
|
* @param string $relationName
|
||||||
|
* @return HasManyList|ManyManyList
|
||||||
*/
|
*/
|
||||||
|
public function relation($relationName) {
|
||||||
function relation($relationName) {
|
|
||||||
$ids = $this->column('ID');
|
$ids = $this->column('ID');
|
||||||
return singleton($this->dataClass)->$relationName()->forForeignID($ids);
|
return singleton($this->dataClass)->$relationName()->forForeignID($ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a number of items to the component set.
|
* Add a number of items to the component set.
|
||||||
|
*
|
||||||
* @param array $items Items to add, as either DataObjects or IDs.
|
* @param array $items Items to add, as either DataObjects or IDs.
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
function addMany($items) {
|
public function addMany($items) {
|
||||||
foreach($items as $item) {
|
foreach($items as $item) {
|
||||||
$this->add($item);
|
$this->add($item);
|
||||||
}
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the items from this list with the given IDs
|
* Remove the items from this list with the given IDs
|
||||||
|
*
|
||||||
|
* @param array $idList
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
function removeMany($idList) {
|
public function removeMany($idList) {
|
||||||
foreach($idList as $id) {
|
foreach($idList as $id) {
|
||||||
$this->removeByID($id);
|
$this->removeByID($id);
|
||||||
}
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove every element in this DataList matching the given $filter.
|
* Remove every element in this DataList matching the given $filter.
|
||||||
|
*
|
||||||
|
* @param string $filter - a sql type where filter
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
function removeByFilter($filter) {
|
public function removeByFilter($filter) {
|
||||||
foreach($this->where($filter) as $item) {
|
foreach($this->where($filter) as $item) {
|
||||||
$this->remove($item);
|
$this->remove($item);
|
||||||
}
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove every element in this DataList.
|
* Remove every element in this DataList.
|
||||||
|
*
|
||||||
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
function removeAll() {
|
public function removeAll() {
|
||||||
foreach($this as $item) {
|
foreach($this as $item) {
|
||||||
$this->remove($item);
|
$this->remove($item);
|
||||||
}
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// These methods are overloaded by HasManyList and ManyMany list to perform
|
/**
|
||||||
// more sophisticated list manipulation
|
* This method are overloaded by HasManyList and ManyMany list to perform more sophisticated
|
||||||
|
* list manipulation
|
||||||
function add($item) {
|
*
|
||||||
|
* @param type $item
|
||||||
|
*/
|
||||||
|
public function add($item) {
|
||||||
// Nothing needs to happen by default
|
// Nothing needs to happen by default
|
||||||
// TO DO: If a filter is given to this data list then
|
// TO DO: If a filter is given to this data list then
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new item to add to this DataList.
|
* Return a new item to add to this DataList.
|
||||||
|
*
|
||||||
* @todo This doesn't factor in filters.
|
* @todo This doesn't factor in filters.
|
||||||
*/
|
*/
|
||||||
function newObject($initialFields = null) {
|
public function newObject($initialFields = null) {
|
||||||
$class = $this->dataClass;
|
$class = $this->dataClass;
|
||||||
return new $class($initialFields, false, $this->model);
|
return new $class($initialFields, false, $this->model);
|
||||||
}
|
}
|
||||||
|
|
||||||
function remove($item) {
|
/**
|
||||||
// TO DO: Allow for amendment of this behaviour - for exmaple, we can remove an item from
|
* Remove this item by deleting it
|
||||||
// an "ActiveItems" DataList by chaning the status to inactive.
|
*
|
||||||
|
* @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) {
|
||||||
// By default, we remove an item from a DataList by deleting it.
|
// By default, we remove an item from a DataList by deleting it.
|
||||||
if($item instanceof $this->dataClass) $item->delete();
|
if($item instanceof $this->dataClass) $item->delete();
|
||||||
|
|
||||||
@ -420,35 +552,67 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove an item from this DataList by ID
|
* Remove an item from this DataList by ID
|
||||||
|
*
|
||||||
|
* @param int $itemID - The primary ID
|
||||||
*/
|
*/
|
||||||
function removeByID($itemID) {
|
public function removeByID($itemID) {
|
||||||
$item = $this->byID($itemID);
|
$item = $this->byID($itemID);
|
||||||
if($item) return $item->delete();
|
if($item) return $item->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methods that won't function on DataLists
|
/**
|
||||||
|
* This method won't function on DataLists due to the specific query that it represent
|
||||||
function push($item) {
|
*
|
||||||
|
* @param mixed $item
|
||||||
|
*/
|
||||||
|
public function push($item) {
|
||||||
user_error("Can't call DataList::push() because its data comes from a specific query.", E_USER_ERROR);
|
user_error("Can't call DataList::push() because its data comes from a specific query.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
function insertFirst($item) {
|
|
||||||
|
/**
|
||||||
|
* This method won't function on DataLists due to the specific query that it represent
|
||||||
|
*
|
||||||
|
* @param mixed $item
|
||||||
|
*/
|
||||||
|
public function insertFirst($item) {
|
||||||
user_error("Can't call DataList::insertFirst() because its data comes from a specific query.", E_USER_ERROR);
|
user_error("Can't call DataList::insertFirst() because its data comes from a specific query.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
function shift() {
|
|
||||||
|
/**
|
||||||
|
* This method won't function on DataLists due to the specific query that it represent
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function shift() {
|
||||||
user_error("Can't call DataList::shift() because its data comes from a specific query.", E_USER_ERROR);
|
user_error("Can't call DataList::shift() because its data comes from a specific query.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
function replace() {
|
|
||||||
|
/**
|
||||||
|
* This method won't function on DataLists due to the specific query that it represent
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function replace() {
|
||||||
user_error("Can't call DataList::replace() because its data comes from a specific query.", E_USER_ERROR);
|
user_error("Can't call DataList::replace() because its data comes from a specific query.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
function merge() {
|
|
||||||
|
/**
|
||||||
|
* This method won't function on DataLists due to the specific query that it represent
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function merge() {
|
||||||
user_error("Can't call DataList::merge() because its data comes from a specific query.", E_USER_ERROR);
|
user_error("Can't call DataList::merge() because its data comes from a specific query.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
function removeDuplicates() {
|
|
||||||
|
/**
|
||||||
|
* This method won't function on DataLists due to the specific query that it represent
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function removeDuplicates() {
|
||||||
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.", E_USER_ERROR);
|
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Necessary for interface ArrayAccess. Returns whether an item with $key exists
|
* Returns whether an item with $key exists
|
||||||
|
*
|
||||||
* @param mixed $key
|
* @param mixed $key
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -457,7 +621,8 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Necessary for interface ArrayAccess. Returns item stored in array with index $key
|
* Returns item stored in list with index $key
|
||||||
|
*
|
||||||
* @param mixed $key
|
* @param mixed $key
|
||||||
* @return DataObject
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
@ -466,20 +631,22 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Necessary for interface ArrayAccess. Set an item with the key in $key
|
* Set an item with the key in $key
|
||||||
|
*
|
||||||
* @param mixed $key
|
* @param mixed $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
public function offsetSet($key, $value) {
|
public function offsetSet($key, $value) {
|
||||||
throw new Exception("Can't alter items in a DataList using array-access");
|
user_error("Can't alter items in a DataList using array-access", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Necessary for interface ArrayAccess. Unset an item with the key in $key
|
* Unset an item with the key in $key
|
||||||
|
*
|
||||||
* @param mixed $key
|
* @param mixed $key
|
||||||
*/
|
*/
|
||||||
public function offsetUnset($key) {
|
public function offsetUnset($key) {
|
||||||
throw new Exception("Can't alter items in a DataList using array-access");
|
user_error("Can't alter items in a DataList using array-access", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate {
|
|||||||
/**
|
/**
|
||||||
* Returns an array of a single field value for all items in the list.
|
* Returns an array of a single field value for all items in the list.
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $colName
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function column($colName = "ID");
|
public function column($colName = "ID");
|
||||||
|
Loading…
Reference in New Issue
Block a user