silverstripe-framework/ORM/DataModel.php

77 lines
1.3 KiB
PHP
Raw Normal View History

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