2011-05-01 07:33:02 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Representation of a DataModel - a collection of DataLists for each different
|
2013-05-20 12:18:07 +02:00
|
|
|
* data type.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-01 07:33:02 +02:00
|
|
|
* Usage:
|
2013-05-20 12:18:07 +02:00
|
|
|
* <code>
|
2011-05-01 07:33:02 +02:00
|
|
|
* $model = new DataModel;
|
|
|
|
* $mainMenu = $model->SiteTree->where('"ParentID" = 0 AND "ShowInMenus" = 1');
|
2013-05-20 12:18:07 +02:00
|
|
|
* </code>
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
|
|
|
class DataModel {
|
2013-05-20 12:18:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DataModel
|
|
|
|
*/
|
2011-05-01 07:33:02 +02:00
|
|
|
protected static $inst;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
/**
|
|
|
|
* @var array $customDataLists
|
|
|
|
*/
|
|
|
|
protected $customDataLists = array();
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* Get the global DataModel.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
|
|
|
* @return DataModel
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function inst() {
|
2013-05-20 12:18:07 +02:00
|
|
|
if(!self::$inst) {
|
|
|
|
self::$inst = new self;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
return self::$inst;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Set the global DataModel, used when data is requested from static
|
2013-05-20 12:18:07 +02:00
|
|
|
* methods.
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param DataModel $inst
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_inst(DataModel $inst) {
|
2011-05-01 07:33:02 +02:00
|
|
|
self::$inst = $inst;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __get($class) {
|
2011-05-01 07:33:02 +02:00
|
|
|
if(isset($this->customDataLists[$class])) {
|
|
|
|
return clone $this->customDataLists[$class];
|
|
|
|
} else {
|
|
|
|
$list = DataList::create($class);
|
2012-05-01 04:43:52 +02:00
|
|
|
$list->setDataModel($this);
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $class
|
|
|
|
* @param DataList $item
|
2013-05-20 12:18:07 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __set($class, $item) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$item = clone $item;
|
2012-05-01 04:43:52 +02:00
|
|
|
$item->setDataModel($this);
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->customDataLists[$class] = $item;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|