2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implements a "lazy loading" DataObjectSet.
|
|
|
|
* Uses {@link DataQuery} to do the actual query generation.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-05-02 10:14:55 +02:00
|
|
|
class DataList extends ViewableData implements SS_List {
|
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
|
|
|
/**
|
|
|
|
* Synonym of the constructor. Can be chained with literate methods.
|
|
|
|
* DataList::create("SiteTree")->sort("Title") is legal, but
|
|
|
|
* new DataList("SiteTree")->sort("Title") is not.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $dataClass - The DataObject class to query.
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public static function create($dataClass) {
|
2009-11-22 06:16:38 +01:00
|
|
|
return new DataList($dataClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2011-12-07 02:35:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the DataModel
|
|
|
|
*
|
|
|
|
* @param DataModel $model
|
|
|
|
*/
|
2011-05-01 07:33:02 +02:00
|
|
|
public function setModel(DataModel $model) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the internal {@link DataQuery} object for direct manipulation
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @return DataQuery
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function dataQuery() {
|
|
|
|
return $this->dataQuery;
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-05 13:01:57 +02:00
|
|
|
* Add a WHERE clause to the query.
|
|
|
|
*
|
|
|
|
* @param string $filter
|
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) {
|
|
|
|
$this->dataQuery->where($filter);
|
2009-11-22 06:16:38 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return $this->dataQuery()->query()->canSortBy($fieldName);
|
2011-03-30 03:19:27 +02:00
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an join clause to this data list's query.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param type $join
|
|
|
|
* @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.');
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->dataQuery->join($join);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restrict the records returned in this query by a limit clause
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $limit
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function limit($limit) {
|
|
|
|
$this->dataQuery->limit($limit);
|
|
|
|
return $this;
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the sort order of this data list
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
* @see SS_List::sort()
|
|
|
|
* @example $list->sort('Name'); // default ASC sorting
|
|
|
|
* @example $list->sort('Name DESC'); // DESC sorting
|
|
|
|
* @example $list->sort('Name', 'ASC');
|
|
|
|
* @example $list->sort(array('Name'=>'ASC,'Age'=>'DESC'));
|
|
|
|
*/
|
|
|
|
public function sort() {
|
|
|
|
if(count(func_get_args())==0){
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
if(count(func_get_args())>2){
|
|
|
|
throw new InvalidArgumentException('This method takes zero, one or two arguments');
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort('Name','Desc')
|
|
|
|
if(count(func_get_args())==2){
|
|
|
|
if(!in_array(strtolower(func_get_arg(1)),array('desc','asc'))){
|
|
|
|
user_error('Second argument to sort must be either ASC or DESC');
|
|
|
|
}
|
|
|
|
$this->dataQuery->sort(func_get_arg(0).' '.func_get_arg(1));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort('Name') - default to ASC sorting if not specified
|
|
|
|
if(is_string(func_get_arg(0)) && func_get_arg(0)){
|
|
|
|
// sort('Name ASC')
|
|
|
|
if(stristr(func_get_arg(0), ' asc') || stristr(func_get_arg(0), ' desc')){
|
|
|
|
$this->dataQuery->sort(func_get_arg(0));
|
|
|
|
} else {
|
|
|
|
$this->dataQuery->sort(func_get_arg(0).' ASC');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort(array('Name'=>'desc'));
|
|
|
|
$argumentArray = func_get_arg(0);
|
|
|
|
if(is_array($argumentArray)){
|
|
|
|
$sort = array();
|
|
|
|
foreach($argumentArray as $column => $direction) {
|
|
|
|
$sort []= '"'.$column.'" '.$direction;
|
|
|
|
}
|
|
|
|
$this->dataQuery->sort(implode(',', $sort));
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the list to include items with these charactaristics
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
* @see SS_List::filter()
|
|
|
|
* @example $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
* @example $list->filter('Name', array('aziz', 'bob'); // aziz and bob in list
|
|
|
|
* @example $list->filter(array('Name'=>'bob, 'Age'=>21)); // bob with the age 21
|
|
|
|
* @example $list->filter(array('Name'=>'bob, 'Age'=>array(21, 43))); // bob with the Age 21 or 43
|
|
|
|
* @example $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
|
|
|
|
*
|
|
|
|
* @todo extract the sql from $customQuery into a SQLGenerator class
|
|
|
|
*/
|
|
|
|
public function filter() {
|
|
|
|
$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);
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException('Arguments passed to filter() is wrong');
|
|
|
|
}
|
|
|
|
|
|
|
|
$SQL_Statements = array();
|
|
|
|
foreach($whereArguments as $field => $value) {
|
|
|
|
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){
|
|
|
|
$comparisor = $this->applyFilterContext($field, $fieldArg, $value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$SQL_Statements[] = '"'.Convert::raw2sql($field).'" '.$customQuery;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count($SQL_Statements)) {
|
|
|
|
foreach($SQL_Statements as $SQL_Statement){
|
|
|
|
$this->dataQuery->where($SQL_Statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* Translates the comparisator to the sql query
|
|
|
|
*
|
|
|
|
* @param string $field - the fieldname in the db
|
|
|
|
* @param string $comparisators - example StartsWith, relates to a filtercontext
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
private function applyFilterContext($field, $comparisators, $value) {
|
|
|
|
$t = singleton($this->dataClass())->dbObject($field);
|
|
|
|
$className = "{$comparisators}Filter";
|
|
|
|
if(!class_exists($className)){
|
|
|
|
throw new InvalidArgumentException('There are no '.$comparisators.' comparisator');
|
|
|
|
}
|
|
|
|
$t = new $className($field,$value);
|
|
|
|
$t->apply($this->dataQuery());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exclude the list to not contain items with these charactaristics
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
* @see SS_List::exclude()
|
|
|
|
* @example $list->exclude('Name', 'bob'); // exclude bob from list
|
|
|
|
* @example $list->exclude('Name', array('aziz', 'bob'); // exclude aziz and bob from list
|
|
|
|
* @example $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude bob that has Age 21
|
|
|
|
* @example $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43
|
|
|
|
* @example $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43))); // bob age 21 or 43, phil age 21 or 43 would be excluded
|
|
|
|
*
|
|
|
|
* @todo extract the sql from this method into a SQLGenerator class
|
|
|
|
*/
|
|
|
|
public function exclude(){
|
|
|
|
$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);
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException('Arguments passed to exclude() is wrong');
|
|
|
|
}
|
|
|
|
|
|
|
|
$SQL_Statements = array();
|
|
|
|
foreach($whereArguments as $fieldName => $value) {
|
|
|
|
if(is_array($value)){
|
|
|
|
$SQL_Statements[] = ('"'.$fieldName.'" NOT IN (\''.implode('\',\'', Convert::raw2sql($value)).'\')');
|
|
|
|
} else {
|
|
|
|
$SQL_Statements[] = ('"'.$fieldName.'" != \''.Convert::raw2sql($value).'\'');
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 14:09:07 +01:00
|
|
|
$this->dataQuery->whereAny($SQL_Statements);
|
2011-12-08 22:08:46 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
|
|
|
* Add an inner join clause to this data list's query.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param string $onClause
|
|
|
|
* @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) {
|
|
|
|
$this->dataQuery->innerJoin($table, $onClause, $alias);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an left join clause to this data list's query.
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param string $onClause
|
|
|
|
* @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) {
|
|
|
|
$this->dataQuery->leftJoin($table, $onClause, $alias);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
foreach($rows as $row) {
|
|
|
|
$results[] = $this->createDataObject($row);
|
|
|
|
}
|
|
|
|
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();
|
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>";
|
|
|
|
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'])) {
|
|
|
|
$item = new $row['RecordClassName']($row, false, $this->model);
|
|
|
|
} else {
|
|
|
|
$item = new $defaultClass($row, false, $this->model);
|
|
|
|
}
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
return $this->limit(array('start' => $offset, 'limit' => $length));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
$clone = clone $this;
|
|
|
|
|
|
|
|
if($key == 'ID') {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
|
|
|
$SQL_col = "\"$baseClass\".\"$key\"";
|
|
|
|
} else {
|
|
|
|
$SQL_col = "\"$key\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clone->where("$SQL_col = '" . Convert::raw2sql($value) . "'")->First();
|
2009-11-22 06:16:38 +01: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
|
|
|
|
*
|
|
|
|
* @param array $ids
|
|
|
|
* @return DataList
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
|
|
|
public function byIDs(array $ids) {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->dataClass);
|
2011-04-05 13:01:57 +02:00
|
|
|
$this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
|
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);
|
2011-04-05 13:01:57 +02:00
|
|
|
return $this->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) {
|
|
|
|
$has[$id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
*
|
|
|
|
* DataObject::get("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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
return new $class($initialFields, false, $this->model);
|
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-30 03:19:27 +02:00
|
|
|
/**
|
|
|
|
* Remove an item from this DataList by ID
|
2011-12-07 02:35:30 +01:00
|
|
|
*
|
|
|
|
* @param int $itemID - The primary ID
|
2011-03-30 03:19:27 +02:00
|
|
|
*/
|
2011-12-07 02:35:30 +01:00
|
|
|
public function removeByID($itemID) {
|
2011-03-30 03:19:27 +02:00
|
|
|
$item = $this->byID($itemID);
|
|
|
|
if($item) return $item->delete();
|
|
|
|
}
|
|
|
|
|
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() {
|
2009-11-22 06:16:38 +01:00
|
|
|
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.", E_USER_ERROR);
|
|
|
|
}
|
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) {
|
|
|
|
return ($this->getRange($key, 1)->First() != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
|
|
|
return $this->getRange($key, 1)->First();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
2011-12-07 02:35:30 +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) {
|
2011-12-07 02:35:30 +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
|
|
|
|
|
|
|
}
|