2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
use SilverStripe\Control\HTTP;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormField;
|
|
|
|
use SilverStripe\Forms\FormScaffolder;
|
|
|
|
use SilverStripe\i18n\i18n;
|
|
|
|
use SilverStripe\i18n\i18nEntityProvider;
|
|
|
|
use SilverStripe\ORM\Filters\SearchFilter;
|
|
|
|
use SilverStripe\ORM\Search\SearchContext;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLInsert;
|
|
|
|
use SilverStripe\ORM\Queries\SQLDelete;
|
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\FieldType\DBComposite;
|
|
|
|
use SilverStripe\ORM\FieldType\DBClassName;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ViewableData;
|
|
|
|
use LogicException;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use BadMethodCallException;
|
|
|
|
use Exception;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* A single database record & abstract class for the data-access-model.
|
2008-09-22 18:02:03 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* <h2>Extensions</h2>
|
2009-03-31 21:34:37 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* See {@link Extension} and {@link DataExtension}.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-03-31 21:34:37 +02:00
|
|
|
* <h2>Permission Control</h2>
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-09-22 18:02:03 +02:00
|
|
|
* Object-level access control by {@link Permission}. Permission codes are arbitrary
|
|
|
|
* strings which can be selected on a group-by-group basis.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-09-22 18:02:03 +02:00
|
|
|
* <code>
|
|
|
|
* class Article extends DataObject implements PermissionProvider {
|
2012-12-08 12:20:20 +01:00
|
|
|
* static $api_access = true;
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-12-08 12:20:20 +01:00
|
|
|
* function canView($member = false) {
|
|
|
|
* return Permission::check('ARTICLE_VIEW');
|
|
|
|
* }
|
|
|
|
* function canEdit($member = false) {
|
|
|
|
* return Permission::check('ARTICLE_EDIT');
|
|
|
|
* }
|
|
|
|
* function canDelete() {
|
|
|
|
* return Permission::check('ARTICLE_DELETE');
|
|
|
|
* }
|
|
|
|
* function canCreate() {
|
|
|
|
* return Permission::check('ARTICLE_CREATE');
|
|
|
|
* }
|
|
|
|
* function providePermissions() {
|
|
|
|
* return array(
|
|
|
|
* 'ARTICLE_VIEW' => 'Read an article object',
|
|
|
|
* 'ARTICLE_EDIT' => 'Edit an article object',
|
|
|
|
* 'ARTICLE_DELETE' => 'Delete an article object',
|
|
|
|
* 'ARTICLE_CREATE' => 'Create an article object',
|
|
|
|
* );
|
|
|
|
* }
|
2008-09-22 18:02:03 +02:00
|
|
|
* }
|
2013-06-21 00:32:08 +02:00
|
|
|
* </code>
|
2008-09-22 18:02:03 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Object-level access control by {@link Group} membership:
|
2008-09-22 18:02:03 +02:00
|
|
|
* <code>
|
|
|
|
* class Article extends DataObject {
|
2012-12-08 12:20:20 +01:00
|
|
|
* static $api_access = true;
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-12-08 12:20:20 +01:00
|
|
|
* function canView($member = false) {
|
|
|
|
* if(!$member) $member = Member::currentUser();
|
|
|
|
* return $member->inGroup('Subscribers');
|
|
|
|
* }
|
|
|
|
* function canEdit($member = false) {
|
|
|
|
* if(!$member) $member = Member::currentUser();
|
|
|
|
* return $member->inGroup('Editors');
|
|
|
|
* }
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-12-08 12:20:20 +01:00
|
|
|
* // ...
|
2008-09-22 18:02:03 +02:00
|
|
|
* }
|
|
|
|
* </code>
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* If any public method on this class is prefixed with an underscore,
|
2009-03-17 21:20:43 +01:00
|
|
|
* the results are cached in memory through {@link cachedCall()}.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
*
|
2009-03-31 21:34:37 +02:00
|
|
|
* @todo Add instance specific removeExtension() which undos loadExtraStatics()
|
|
|
|
* and defineMethods()
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2014-01-26 04:17:17 +01:00
|
|
|
* @property integer ID ID of the DataObject, 0 if the DataObject doesn't exist in database.
|
|
|
|
* @property string ClassName Class name of the DataObject
|
|
|
|
* @property string LastEdited Date and time of DataObject's last modification.
|
|
|
|
* @property string Created Date and time of DataObject creation.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider {
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* Human-readable singular name.
|
|
|
|
* @var string
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $singular_name = null;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
2014-07-29 01:52:59 +02:00
|
|
|
* Human-readable plural name
|
2009-03-14 01:16:32 +01:00
|
|
|
* @var string
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $plural_name = null;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* Allow API access to this object?
|
|
|
|
* @todo Define the options that can be set here
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $api_access = false;
|
2012-04-12 07:03:18 +02:00
|
|
|
|
2016-01-21 05:08:35 +01:00
|
|
|
/**
|
|
|
|
* Allows specification of a default value for the ClassName field.
|
|
|
|
* Configure this value only in subclasses of DataObject.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_classname = null;
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* True if this DataObject has been destroyed.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $destroyed = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* The DataModel from this this object comes
|
|
|
|
*/
|
|
|
|
protected $model;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Data stored in this objects database record. An array indexed by fieldname.
|
|
|
|
*
|
2010-12-06 22:46:24 +01:00
|
|
|
* Use {@link toMap()} if you want an array representation
|
|
|
|
* of this object, as the $record array might contain lazy loaded field aliases.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $record;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
/**
|
|
|
|
* If selected through a many_many through relation, this is the instance of the through record
|
|
|
|
*
|
|
|
|
* @var DataObject
|
|
|
|
*/
|
|
|
|
protected $joinRecord;
|
|
|
|
|
2013-10-17 01:55:58 +02:00
|
|
|
/**
|
|
|
|
* Represents a field that hasn't changed (before === after, thus before == after)
|
|
|
|
*/
|
|
|
|
const CHANGE_NONE = 0;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-10-17 01:55:58 +02:00
|
|
|
/**
|
|
|
|
* Represents a field that has changed type, although not the loosely defined value.
|
|
|
|
* (before !== after && before == after)
|
|
|
|
* E.g. change 1 to true or "true" to true, but not true to 0.
|
|
|
|
* Value changes are by nature also considered strict changes.
|
|
|
|
*/
|
|
|
|
const CHANGE_STRICT = 1;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-10-17 01:55:58 +02:00
|
|
|
/**
|
|
|
|
* Represents a field that has changed the loosely defined value
|
|
|
|
* (before != after, thus, before !== after))
|
|
|
|
* E.g. change false to true, but not false to 0
|
|
|
|
*/
|
|
|
|
const CHANGE_VALUE = 2;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* An array indexed by fieldname, true if the field has been changed.
|
2009-05-27 02:09:23 +02:00
|
|
|
* Use {@link getChangedFields()} and {@link isChanged()} to inspect
|
|
|
|
* the changed state.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2009-05-27 02:09:23 +02:00
|
|
|
private $changed;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The database record (in the same format as $record), before
|
|
|
|
* any changes.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $original;
|
|
|
|
|
2009-05-12 03:58:45 +02:00
|
|
|
/**
|
|
|
|
* Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $brokenOnDelete = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-05-12 03:58:45 +02:00
|
|
|
/**
|
|
|
|
* Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite()
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $brokenOnWrite = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var boolean Should dataobjects be validated before they are written?
|
2013-09-12 15:27:23 +02:00
|
|
|
* Caution: Validation can contain safeguards against invalid/malicious data,
|
|
|
|
* and check permission levels (e.g. on {@link Group}). Therefore it is recommended
|
|
|
|
* to only disable validation for very specific use cases.
|
2009-03-04 04:44:11 +01:00
|
|
|
*/
|
|
|
|
private static $validation_enabled = true;
|
2012-04-12 07:03:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static caches used by relevant functions.
|
2016-10-06 06:31:38 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2012-04-12 07:03:18 +02:00
|
|
|
*/
|
|
|
|
protected static $_cache_get_one;
|
2016-10-06 06:31:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of field labels
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-04-13 18:38:23 +02:00
|
|
|
protected static $_cache_field_labels = array();
|
2012-04-12 07:03:18 +02:00
|
|
|
|
2015-09-10 05:46:23 +02:00
|
|
|
/**
|
|
|
|
* Base fields which are not defined in static $db
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-07-02 15:09:05 +02:00
|
|
|
private static $fixed_fields = array(
|
2015-09-04 05:49:22 +02:00
|
|
|
'ID' => 'PrimaryKey',
|
|
|
|
'ClassName' => 'DBClassName',
|
2016-06-15 06:03:16 +02:00
|
|
|
'LastEdited' => 'DBDatetime',
|
|
|
|
'Created' => 'DBDatetime',
|
2013-07-02 15:09:05 +02:00
|
|
|
);
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Core dataobject extensions
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $extensions = array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'AssetControl' => 'SilverStripe\\Assets\\AssetControlExtension'
|
2015-12-09 22:19:23 +01:00
|
|
|
);
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
/**
|
|
|
|
* Override table name for this class. If ignored will default to FQN of class.
|
|
|
|
* This option is not inheritable, and must be set on each class.
|
|
|
|
* If left blank naming will default to the legacy (3.x) behaviour.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = null;
|
|
|
|
|
2012-04-12 07:03:18 +02:00
|
|
|
/**
|
|
|
|
* Non-static relationship cache, indexed by component name.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var DataObject[]
|
2012-04-12 07:03:18 +02:00
|
|
|
*/
|
|
|
|
protected $components;
|
|
|
|
|
2012-11-03 04:16:51 +01:00
|
|
|
/**
|
|
|
|
* Non-static cache of has_many and many_many relations that can't be written until this object is saved.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var UnsavedRelationList[]
|
2012-11-03 04:16:51 +01:00
|
|
|
*/
|
|
|
|
protected $unsavedRelations;
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
/**
|
|
|
|
* Get schema object
|
|
|
|
*
|
|
|
|
* @return DataObjectSchema
|
|
|
|
*/
|
|
|
|
public static function getSchema() {
|
2016-10-06 06:31:38 +02:00
|
|
|
return Injector::inst()->get(DataObjectSchema::class);
|
2009-08-11 10:49:52 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Construct a new DataObject.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of
|
2016-06-17 05:47:10 +02:00
|
|
|
* field values. Normally this constructor is only used by the internal systems that get objects from the database.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods.
|
|
|
|
* Singletons don't have their defaults set.
|
2016-03-02 06:18:10 +01:00
|
|
|
* @param DataModel $model
|
|
|
|
* @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-03-02 06:18:10 +01:00
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) {
|
2012-07-02 07:25:14 +02:00
|
|
|
parent::__construct();
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
// Set query params on the DataObject to tell the lazy loading mechanism the context the object creation context
|
|
|
|
$this->setSourceQueryParams($queryParams);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Set the fields data.
|
|
|
|
if(!$record) {
|
2009-04-27 03:36:15 +02:00
|
|
|
$record = array(
|
|
|
|
'ID' => 0,
|
2016-10-06 06:31:38 +02:00
|
|
|
'ClassName' => static::class,
|
|
|
|
'RecordClassName' => static::class
|
2009-04-27 03:36:15 +02:00
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-11-02 00:57:07 +01:00
|
|
|
if(!is_array($record) && !is_a($record, "stdClass")) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(is_object($record)) {
|
|
|
|
$passed = "an object of type '".get_class($record)."'";
|
|
|
|
} else {
|
|
|
|
$passed = "The value '$record'";
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("DataObject::__construct passed $passed. It's supposed to be passed an array,"
|
|
|
|
. " taken straight from the database. Perhaps you should use DataList::create()->First(); instead?",
|
|
|
|
E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
$record = null;
|
|
|
|
}
|
|
|
|
|
2015-05-21 05:30:19 +02:00
|
|
|
if(is_a($record, "stdClass")) {
|
|
|
|
$record = (array)$record;
|
|
|
|
}
|
|
|
|
|
2009-05-07 08:00:50 +02:00
|
|
|
// Set $this->record to $record, but ignore NULLs
|
|
|
|
$this->record = array();
|
|
|
|
foreach($record as $k => $v) {
|
2010-10-13 03:19:33 +02:00
|
|
|
// Ensure that ID is stored as a number and not a string
|
|
|
|
// To do: this kind of clean-up should be done on all numeric fields, in some relatively
|
|
|
|
// performant manner
|
|
|
|
if($v !== null) {
|
|
|
|
if($k == 'ID' && is_numeric($v)) $this->record[$k] = (int)$v;
|
|
|
|
else $this->record[$k] = $v;
|
|
|
|
}
|
2009-05-07 08:00:50 +02:00
|
|
|
}
|
2012-04-20 00:08:17 +02:00
|
|
|
|
|
|
|
// Identify fields that should be lazy loaded, but only on existing records
|
|
|
|
if(!empty($record['ID'])) {
|
2016-10-06 06:31:38 +02:00
|
|
|
// Get all field specs scoped to class for later lazy loading
|
|
|
|
$fields = static::getSchema()->fieldSpecs(static::class, ['includeClass', 'dbOnly']);
|
|
|
|
foreach($fields as $field => $fieldSpec) {
|
|
|
|
$fieldClass = strtok($fieldSpec, ".");
|
|
|
|
if(!array_key_exists($field, $record)) {
|
|
|
|
$this->record[$field.'_Lazy'] = $fieldClass;
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 08:00:50 +02:00
|
|
|
$this->original = $this->record;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Keep track of the modification date of all the data sourced to make this page
|
|
|
|
// From this we create a Last-Modified HTTP header
|
|
|
|
if(isset($record['LastEdited'])) {
|
|
|
|
HTTP::register_modification_date($record['LastEdited']);
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:57:11 +02:00
|
|
|
// this must be called before populateDefaults(), as field getters on a DataObject
|
|
|
|
// may call getComponent() and others, which rely on $this->model being set.
|
|
|
|
$this->model = $model ? $model : DataModel::inst();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Must be called after parent constructor
|
2008-02-25 03:10:37 +01:00
|
|
|
if(!$isSingleton && (!isset($this->record['ID']) || !$this->record['ID'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->populateDefaults();
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// prevent populateDefaults() and setField() from marking overwritten defaults as changed
|
|
|
|
$this->changed = array();
|
2011-05-01 07:33:02 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
/**
|
|
|
|
* Set the DataModel
|
2013-02-12 11:21:15 +01:00
|
|
|
* @param DataModel $model
|
|
|
|
* @return DataObject $this
|
2011-05-01 07:33:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setDataModel(DataModel $model) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$this->model = $model;
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-04-12 07:03:18 +02:00
|
|
|
* Destroy all of this objects dependant objects and local caches.
|
2007-07-19 12:40:28 +02:00
|
|
|
* You'll need to call this to get the memory of an object that has components or extensions freed.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function destroy() {
|
2012-06-11 12:38:59 +02:00
|
|
|
//$this->destroyed = true;
|
|
|
|
gc_collect_cycles();
|
2010-04-12 07:04:34 +02:00
|
|
|
$this->flushCache(false);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a duplicate of this node.
|
2010-09-16 05:40:34 +02:00
|
|
|
* Note: now also duplicates relations.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param bool $doWrite Perform a write() operation before returning the object.
|
|
|
|
* If this is true, it will create the duplicate in the database.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject A duplicate of this node. The exact type will be the type of this node.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function duplicate($doWrite = true) {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var static $clone */
|
2016-10-06 06:31:38 +02:00
|
|
|
$clone = Injector::inst()->create(static::class, $this->toMap(), false, $this->model );
|
2007-07-19 12:40:28 +02:00
|
|
|
$clone->ID = 0;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-02-05 23:16:18 +01:00
|
|
|
$clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite);
|
2010-09-16 05:40:34 +02:00
|
|
|
if($doWrite) {
|
|
|
|
$clone->write();
|
|
|
|
$this->duplicateManyManyRelations($this, $clone);
|
|
|
|
}
|
2013-02-05 23:16:18 +01:00
|
|
|
$clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $clone;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2010-09-16 05:40:34 +02:00
|
|
|
/**
|
|
|
|
* Copies the many_many and belongs_many_many relations from one object to another instance of the name of object
|
2012-09-26 23:34:00 +02:00
|
|
|
* The destinationObject must be written to the database already and have an ID. Writing is performed
|
|
|
|
* automatically when adding the new relations.
|
2010-09-17 04:27:44 +02:00
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param DataObject $sourceObject the source object to duplicate from
|
|
|
|
* @param DataObject $destinationObject the destination object to populate with the duplicated relations
|
2010-09-17 04:27:44 +02:00
|
|
|
* @return DataObject with the new many_many relations copied in
|
|
|
|
*/
|
2010-09-16 05:40:34 +02:00
|
|
|
protected function duplicateManyManyRelations($sourceObject, $destinationObject) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if (!$destinationObject || $destinationObject->ID < 1) {
|
|
|
|
user_error("Can't duplicate relations for an object that has not been written to the database",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2010-09-16 05:40:34 +02:00
|
|
|
|
|
|
|
//duplicate complex relations
|
2012-09-26 23:34:00 +02:00
|
|
|
// DO NOT copy has_many relations, because copying the relation would result in us changing the has_one
|
|
|
|
// relation on the other side of this relation to point at the copy and no longer the original (being a
|
|
|
|
// has_one, it can only point at one thing at a time). So, all relations except has_many can and are copied
|
2015-02-25 16:21:33 +01:00
|
|
|
if ($sourceObject->hasOne()) foreach($sourceObject->hasOne() as $name => $type) {
|
2010-09-16 05:40:34 +02:00
|
|
|
$this->duplicateRelations($sourceObject, $destinationObject, $name);
|
|
|
|
}
|
2015-02-25 16:21:33 +01:00
|
|
|
if ($sourceObject->manyMany()) foreach($sourceObject->manyMany() as $name => $type) {
|
2012-09-26 23:34:00 +02:00
|
|
|
//many_many include belongs_many_many
|
2010-09-16 05:40:34 +02:00
|
|
|
$this->duplicateRelations($sourceObject, $destinationObject, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $destinationObject;
|
|
|
|
}
|
|
|
|
|
2010-09-17 04:27:44 +02:00
|
|
|
/**
|
|
|
|
* Helper function to duplicate relations from one object to another
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param DataObject $sourceObject the source object to duplicate from
|
|
|
|
* @param DataObject $destinationObject the destination object to populate with the duplicated relations
|
|
|
|
* @param string $name the name of the relation to duplicate (e.g. members)
|
2010-09-17 04:27:44 +02:00
|
|
|
*/
|
2010-09-16 05:40:34 +02:00
|
|
|
private function duplicateRelations($sourceObject, $destinationObject, $name) {
|
|
|
|
$relations = $sourceObject->$name();
|
|
|
|
if ($relations) {
|
2009-11-22 06:16:38 +01:00
|
|
|
if ($relations instanceOf RelationList) { //many-to-something relation
|
2016-05-25 07:30:01 +02:00
|
|
|
if ($relations->count() > 0) { //with more than one thing it is related to
|
2010-09-16 05:40:34 +02:00
|
|
|
foreach($relations as $relation) {
|
|
|
|
$destinationObject->$name()->add($relation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { //one-to-one relation
|
2013-06-03 14:48:52 +02:00
|
|
|
$destinationObject->{"{$name}ID"} = $relations->ID;
|
2010-09-16 05:40:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 04:30:10 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* Return obsolete class name, if this is no longer a valid class
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getObsoleteClassName() {
|
2012-08-29 04:30:10 +02:00
|
|
|
$className = $this->getField("ClassName");
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!ClassInfo::exists($className)) {
|
|
|
|
return $className;
|
|
|
|
}
|
|
|
|
return null;
|
2012-08-29 04:30:10 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* Gets name of this class
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getClassName() {
|
2012-08-29 04:30:10 +02:00
|
|
|
$className = $this->getField("ClassName");
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!ClassInfo::exists($className)) {
|
2016-10-06 06:31:38 +02:00
|
|
|
return static::class;
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2012-08-29 04:30:10 +02:00
|
|
|
return $className;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-04-27 07:55:25 +02:00
|
|
|
* Set the ClassName attribute. {@link $class} is also updated.
|
|
|
|
* Warning: This will produce an inconsistent record, as the object
|
|
|
|
* instance will not automatically switch to the new subclass.
|
|
|
|
* Please use {@link newClassInstance()} for this purpose,
|
|
|
|
* or destroy and reinstanciate the record.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2009-04-27 07:55:25 +02:00
|
|
|
* @param string $className The new ClassName attribute (a subclass of {@link DataObject})
|
2016-09-05 08:18:52 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setClassName($className) {
|
2009-06-09 05:29:24 +02:00
|
|
|
$className = trim($className);
|
2016-10-06 06:31:38 +02:00
|
|
|
if(!$className || !is_subclass_of($className, self::class)) {
|
2016-09-05 08:18:52 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2009-06-09 05:29:24 +02:00
|
|
|
|
|
|
|
$this->class = $className;
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField("ClassName", $className);
|
2016-09-05 08:18:52 +02:00
|
|
|
$this->setField('RecordClassName', $className);
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-14 08:35:45 +02:00
|
|
|
* Create a new instance of a different class from this object's record.
|
2007-07-19 12:40:28 +02:00
|
|
|
* This is useful when dynamically changing the type of an instance. Specifically,
|
2007-09-15 01:10:25 +02:00
|
|
|
* it ensures that the instance of the class is a match for the className of the
|
2009-04-27 07:55:25 +02:00
|
|
|
* record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName}
|
|
|
|
* property manually before calling this method, as it will confuse change detection.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-10-14 08:35:45 +02:00
|
|
|
* If the new class is different to the original class, defaults are populated again
|
|
|
|
* because this will only occur automatically on instantiation of a DataObject if
|
|
|
|
* there is no record, or the record has no ID. In this case, we do have an ID but
|
|
|
|
* we still need to repopulate the defaults.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $newClassName The name of the new class
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject The new instance of the new class, The exact type will be of the class name provided.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function newClassInstance($newClassName) {
|
2016-10-06 06:31:38 +02:00
|
|
|
if (!is_subclass_of($newClassName, self::class)) {
|
2016-09-05 08:18:52 +02:00
|
|
|
throw new InvalidArgumentException("$newClassName is not a valid subclass of DataObject");
|
|
|
|
}
|
|
|
|
|
2009-04-27 07:55:25 +02:00
|
|
|
$originalClass = $this->ClassName;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-09-05 08:18:52 +02:00
|
|
|
/** @var DataObject $newInstance */
|
|
|
|
$newInstance = Injector::inst()->create($newClassName, $this->record, false, $this->model);
|
|
|
|
|
|
|
|
// Modify ClassName
|
2009-10-14 08:35:45 +02:00
|
|
|
if($newClassName != $originalClass) {
|
2009-04-27 07:55:25 +02:00
|
|
|
$newInstance->setClassName($newClassName);
|
2009-10-14 08:35:45 +02:00
|
|
|
$newInstance->populateDefaults();
|
2009-04-27 07:55:25 +02:00
|
|
|
$newInstance->forceChange();
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $newInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds methods from the extensions.
|
|
|
|
* Called by Object::__construct() once per class.
|
2007-09-15 01:10:25 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function defineMethods() {
|
2007-08-16 08:32:49 +02:00
|
|
|
parent::defineMethods();
|
|
|
|
|
2009-06-17 09:01:28 +02:00
|
|
|
// Define the extra db fields - this is only necessary for extensions added in the
|
|
|
|
// class definition. Object::add_extension() will call this at definition time for
|
|
|
|
// those objects, which is a better mechanism. Perhaps extensions defined inside the
|
|
|
|
// class def can somehow be applied at definiton time also?
|
2007-08-16 08:32:49 +02:00
|
|
|
if($this->extension_instances) foreach($this->extension_instances as $i => $instance) {
|
2009-09-18 05:02:19 +02:00
|
|
|
if(!$instance->class) {
|
|
|
|
$class = get_class($instance);
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("DataObject::defineMethods(): Please ensure {$class}::__construct() calls"
|
|
|
|
. " parent::__construct()", E_USER_ERROR);
|
2009-09-18 05:02:19 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if(static::class === self::class) {
|
|
|
|
return;
|
|
|
|
}
|
2010-10-13 03:35:19 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Set up accessors for joined items
|
2015-02-25 16:21:33 +01:00
|
|
|
if($manyMany = $this->manyMany()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($manyMany as $relationship => $class) {
|
|
|
|
$this->addWrapperMethod($relationship, 'getManyManyComponents');
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 16:21:33 +01:00
|
|
|
if($hasMany = $this->hasMany()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
foreach($hasMany as $relationship => $class) {
|
|
|
|
$this->addWrapperMethod($relationship, 'getComponents');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-02-25 16:21:33 +01:00
|
|
|
if($hasOne = $this->hasOne()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($hasOne as $relationship => $class) {
|
|
|
|
$this->addWrapperMethod($relationship, 'getComponent');
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 16:21:33 +01:00
|
|
|
if($belongsTo = $this->belongsTo()) foreach(array_keys($belongsTo) as $relationship) {
|
2009-10-24 01:27:51 +02:00
|
|
|
$this->addWrapperMethod($relationship, 'getComponent');
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this object "exists", i.e., has a sensible value.
|
2007-10-02 06:57:24 +02:00
|
|
|
* The default behaviour for a DataObject is to return true if
|
|
|
|
* the object exists in the database, you can override this in subclasses.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean true if this object exists
|
|
|
|
*/
|
|
|
|
public function exists() {
|
2013-10-29 22:34:09 +01:00
|
|
|
return (isset($this->record['ID']) && $this->record['ID'] > 0);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2010-12-06 23:12:24 +01:00
|
|
|
/**
|
|
|
|
* Returns TRUE if all values (other than "ID") are
|
|
|
|
* considered empty (by weak boolean comparison).
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-12-06 23:12:24 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-09-10 05:46:23 +02:00
|
|
|
public function isEmpty() {
|
|
|
|
$fixed = $this->config()->fixed_fields;
|
|
|
|
foreach($this->toMap() as $field => $value){
|
|
|
|
// only look at custom fields
|
|
|
|
if(isset($fixed[$field])) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2015-09-10 05:46:23 +02:00
|
|
|
$dbObject = $this->dbObject($field);
|
|
|
|
if(!$dbObject) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($dbObject->exists()) {
|
|
|
|
return false;
|
2007-08-23 07:47:54 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 05:46:23 +02:00
|
|
|
return true;
|
2007-08-23 07:47:54 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2016-04-07 02:48:40 +02:00
|
|
|
/**
|
|
|
|
* Pluralise this item given a specific count.
|
|
|
|
*
|
|
|
|
* E.g. "0 Pages", "1 File", "3 Images"
|
|
|
|
*
|
|
|
|
* @param string $count
|
|
|
|
* @param bool $prependNumber Include number in result. Defaults to true.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function i18n_pluralise($count, $prependNumber = true) {
|
|
|
|
return i18n::pluralise(
|
|
|
|
$this->i18n_singular_name(),
|
|
|
|
$this->i18n_plural_name(),
|
|
|
|
$count,
|
|
|
|
$prependNumber
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the user friendly singular name of this DataObject.
|
|
|
|
* If the name is not defined (by redefining $singular_name in the subclass),
|
|
|
|
* this returns the class name.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string User friendly singular name of this DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function singular_name() {
|
2009-05-22 05:49:15 +02:00
|
|
|
if(!$name = $this->stat('singular_name')) {
|
2016-06-15 06:03:16 +02:00
|
|
|
$reflection = new \ReflectionClass($this);
|
|
|
|
$name = ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $reflection->getShortName()))));
|
2007-09-15 01:10:25 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-09-15 01:10:25 +02:00
|
|
|
return $name;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Get the translated user friendly singular name of this DataObject
|
|
|
|
* same as singular_name() but runs it through the translating function
|
|
|
|
*
|
|
|
|
* Translating string is in the form:
|
|
|
|
* $this->class.SINGULARNAME
|
|
|
|
* Example:
|
|
|
|
* Page.SINGULARNAME
|
|
|
|
*
|
|
|
|
* @return string User friendly translated singular name of this DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function i18n_singular_name() {
|
2016-08-09 04:09:30 +02:00
|
|
|
// @todo Update localisation to FQN for all classes
|
|
|
|
$reflection = new \ReflectionClass($this);
|
|
|
|
return _t($reflection->getShortName().'.SINGULARNAME', $this->singular_name());
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the user friendly plural name of this DataObject
|
|
|
|
* If the name is not defined (by renaming $plural_name in the subclass),
|
|
|
|
* this returns a pluralised version of the class name.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string User friendly plural name of this DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function plural_name() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($name = $this->stat('plural_name')) {
|
2007-09-15 01:10:25 +02:00
|
|
|
return $name;
|
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$name = $this->singular_name();
|
2015-11-06 15:38:59 +01:00
|
|
|
//if the penultimate character is not a vowel, replace "y" with "ies"
|
|
|
|
if (preg_match('/[^aeiou]y$/i', $name)) {
|
|
|
|
$name = substr($name,0,-1) . 'ie';
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
return ucfirst($name . 's');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Get the translated user friendly plural name of this DataObject
|
|
|
|
* Same as plural_name but runs it through the translation function
|
|
|
|
* Translation string is in the form:
|
|
|
|
* $this->class.PLURALNAME
|
|
|
|
* Example:
|
|
|
|
* Page.PLURALNAME
|
|
|
|
*
|
|
|
|
* @return string User friendly translated plural name of this DataObject
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function i18n_plural_name()
|
2008-02-25 03:10:37 +01:00
|
|
|
{
|
2016-08-09 04:09:30 +02:00
|
|
|
// @todo Update localisation to FQN for all classes
|
2008-08-09 07:04:15 +02:00
|
|
|
$name = $this->plural_name();
|
2016-08-09 04:09:30 +02:00
|
|
|
$reflection = new \ReflectionClass($this);
|
|
|
|
return _t($reflection->getShortName().'.PLURALNAME', $name);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-11 04:25:44 +02:00
|
|
|
/**
|
|
|
|
* Standard implementation of a title/label for a specific
|
|
|
|
* record. Tries to find properties 'Title' or 'Name',
|
|
|
|
* and falls back to the 'ID'. Useful to provide
|
|
|
|
* user-friendly identification of a record, e.g. in errormessages
|
|
|
|
* or UI-selections.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-08-11 04:25:44 +02:00
|
|
|
* Overload this method to have a more specialized implementation,
|
|
|
|
* e.g. for an Address record this could be:
|
|
|
|
* <code>
|
2012-09-19 12:07:39 +02:00
|
|
|
* function getTitle() {
|
2008-08-11 04:25:44 +02:00
|
|
|
* return "{$this->StreetNumber} {$this->StreetName} {$this->City}";
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
|
|
|
if($schema->fieldSpec($this, 'Title')) {
|
|
|
|
return $this->getField('Title');
|
|
|
|
}
|
|
|
|
if($schema->fieldSpec($this, 'Name')) {
|
|
|
|
return $this->getField('Name');
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-11 04:25:44 +02:00
|
|
|
return "#{$this->ID}";
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2008-01-10 04:28:13 +01:00
|
|
|
/**
|
2007-07-19 12:40:28 +02:00
|
|
|
* Returns the associated database record - in this case, the object itself.
|
|
|
|
* This is included so that you can call $dataOrController->data() and get a DataObject all the time.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject Associated database record
|
|
|
|
*/
|
|
|
|
public function data() {
|
|
|
|
return $this;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Convert this object to a map.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return array The data as a map.
|
|
|
|
*/
|
|
|
|
public function toMap() {
|
2012-07-18 06:28:24 +02:00
|
|
|
$this->loadLazyFields();
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->record;
|
|
|
|
}
|
|
|
|
|
2013-04-21 03:18:18 +02:00
|
|
|
/**
|
|
|
|
* Return all currently fetched database fields.
|
|
|
|
*
|
|
|
|
* This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields.
|
|
|
|
* Obviously, this makes it a lot faster.
|
|
|
|
*
|
|
|
|
* @return array The data as a map.
|
|
|
|
*/
|
|
|
|
public function getQueriedDatabaseFields() {
|
|
|
|
return $this->record;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-10-02 02:45:13 +02:00
|
|
|
* Update a number of fields on this object, given a map of the desired changes.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-02 15:58:08 +02:00
|
|
|
* The field names can be simple names, or you can use a dot syntax to access $has_one relations.
|
2008-10-02 02:45:13 +02:00
|
|
|
* For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim".
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* update() doesn't write the main object, but if you use the dot syntax, it will write()
|
2008-10-02 02:45:13 +02:00
|
|
|
* the related objects that it alters.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param array $data A map of field name to data values to update.
|
2013-02-12 11:21:15 +01:00
|
|
|
* @return DataObject $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function update($data) {
|
|
|
|
foreach($data as $k => $v) {
|
2008-10-02 02:45:13 +02:00
|
|
|
// Implement dot syntax for updates
|
|
|
|
if(strpos($k,'.') !== false) {
|
|
|
|
$relations = explode('.', $k);
|
|
|
|
$fieldName = array_pop($relations);
|
|
|
|
$relObj = $this;
|
2016-08-19 00:51:35 +02:00
|
|
|
$relation = null;
|
2008-10-02 02:45:13 +02:00
|
|
|
foreach($relations as $i=>$relation) {
|
2008-10-02 15:58:08 +02:00
|
|
|
// no support for has_many or many_many relationships,
|
|
|
|
// as the updater wouldn't know which object to write to (or create)
|
2008-10-03 00:47:01 +02:00
|
|
|
if($relObj->$relation() instanceof DataObject) {
|
2013-05-09 01:45:25 +02:00
|
|
|
$parentObj = $relObj;
|
2008-10-02 15:58:08 +02:00
|
|
|
$relObj = $relObj->$relation();
|
|
|
|
// If the intermediate relationship objects have been created, then write them
|
2013-05-09 01:45:25 +02:00
|
|
|
if($i<sizeof($relation)-1 && !$relObj->ID || (!$relObj->ID && $parentObj != $this)) {
|
|
|
|
$relObj->write();
|
|
|
|
$relatedFieldName = $relation."ID";
|
|
|
|
$parentObj->$relatedFieldName = $relObj->ID;
|
|
|
|
$parentObj->write();
|
|
|
|
}
|
2008-10-02 15:58:08 +02:00
|
|
|
} else {
|
|
|
|
user_error(
|
2013-06-21 00:32:08 +02:00
|
|
|
"DataObject::update(): Can't traverse relationship '$relation'," .
|
|
|
|
"it has to be a has_one relationship or return a single DataObject",
|
2008-10-02 15:58:08 +02:00
|
|
|
E_USER_NOTICE
|
|
|
|
);
|
|
|
|
// unset relation object so we don't write properties to the wrong object
|
2016-08-19 00:51:35 +02:00
|
|
|
$relObj = null;
|
2008-10-02 15:58:08 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-10-02 02:45:13 +02:00
|
|
|
}
|
2008-10-03 00:47:01 +02:00
|
|
|
|
2008-10-02 02:45:13 +02:00
|
|
|
if($relObj) {
|
|
|
|
$relObj->$fieldName = $v;
|
|
|
|
$relObj->write();
|
2013-05-09 01:45:25 +02:00
|
|
|
$relatedFieldName = $relation."ID";
|
|
|
|
$this->$relatedFieldName = $relObj->ID;
|
2008-10-02 02:45:13 +02:00
|
|
|
$relObj->flushCache();
|
|
|
|
} else {
|
|
|
|
user_error("Couldn't follow dot syntax '$k' on '$this->class' object", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->$k = $v;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Pass changes as a map, and try to
|
|
|
|
* get automatic casting for these fields.
|
|
|
|
* Doesn't write to the database. To write the data,
|
|
|
|
* use the write() method.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param array $data A map of field name to data values to update.
|
2013-02-12 11:21:15 +01:00
|
|
|
* @return DataObject $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function castedUpdate($data) {
|
|
|
|
foreach($data as $k => $v) {
|
|
|
|
$this->setCastedField($k,$v);
|
|
|
|
}
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
/**
|
|
|
|
* Merges data and relations from another object of same class,
|
|
|
|
* without conflict resolution. Allows to specify which
|
|
|
|
* dataset takes priority in case its not empty.
|
|
|
|
* has_one-relations are just transferred with priority 'right'.
|
|
|
|
* has_many and many_many-relations are added regardless of priority.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2007-10-18 22:04:02 +02:00
|
|
|
* Caution: has_many/many_many relations are moved rather than duplicated,
|
|
|
|
* meaning they are not connected to the merged object any longer.
|
|
|
|
* Caution: Just saves updated has_many/many_many relations to the database,
|
2008-08-09 07:04:15 +02:00
|
|
|
* doesn't write the updated object itself (just writes the object-properties).
|
2007-10-18 22:04:02 +02:00
|
|
|
* Caution: Does not delete the merged object.
|
|
|
|
* Caution: Does now overwrite Created date on the original object.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param DataObject $rightObj
|
|
|
|
* @param string $priority left|right Determines who wins in case of a conflict (optional)
|
|
|
|
* @param bool $includeRelations Merge any existing relations (optional)
|
|
|
|
* @param bool $overwriteWithEmpty Overwrite existing left values with empty right values.
|
2012-12-08 12:20:20 +01:00
|
|
|
* Only applicable with $priority='right'. (optional)
|
2007-10-18 22:04:02 +02:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) {
|
|
|
|
$leftObj = $this;
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
if($leftObj->ClassName != $rightObj->ClassName) {
|
|
|
|
// we can't merge similiar subclasses because they might have additional relations
|
2008-08-09 07:04:15 +02:00
|
|
|
user_error("DataObject->merge(): Invalid object class '{$rightObj->ClassName}'
|
|
|
|
(expected '{$leftObj->ClassName}').", E_USER_WARNING);
|
2007-10-18 22:04:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$rightObj->ID) {
|
2008-08-09 07:04:15 +02:00
|
|
|
user_error("DataObject->merge(): Please write your merged-in object to the database before merging,
|
2007-10-18 22:04:02 +02:00
|
|
|
to make sure all relations are transferred properly.').", E_USER_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
// makes sure we don't merge data like ID or ClassName
|
2016-10-06 06:31:38 +02:00
|
|
|
$rightData = DataObject::getSchema()->fieldSpecs(get_class($rightObj));
|
2015-09-10 05:46:23 +02:00
|
|
|
foreach($rightData as $key=>$rightSpec) {
|
|
|
|
// Don't merge ID
|
|
|
|
if($key === 'ID') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only merge relations if allowed
|
|
|
|
if($rightSpec === 'ForeignKey' && !$includeRelations) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
// don't merge conflicting values if priority is 'left'
|
2015-09-10 05:46:23 +02:00
|
|
|
if($priority == 'left' && $leftObj->{$key} !== $rightObj->{$key}) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
// don't overwrite existing left values with empty right values (if $overwriteWithEmpty is set)
|
2015-09-10 05:46:23 +02:00
|
|
|
if($priority == 'right' && !$overwriteWithEmpty && empty($rightObj->{$key})) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-18 22:04:02 +02:00
|
|
|
|
|
|
|
// TODO remove redundant merge of has_one fields
|
|
|
|
$leftObj->{$key} = $rightObj->{$key};
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
// merge relations
|
|
|
|
if($includeRelations) {
|
2015-02-25 16:21:33 +01:00
|
|
|
if($manyMany = $this->manyMany()) {
|
2007-10-18 22:04:02 +02:00
|
|
|
foreach($manyMany as $relationship => $class) {
|
|
|
|
$leftComponents = $leftObj->getManyManyComponents($relationship);
|
|
|
|
$rightComponents = $rightObj->getManyManyComponents($relationship);
|
2012-09-26 23:34:00 +02:00
|
|
|
if($rightComponents && $rightComponents->exists()) {
|
|
|
|
$leftComponents->addMany($rightComponents->column('ID'));
|
|
|
|
}
|
2007-10-18 22:04:02 +02:00
|
|
|
$leftComponents->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
if($hasMany = $this->hasMany()) {
|
2007-10-18 22:04:02 +02:00
|
|
|
foreach($hasMany as $relationship => $class) {
|
|
|
|
$leftComponents = $leftObj->getComponents($relationship);
|
|
|
|
$rightComponents = $rightObj->getComponents($relationship);
|
2012-09-26 23:34:00 +02:00
|
|
|
if($rightComponents && $rightComponents->exists()) {
|
|
|
|
$leftComponents->addMany($rightComponents->column('ID'));
|
|
|
|
}
|
2007-10-18 22:04:02 +02:00
|
|
|
$leftComponents->write();
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-10-18 22:04:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Forces the record to think that all its data has changed.
|
2009-04-27 07:55:25 +02:00
|
|
|
* Doesn't write to the database. Only sets fields as changed
|
|
|
|
* if they are not already marked as changed.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2016-05-18 01:00:04 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function forceChange() {
|
2012-07-18 06:28:24 +02:00
|
|
|
// Ensure lazy fields loaded
|
|
|
|
$this->loadLazyFields();
|
2016-10-06 06:31:38 +02:00
|
|
|
$fields = static::getSchema()->fieldSpecs(static::class);
|
2012-07-18 06:28:24 +02:00
|
|
|
|
2009-07-03 03:21:48 +02:00
|
|
|
// $this->record might not contain the blank values so we loop on $this->inheritedDatabaseFields() as well
|
2012-09-26 23:34:00 +02:00
|
|
|
$fieldNames = array_unique(array_merge(
|
2013-06-21 00:32:08 +02:00
|
|
|
array_keys($this->record),
|
2016-10-06 06:31:38 +02:00
|
|
|
array_keys($fields)
|
2015-09-10 05:46:23 +02:00
|
|
|
));
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-07-03 03:21:48 +02:00
|
|
|
foreach($fieldNames as $fieldName) {
|
2013-10-17 01:55:58 +02:00
|
|
|
if(!isset($this->changed[$fieldName])) $this->changed[$fieldName] = self::CHANGE_STRICT;
|
2009-07-03 03:21:48 +02:00
|
|
|
// Populate the null values in record so that they actually get written
|
2012-07-18 06:28:24 +02:00
|
|
|
if(!isset($this->record[$fieldName])) $this->record[$fieldName] = null;
|
2009-04-27 07:55:25 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
// @todo Find better way to allow versioned to write a new version after forceChange
|
2016-10-06 06:31:38 +02:00
|
|
|
if($this->isChanged('Version')) {
|
|
|
|
unset($this->changed['Version']);
|
|
|
|
}
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Validate the current object.
|
|
|
|
*
|
|
|
|
* By default, there is no validation - objects are always valid! However, you can overload this method in your
|
2012-04-13 06:18:06 +02:00
|
|
|
* DataObject sub-classes to specify custom validation, or use the hook through DataExtension.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-04-26 08:31:52 +02:00
|
|
|
* Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite()
|
|
|
|
* and onAfterWrite() won't get called either.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* It is expected that you call validate() in your own application to test that an object is valid before
|
|
|
|
* attempting a write, and respond appropriately if it isn't.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2014-11-09 09:19:39 +01:00
|
|
|
* @see {@link ValidationResult}
|
|
|
|
* @return ValidationResult
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2015-06-17 05:51:30 +02:00
|
|
|
public function validate() {
|
2013-06-03 17:12:58 +02:00
|
|
|
$result = ValidationResult::create();
|
2012-04-13 06:18:06 +02:00
|
|
|
$this->extend('validate', $result);
|
|
|
|
return $result;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2015-06-16 01:39:32 +02:00
|
|
|
/**
|
|
|
|
* Public accessor for {@see DataObject::validate()}
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-06-16 01:39:32 +02:00
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
|
|
|
public function doValidate() {
|
2015-06-17 05:51:30 +02:00
|
|
|
Deprecation::notice('5.0', 'Use validate');
|
2015-06-16 01:39:32 +02:00
|
|
|
return $this->validate();
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Event handler called before writing to the database.
|
|
|
|
* You can overload this to clean up or otherwise process data before writing it to the
|
|
|
|
* database. Don't forget to call parent::onBeforeWrite(), though!
|
2008-04-26 08:31:52 +02:00
|
|
|
*
|
|
|
|
* This called after {@link $this->validate()}, so you can be sure that your data is valid.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* @uses DataExtension->onBeforeWrite()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeWrite() {
|
|
|
|
$this->brokenOnWrite = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
$dummy = null;
|
|
|
|
$this->extend('onBeforeWrite', $dummy);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Event handler called after writing to the database.
|
|
|
|
* You can overload this to act upon changes made to the data after it is written.
|
|
|
|
* $this->changed will have a record
|
|
|
|
* database. Don't forget to call parent::onAfterWrite(), though!
|
2008-11-07 13:18:35 +01:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* @uses DataExtension->onAfterWrite()
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
|
|
|
protected function onAfterWrite() {
|
2008-11-07 13:18:35 +01:00
|
|
|
$dummy = null;
|
|
|
|
$this->extend('onAfterWrite', $dummy);
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Event handler called before deleting from the database.
|
2007-09-15 01:10:25 +02:00
|
|
|
* You can overload this to clean up or otherwise process data before delete this
|
2007-07-19 12:40:28 +02:00
|
|
|
* record. Don't forget to call parent::onBeforeDelete(), though!
|
2008-11-07 13:18:35 +01:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* @uses DataExtension->onBeforeDelete()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeDelete() {
|
|
|
|
$this->brokenOnDelete = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
$dummy = null;
|
|
|
|
$this->extend('onBeforeDelete', $dummy);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
protected function onAfterDelete() {
|
|
|
|
$this->extend('onAfterDelete');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Load the default values in from the self::$defaults array.
|
|
|
|
* Will traverse the defaults of the current class and all its parent classes.
|
|
|
|
* Called by the constructor when creating new records.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2013-02-12 11:21:15 +01:00
|
|
|
* @uses DataExtension->populateDefaults()
|
|
|
|
* @return DataObject $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function populateDefaults() {
|
|
|
|
$classes = array_reverse(ClassInfo::ancestry($this));
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($classes as $class) {
|
2012-04-12 18:26:56 +02:00
|
|
|
$defaults = Config::inst()->get($class, 'defaults', Config::UNINHERITED);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-06-17 04:13:02 +02:00
|
|
|
if($defaults && !is_array($defaults)) {
|
|
|
|
user_error("Bad '$this->class' defaults given: " . var_export($defaults, true),
|
2012-12-08 12:20:20 +01:00
|
|
|
E_USER_WARNING);
|
2009-06-17 04:13:02 +02:00
|
|
|
$defaults = null;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($defaults) foreach($defaults as $fieldName => $fieldValue) {
|
|
|
|
// SRM 2007-03-06: Stricter check
|
2008-09-23 02:10:41 +02:00
|
|
|
if(!isset($this->$fieldName) || $this->$fieldName === null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->$fieldName = $fieldValue;
|
|
|
|
}
|
|
|
|
// Set many-many defaults with an array of ids
|
2016-10-06 06:31:38 +02:00
|
|
|
if(is_array($fieldValue) && $this->getSchema()->manyManyComponent(static::class, $fieldName)) {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var ManyManyList $manyManyJoin */
|
2007-07-19 12:40:28 +02:00
|
|
|
$manyManyJoin = $this->$fieldName();
|
2016-08-19 00:51:35 +02:00
|
|
|
$manyManyJoin->setByIDList($fieldValue);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 06:31:38 +02:00
|
|
|
if($class == self::class) {
|
2007-07-19 12:40:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
$this->extend('populateDefaults');
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Determine validation of this object prior to write
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return ValidationException Exception generated by this write, or null if valid
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected function validateWrite() {
|
2012-08-29 04:30:10 +02:00
|
|
|
if ($this->ObsoleteClassName) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return new ValidationException(
|
2012-08-29 04:30:10 +02:00
|
|
|
"Object is of class '{$this->ObsoleteClassName}' which doesn't exist - ".
|
|
|
|
"you need to change the ClassName before you can write it",
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if($this->config()->get('validation_enabled')) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$result = $this->validate();
|
|
|
|
if (!$result->valid()) {
|
|
|
|
return new ValidationException(
|
|
|
|
$result,
|
|
|
|
$result->message(),
|
2012-08-29 04:30:10 +02:00
|
|
|
E_USER_WARNING
|
|
|
|
);
|
2009-03-04 04:44:11 +01:00
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2009-03-04 04:44:11 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Prepare an object prior to write
|
|
|
|
*
|
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
|
|
|
protected function preWrite() {
|
|
|
|
// Validate this object
|
|
|
|
if($writeException = $this->validateWrite()) {
|
2012-08-29 04:30:10 +02:00
|
|
|
// Used by DODs to clean up after themselves, eg, Versioned
|
2013-02-05 23:16:18 +01:00
|
|
|
$this->invokeWithExtensions('onAfterSkippedWrite');
|
2012-08-29 04:30:10 +02:00
|
|
|
throw $writeException;
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check onBeforeWrite
|
|
|
|
$this->brokenOnWrite = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->onBeforeWrite();
|
|
|
|
if($this->brokenOnWrite) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("$this->class has a broken onBeforeWrite() function."
|
|
|
|
. " Make sure that you call parent::onBeforeWrite().", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Detects and updates all changes made to this object
|
|
|
|
*
|
|
|
|
* @param bool $forceChanges If set to true, force all fields to be treated as changed
|
|
|
|
* @return bool True if any changes are detected
|
|
|
|
*/
|
2016-05-18 01:00:04 +02:00
|
|
|
protected function updateChanges($forceChanges = false)
|
|
|
|
{
|
|
|
|
if($forceChanges) {
|
|
|
|
// Force changes, but only for loaded fields
|
|
|
|
foreach($this->record as $field => $value) {
|
|
|
|
$this->changed[$field] = static::CHANGE_VALUE;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2016-05-18 01:00:04 +02:00
|
|
|
return true;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2016-05-18 01:00:04 +02:00
|
|
|
return $this->isChanged();
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Writes a subset of changes for a specific table to the given manipulation
|
|
|
|
*
|
|
|
|
* @param string $baseTable Base table
|
|
|
|
* @param string $now Timestamp to use for the current time
|
|
|
|
* @param bool $isNewRecord Whether this should be treated as a new record write
|
|
|
|
* @param array $manipulation Manipulation to write to
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $class Class of table to manipulate
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
|
|
|
protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = $this->getSchema();
|
|
|
|
$table = $schema->tableName($class);
|
2016-05-25 07:09:29 +02:00
|
|
|
$manipulation[$table] = array();
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Extract records for this table
|
|
|
|
foreach($this->record as $fieldName => $fieldValue) {
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// we're not attempting to reset the BaseTable->ID
|
2016-10-06 06:31:38 +02:00
|
|
|
// Ignore unchanged fields or attempts to reset the BaseTable->ID
|
|
|
|
if (empty($this->changed[$fieldName]) || ($table === $baseTable && $fieldName === 'ID')) {
|
2013-06-21 00:32:08 +02:00
|
|
|
continue;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
// Ensure this field pertains to this table
|
|
|
|
$specification = $schema->fieldSpec($class, $fieldName, ['dbOnly', 'uninherited']);
|
|
|
|
if (!$specification) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// if database column doesn't correlate to a DBField instance...
|
|
|
|
$fieldObj = $this->dbObject($fieldName);
|
|
|
|
if(!$fieldObj) {
|
|
|
|
$fieldObj = DBField::create_field('Varchar', $fieldValue, $fieldName);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
// Write to manipulation
|
2016-05-25 07:09:29 +02:00
|
|
|
$fieldObj->writeToManipulation($manipulation[$table]);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2008-02-25 02:06:39 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Ensure update of Created and LastEdited columns
|
2016-05-25 07:09:29 +02:00
|
|
|
if($baseTable === $table) {
|
|
|
|
$manipulation[$table]['fields']['LastEdited'] = $now;
|
2013-06-21 00:32:08 +02:00
|
|
|
if($isNewRecord) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$manipulation[$table]['fields']['Created']
|
2013-06-21 00:32:08 +02:00
|
|
|
= empty($this->record['Created'])
|
|
|
|
? $now
|
|
|
|
: $this->record['Created'];
|
2016-05-25 07:09:29 +02:00
|
|
|
$manipulation[$table]['fields']['ClassName'] = $this->class;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2007-09-16 18:40:57 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Inserts done one the base table are performed in another step, so the manipulation should instead
|
|
|
|
// attempt an update, as though it were a normal update.
|
2016-05-25 07:09:29 +02:00
|
|
|
$manipulation[$table]['command'] = $isNewRecord ? 'insert' : 'update';
|
|
|
|
$manipulation[$table]['id'] = $this->record['ID'];
|
|
|
|
$manipulation[$table]['class'] = $class;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2007-09-16 18:40:57 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Ensures that a blank base record exists with the basic fixed fields for this dataobject
|
|
|
|
*
|
|
|
|
* Does nothing if an ID is already assigned for this record
|
|
|
|
*
|
|
|
|
* @param string $baseTable Base table
|
|
|
|
* @param string $now Timestamp to use for the current time
|
|
|
|
*/
|
|
|
|
protected function writeBaseRecord($baseTable, $now) {
|
|
|
|
// Generate new ID if not specified
|
|
|
|
if($this->isInDB()) return;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Perform an insert on the base table
|
|
|
|
$insert = new SQLInsert('"'.$baseTable.'"');
|
|
|
|
$insert
|
|
|
|
->assign('"Created"', $now)
|
|
|
|
->execute();
|
|
|
|
$this->changed['ID'] = self::CHANGE_VALUE;
|
|
|
|
$this->record['ID'] = DB::get_generated_id($baseTable);
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Generate and write the database manipulation for all changed fields
|
|
|
|
*
|
|
|
|
* @param string $baseTable Base table
|
|
|
|
* @param string $now Timestamp to use for the current time
|
|
|
|
* @param bool $isNewRecord If this is a new record
|
|
|
|
*/
|
|
|
|
protected function writeManipulation($baseTable, $now, $isNewRecord) {
|
|
|
|
// Generate database manipulations for each class
|
|
|
|
$manipulation = array();
|
2016-10-06 06:31:38 +02:00
|
|
|
foreach(ClassInfo::ancestry(static::class, true) as $class) {
|
|
|
|
$this->prepareManipulationTable($baseTable, $now, $isNewRecord, $manipulation, $class);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Allow extensions to extend this manipulation
|
|
|
|
$this->extend('augmentWrite', $manipulation);
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// New records have their insert into the base data table done first, so that they can pass the
|
|
|
|
// generated ID on to the rest of the manipulation
|
|
|
|
if($isNewRecord) {
|
|
|
|
$manipulation[$baseTable]['command'] = 'update';
|
|
|
|
}
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Perform the manipulation
|
|
|
|
DB::manipulate($manipulation);
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Writes all changes to this object to the database.
|
|
|
|
* - It will insert a record whenever ID isn't set, otherwise update.
|
|
|
|
* - All relevant tables will be updated.
|
|
|
|
* - $this->onBeforeWrite() gets called beforehand.
|
|
|
|
* - Extensions such as Versioned will ammend the database-write to ensure that a version is saved.
|
|
|
|
*
|
|
|
|
* @uses DataExtension->augmentWrite()
|
|
|
|
*
|
|
|
|
* @param boolean $showDebug Show debugging information
|
|
|
|
* @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists
|
|
|
|
* @param boolean $forceWrite Write to database even if there are no changes
|
|
|
|
* @param boolean $writeComponents Call write() on all associated component instances which were previously
|
|
|
|
* retrieved through {@link getComponent()}, {@link getComponents()} or
|
|
|
|
* {@link getManyManyComponents()} (Default: false)
|
|
|
|
* @return int The ID of the record
|
|
|
|
* @throws ValidationException Exception that can be caught and handled by the calling function
|
|
|
|
*/
|
|
|
|
public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) {
|
2015-08-30 07:02:55 +02:00
|
|
|
$now = DBDatetime::now()->Rfc2822();
|
2008-02-25 02:06:39 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Execute pre-write tasks
|
|
|
|
$this->preWrite();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check if we are doing an update or an insert
|
|
|
|
$isNewRecord = !$this->isInDB() || $forceInsert;
|
|
|
|
|
|
|
|
// Check changes exist, abort if there are none
|
2016-05-18 01:00:04 +02:00
|
|
|
$hasChanges = $this->updateChanges($isNewRecord);
|
2013-06-21 00:32:08 +02:00
|
|
|
if($hasChanges || $forceWrite || $isNewRecord) {
|
|
|
|
// New records have their insert into the base data table done first, so that they can pass the
|
|
|
|
// generated primary key on to the rest of the manipulation
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseTable = $this->baseTable();
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->writeBaseRecord($baseTable, $now);
|
|
|
|
|
|
|
|
// Write the DB manipulation for all changed fields
|
|
|
|
$this->writeManipulation($baseTable, $now, $isNewRecord);
|
|
|
|
|
|
|
|
// If there's any relations that couldn't be saved before, save them now (we have an ID here)
|
|
|
|
$this->writeRelations();
|
|
|
|
$this->onAfterWrite();
|
|
|
|
$this->changed = array();
|
2010-10-12 23:48:07 +02:00
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
if($showDebug) Debug::message("no changes for DataObject");
|
|
|
|
|
2010-10-12 23:48:07 +02:00
|
|
|
// Used by DODs to clean up after themselves, eg, Versioned
|
2013-02-05 23:16:18 +01:00
|
|
|
$this->invokeWithExtensions('onAfterSkippedWrite');
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2008-02-25 02:06:39 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Ensure Created and LastEdited are populated
|
|
|
|
if(!isset($this->record['Created'])) {
|
|
|
|
$this->record['Created'] = $now;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->record['LastEdited'] = $now;
|
|
|
|
|
|
|
|
// Write relations as necessary
|
|
|
|
if($writeComponents) $this->writeComponents(true);
|
|
|
|
|
|
|
|
// Clears the cache for this object so get_one returns the correct object.
|
|
|
|
$this->flushCache();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->record['ID'];
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Writes cached relation lists to the database, if possible
|
|
|
|
*/
|
|
|
|
public function writeRelations() {
|
|
|
|
if(!$this->isInDB()) return;
|
|
|
|
|
|
|
|
// If there's any relations that couldn't be saved before, save them now (we have an ID here)
|
|
|
|
if($this->unsavedRelations) {
|
|
|
|
foreach($this->unsavedRelations as $name => $list) {
|
|
|
|
$list->changeToList($this->$name());
|
|
|
|
}
|
|
|
|
$this->unsavedRelations = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-10 03:43:31 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Write the cached components to the database. Cached components could refer to two different instances of the
|
|
|
|
* same record.
|
2012-04-12 07:03:18 +02:00
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param bool $recursive Recursively write components
|
2013-02-12 11:21:15 +01:00
|
|
|
* @return DataObject $this
|
2008-04-10 03:43:31 +02:00
|
|
|
*/
|
|
|
|
public function writeComponents($recursive = false) {
|
2016-09-26 07:22:19 +02:00
|
|
|
if($this->components) {
|
|
|
|
foreach ($this->components as $component) {
|
|
|
|
$component->write(false, false, false, $recursive);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
if ($join = $this->getJoin()) {
|
|
|
|
$join->write(false, false, false, $recursive);
|
2008-04-10 03:43:31 +02:00
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2008-04-10 03:43:31 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Delete this data object.
|
|
|
|
* $this->onBeforeDelete() gets called.
|
|
|
|
* Note that in Versioned objects, both Stage and Live will be deleted.
|
2011-04-15 11:35:30 +02:00
|
|
|
* @uses DataExtension->augmentSQL()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-15 01:10:25 +02:00
|
|
|
public function delete() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->brokenOnDelete = true;
|
|
|
|
$this->onBeforeDelete();
|
|
|
|
if($this->brokenOnDelete) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("$this->class has a broken onBeforeDelete() function."
|
|
|
|
. " Make sure that you call parent::onBeforeDelete().", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
// Deleting a record without an ID shouldn't do anything
|
|
|
|
if(!$this->ID) throw new LogicException("DataObject::delete() called on a DataObject without an ID");
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
// TODO: This is quite ugly. To improve:
|
|
|
|
// - move the details of the delete code in the DataQuery system
|
|
|
|
// - update the code to just delete the base table, and rely on cascading deletes in the DB to do the rest
|
|
|
|
// obviously, that means getting requireTable() to configure cascading deletes ;-)
|
2011-05-01 07:33:02 +02:00
|
|
|
$srcQuery = DataList::create($this->class, $this->model)->where("ID = $this->ID")->dataQuery()->query();
|
2009-11-22 06:16:38 +01:00
|
|
|
foreach($srcQuery->queriedTables() as $table) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$delete = new SQLDelete("\"$table\"", array('"ID"' => $this->ID));
|
|
|
|
$delete->execute();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-10-04 06:50:43 +02:00
|
|
|
// Remove this item out of any caches
|
|
|
|
$this->flushCache();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
$this->onAfterDelete();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$this->OldID = $this->ID;
|
|
|
|
$this->ID = 0;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Delete the record with the given ID.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $className The class name of the record to be deleted
|
|
|
|
* @param int $id ID of record to be deleted
|
|
|
|
*/
|
|
|
|
public static function delete_by_id($className, $id) {
|
|
|
|
$obj = DataObject::get_by_id($className, $id);
|
|
|
|
if($obj) {
|
|
|
|
$obj->delete();
|
|
|
|
} else {
|
|
|
|
user_error("$className object #$id wasn't found when calling DataObject::delete_by_id", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the class ancestry, including the current class name.
|
|
|
|
* The ancestry will be returned as an array of class names, where the 0th element
|
|
|
|
* will be the class that inherits directly from DataObject, and the last element
|
|
|
|
* will be the current class.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return array Class ancestry
|
|
|
|
*/
|
|
|
|
public function getClassAncestry() {
|
2016-10-06 06:31:38 +02:00
|
|
|
return ClassInfo::ancestry(static::class);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a component object from a one to one relationship, as a DataObject.
|
2013-07-10 02:44:24 +02:00
|
|
|
* If no component is available, an 'empty component' will be returned for
|
|
|
|
* non-polymorphic relations, or for polymorphic relations with a class set.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $componentName Name of the component
|
|
|
|
* @return DataObject The component object. It's exact type will be that of the component.
|
2016-02-25 05:32:41 +01:00
|
|
|
* @throws Exception
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function getComponent($componentName) {
|
|
|
|
if(isset($this->components[$componentName])) {
|
|
|
|
return $this->components[$componentName];
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
|
|
|
if($class = $schema->hasOneComponent(static::class, $componentName)) {
|
2009-10-24 01:27:51 +02:00
|
|
|
$joinField = $componentName . 'ID';
|
|
|
|
$joinID = $this->getField($joinField);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Extract class name for polymorphic relations
|
2016-10-06 06:31:38 +02:00
|
|
|
if($class === self::class) {
|
2013-07-10 02:44:24 +02:00
|
|
|
$class = $this->getField($componentName . 'Class');
|
|
|
|
if(empty($class)) return null;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
if($joinID) {
|
2016-02-25 05:32:41 +01:00
|
|
|
// Ensure that the selected object originates from the same stage, subsite, etc
|
|
|
|
$component = DataObject::get($class)
|
|
|
|
->filter('ID', $joinID)
|
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->first();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-10-11 22:57:11 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
if(empty($component)) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$component = $this->model->$class->newObject();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-10-06 06:31:38 +02:00
|
|
|
} elseif($class = $schema->belongsToComponent(static::class, $componentName)) {
|
|
|
|
$joinField = $schema->getRemoteJoinField(static::class, $componentName, 'belongs_to', $polymorphic);
|
2016-02-25 05:32:41 +01:00
|
|
|
$joinID = $this->ID;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
if($joinID) {
|
2016-02-25 05:32:41 +01:00
|
|
|
// Prepare filter for appropriate join type
|
|
|
|
if($polymorphic) {
|
|
|
|
$filter = array(
|
2013-06-21 00:32:08 +02:00
|
|
|
"{$joinField}ID" => $joinID,
|
|
|
|
"{$joinField}Class" => $this->class
|
2016-02-25 05:32:41 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$filter = array(
|
2013-06-21 00:32:08 +02:00
|
|
|
$joinField => $joinID
|
|
|
|
);
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the selected object originates from the same stage, subsite, etc
|
|
|
|
$component = DataObject::get($class)
|
|
|
|
->filter($filter)
|
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->first();
|
2009-10-24 01:27:51 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
if(empty($component)) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$component = $this->model->$class->newObject();
|
2013-07-10 02:44:24 +02:00
|
|
|
if($polymorphic) {
|
|
|
|
$component->{$joinField.'ID'} = $this->ID;
|
|
|
|
$component->{$joinField.'Class'} = $this->class;
|
|
|
|
} else {
|
|
|
|
$component->$joinField = $this->ID;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-25 05:32:41 +01:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"DataObject->getComponent(): Could not find component '$componentName'."
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
$this->components[$componentName] = $component;
|
|
|
|
return $component;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-11-22 06:29:24 +01:00
|
|
|
* Returns a one-to-many relation as a HasManyList
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $componentName Name of the component
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return HasManyList|UnsavedRelationList The components of the one-to-many relationship.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public function getComponents($componentName) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$result = null;
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = $this->getSchema();
|
|
|
|
$componentClass = $schema->hasManyComponent(static::class, $componentName);
|
2016-02-25 05:32:41 +01:00
|
|
|
if(!$componentClass) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"DataObject::getComponents(): Unknown 1-to-many component '%s' on class '%s'",
|
|
|
|
$componentName,
|
|
|
|
$this->class
|
|
|
|
));
|
2015-02-25 12:13:26 +01:00
|
|
|
}
|
|
|
|
|
2012-11-03 04:16:51 +01:00
|
|
|
// If we haven't been written yet, we can't save these relations, so use a list that handles this case
|
|
|
|
if(!$this->ID) {
|
|
|
|
if(!isset($this->unsavedRelations[$componentName])) {
|
|
|
|
$this->unsavedRelations[$componentName] =
|
2013-06-21 00:32:08 +02:00
|
|
|
new UnsavedRelationList($this->class, $componentName, $componentClass);
|
2012-11-03 04:16:51 +01:00
|
|
|
}
|
|
|
|
return $this->unsavedRelations[$componentName];
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
// Determine type and nature of foreign relation
|
2016-10-06 06:31:38 +02:00
|
|
|
$joinField = $schema->getRemoteJoinField(static::class, $componentName, 'has_many', $polymorphic);
|
2016-02-25 05:32:41 +01:00
|
|
|
/** @var HasManyList $result */
|
2013-07-10 02:44:24 +02:00
|
|
|
if($polymorphic) {
|
|
|
|
$result = PolymorphicHasManyList::create($componentClass, $joinField, $this->class);
|
|
|
|
} else {
|
|
|
|
$result = HasManyList::create($componentClass, $joinField);
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
if($this->model) {
|
|
|
|
$result->setDataModel($this->model);
|
|
|
|
}
|
2008-02-25 02:06:39 +01:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
return $result
|
2016-02-25 05:32:41 +01:00
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->forForeignID($this->ID);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2012-04-20 06:03:38 +02:00
|
|
|
/**
|
|
|
|
* Find the foreign class of a relation on this DataObject, regardless of the relation type.
|
|
|
|
*
|
2016-03-22 02:39:25 +01:00
|
|
|
* @param string $relationName Relation name.
|
2012-04-20 06:03:38 +02:00
|
|
|
* @return string Class name, or null if not found.
|
|
|
|
*/
|
|
|
|
public function getRelationClass($relationName) {
|
2016-09-26 07:22:19 +02:00
|
|
|
// Parse many_many
|
2016-10-06 06:31:38 +02:00
|
|
|
$manyManyComponent = $this->getSchema()->manyManyComponent(static::class, $relationName);
|
2016-09-26 07:22:19 +02:00
|
|
|
if ($manyManyComponent) {
|
|
|
|
list(
|
|
|
|
$relationClass, $parentClass, $componentClass,
|
|
|
|
$parentField, $childField, $tableOrClass
|
|
|
|
) = $manyManyComponent;
|
|
|
|
return $componentClass;
|
|
|
|
}
|
|
|
|
|
2012-04-20 06:03:38 +02:00
|
|
|
// Go through all relationship configuration fields.
|
|
|
|
$candidates = array_merge(
|
|
|
|
($relations = Config::inst()->get($this->class, 'has_one')) ? $relations : array(),
|
|
|
|
($relations = Config::inst()->get($this->class, 'has_many')) ? $relations : array(),
|
|
|
|
($relations = Config::inst()->get($this->class, 'belongs_to')) ? $relations : array()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($candidates[$relationName])) {
|
|
|
|
$remoteClass = $candidates[$relationName];
|
|
|
|
|
|
|
|
// If dot notation is present, extract just the first part that contains the class.
|
|
|
|
if(($fieldPos = strpos($remoteClass, '.'))!==false) {
|
|
|
|
return substr($remoteClass, 0, $fieldPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise just return the class
|
|
|
|
return $remoteClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
/**
|
|
|
|
* Given a relation name, determine the relation type
|
|
|
|
*
|
|
|
|
* @param string $component Name of component
|
|
|
|
* @return string has_one, has_many, many_many, belongs_many_many or belongs_to
|
|
|
|
*/
|
|
|
|
public function getRelationType($component) {
|
|
|
|
$types = array('has_one', 'has_many', 'many_many', 'belongs_many_many', 'belongs_to');
|
|
|
|
foreach($types as $type) {
|
|
|
|
$relations = Config::inst()->get($this->class, $type);
|
|
|
|
if($relations && isset($relations[$component])) {
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a relation declared on a remote class, generate a substitute component for the opposite
|
|
|
|
* side of the relation.
|
|
|
|
*
|
|
|
|
* Notes on behaviour:
|
|
|
|
* - This can still be used on components that are defined on both sides, but do not need to be.
|
|
|
|
* - All has_ones on remote class will be treated as local has_many, even if they are belongs_to
|
|
|
|
* - Cannot be used on polymorphic relationships
|
|
|
|
* - Cannot be used on unsaved objects.
|
|
|
|
*
|
|
|
|
* @param string $remoteClass
|
|
|
|
* @param string $remoteRelation
|
|
|
|
* @return DataList|DataObject The component, either as a list or single object
|
|
|
|
* @throws BadMethodCallException
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function inferReciprocalComponent($remoteClass, $remoteRelation) {
|
2016-09-26 07:22:19 +02:00
|
|
|
$remote = DataObject::singleton($remoteClass);
|
2016-03-22 02:39:25 +01:00
|
|
|
$class = $remote->getRelationClass($remoteRelation);
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
2016-03-22 02:39:25 +01:00
|
|
|
|
|
|
|
// Validate arguments
|
|
|
|
if(!$this->isInDB()) {
|
|
|
|
throw new BadMethodCallException(__METHOD__ . " cannot be called on unsaved objects");
|
|
|
|
}
|
|
|
|
if(empty($class)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"%s invoked with invalid relation %s.%s",
|
|
|
|
__METHOD__,
|
|
|
|
$remoteClass,
|
|
|
|
$remoteRelation
|
|
|
|
));
|
|
|
|
}
|
2016-10-06 06:31:38 +02:00
|
|
|
if($class === self::class) {
|
2016-03-22 02:39:25 +01:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"%s cannot generate opposite component of relation %s.%s as it is polymorphic. " .
|
|
|
|
"This method does not support polymorphic relationships",
|
|
|
|
__METHOD__,
|
|
|
|
$remoteClass,
|
|
|
|
$remoteRelation
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if(!is_a($this, $class, true)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"Relation %s on %s does not refer to objects of type %s",
|
2016-10-06 06:31:38 +02:00
|
|
|
$remoteRelation, $remoteClass, static::class
|
2016-03-22 02:39:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the relation type to mock
|
|
|
|
$relationType = $remote->getRelationType($remoteRelation);
|
|
|
|
switch($relationType) {
|
|
|
|
case 'has_one': {
|
|
|
|
// Mock has_many
|
|
|
|
$joinField = "{$remoteRelation}ID";
|
2016-10-06 06:31:38 +02:00
|
|
|
$componentClass = $schema->classForField($remoteClass, $joinField);
|
2016-03-22 02:39:25 +01:00
|
|
|
$result = HasManyList::create($componentClass, $joinField);
|
|
|
|
if ($this->model) {
|
|
|
|
$result->setDataModel($this->model);
|
|
|
|
}
|
|
|
|
return $result
|
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->forForeignID($this->ID);
|
|
|
|
}
|
|
|
|
case 'belongs_to':
|
|
|
|
case 'has_many': {
|
|
|
|
// These relations must have a has_one on the other end, so find it
|
2016-10-06 06:31:38 +02:00
|
|
|
$joinField = $schema->getRemoteJoinField($remoteClass, $remoteRelation, $relationType, $polymorphic);
|
2016-03-22 02:39:25 +01:00
|
|
|
if ($polymorphic) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"%s cannot generate opposite component of relation %s.%s, as the other end appears" .
|
|
|
|
"to be a has_one polymorphic. This method does not support polymorphic relationships",
|
|
|
|
__METHOD__,
|
|
|
|
$remoteClass,
|
|
|
|
$remoteRelation
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$joinID = $this->getField($joinField);
|
|
|
|
if (empty($joinID)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Get object by joined ID
|
|
|
|
return DataObject::get($remoteClass)
|
|
|
|
->filter('ID', $joinID)
|
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
case 'many_many':
|
|
|
|
case 'belongs_many_many': {
|
|
|
|
// Get components and extra fields from parent
|
2016-09-26 07:22:19 +02:00
|
|
|
list($relationClass, $componentClass, $parentClass, $componentField, $parentField, $table)
|
2016-10-06 06:31:38 +02:00
|
|
|
= $remote->getSchema()->manyManyComponent($remoteClass, $remoteRelation);
|
|
|
|
$extraFields = $schema->manyManyExtraFieldsForComponent($remoteClass, $remoteRelation) ?: array();
|
2016-03-22 02:39:25 +01:00
|
|
|
|
|
|
|
// Reverse parent and component fields and create an inverse ManyManyList
|
2016-09-26 07:22:19 +02:00
|
|
|
/** @var RelationList $result */
|
|
|
|
$result = Injector::inst()->create(
|
|
|
|
$relationClass, $componentClass, $table, $componentField, $parentField, $extraFields
|
|
|
|
);
|
2016-03-22 02:39:25 +01:00
|
|
|
if($this->model) {
|
|
|
|
$result->setDataModel($this->model);
|
|
|
|
}
|
|
|
|
$this->extend('updateManyManyComponents', $result);
|
|
|
|
|
|
|
|
// If this is called on a singleton, then we return an 'orphaned relation' that can have the
|
|
|
|
// foreignID set elsewhere.
|
|
|
|
return $result
|
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->forForeignID($this->ID);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-11-22 06:29:24 +01:00
|
|
|
* Returns a many-to-many component, as a ManyManyList.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $componentName Name of the many-many component
|
2016-09-26 07:22:19 +02:00
|
|
|
* @return RelationList|UnsavedRelationList The set of components
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public function getManyManyComponents($componentName) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
|
|
|
$manyManyComponent = $schema->manyManyComponent(static::class, $componentName);
|
2016-02-25 05:32:41 +01:00
|
|
|
if(!$manyManyComponent) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'",
|
|
|
|
$componentName,
|
|
|
|
$this->class
|
|
|
|
));
|
2015-02-25 12:13:26 +01:00
|
|
|
}
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
list($relationClass, $parentClass, $componentClass, $parentField, $componentField, $tableOrClass)
|
|
|
|
= $manyManyComponent;
|
2016-02-25 05:32:41 +01:00
|
|
|
|
2012-11-03 04:16:51 +01:00
|
|
|
// If we haven't been written yet, we can't save these relations, so use a list that handles this case
|
|
|
|
if(!$this->ID) {
|
|
|
|
if(!isset($this->unsavedRelations[$componentName])) {
|
|
|
|
$this->unsavedRelations[$componentName] =
|
2013-06-21 00:32:08 +02:00
|
|
|
new UnsavedRelationList($parentClass, $componentName, $componentClass);
|
2012-11-03 04:16:51 +01:00
|
|
|
}
|
|
|
|
return $this->unsavedRelations[$componentName];
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
$extraFields = $schema->manyManyExtraFieldsForComponent(static::class, $componentName) ?: array();
|
2016-09-26 07:22:19 +02:00
|
|
|
/** @var RelationList $result */
|
|
|
|
$result = Injector::inst()->create(
|
|
|
|
$relationClass,
|
|
|
|
$componentClass,
|
|
|
|
$tableOrClass,
|
|
|
|
$componentField,
|
|
|
|
$parentField,
|
|
|
|
$extraFields
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-10-07 16:12:00 +02:00
|
|
|
|
|
|
|
// Store component data in query meta-data
|
|
|
|
$result = $result->alterDataQuery(function($query) use ($extraFields) {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var DataQuery $query */
|
2015-10-07 16:12:00 +02:00
|
|
|
$query->setQueryParam('Component.ExtraFields', $extraFields);
|
|
|
|
});
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
if($this->model) {
|
|
|
|
$result->setDataModel($this->model);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-22 04:39:40 +02:00
|
|
|
$this->extend('updateManyManyComponents', $result);
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2009-11-22 06:29:24 +01:00
|
|
|
// If this is called on a singleton, then we return an 'orphaned relation' that can have the
|
|
|
|
// foreignID set elsewhere.
|
2013-07-10 02:44:24 +02:00
|
|
|
return $result
|
2016-02-25 05:32:41 +01:00
|
|
|
->setDataQueryParam($this->getInheritableQueryParams())
|
|
|
|
->forForeignID($this->ID);
|
2015-02-25 12:13:26 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and
|
2013-07-10 02:44:24 +02:00
|
|
|
* their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2015-02-25 12:13:26 +01:00
|
|
|
* @return string|array The class of the one-to-one component, or an array of all one-to-one components and
|
|
|
|
* their classes.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public function hasOne() {
|
2015-03-03 13:41:04 +01:00
|
|
|
return (array)Config::inst()->get($this->class, 'has_one', Config::INHERITED);
|
2015-02-25 12:13:26 +01:00
|
|
|
}
|
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
/**
|
|
|
|
* Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and
|
|
|
|
* their class name will be returned.
|
|
|
|
*
|
|
|
|
* @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have
|
|
|
|
* the field data stripped off. It defaults to TRUE.
|
|
|
|
* @return string|array
|
|
|
|
*/
|
2016-09-26 07:22:19 +02:00
|
|
|
public function belongsTo($classOnly = true) {
|
2015-02-25 12:13:26 +01:00
|
|
|
$belongsTo = (array)Config::inst()->get($this->class, 'belongs_to', Config::INHERITED);
|
2009-10-24 01:27:51 +02:00
|
|
|
if($belongsTo && $classOnly) {
|
|
|
|
return preg_replace('/(.+)?\..+/', '$1', $belongsTo);
|
|
|
|
} else {
|
|
|
|
return $belongsTo ? $belongsTo : array();
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-24 01:26:10 +02:00
|
|
|
* Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many
|
|
|
|
* relationships and their classes will be returned.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2009-10-24 01:26:10 +02:00
|
|
|
* @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have
|
|
|
|
* the field data stripped off. It defaults to TRUE.
|
2015-02-25 12:13:26 +01:00
|
|
|
* @return string|array|false
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-09-26 07:22:19 +02:00
|
|
|
public function hasMany($classOnly = true) {
|
2015-02-25 12:13:26 +01:00
|
|
|
$hasMany = (array)Config::inst()->get($this->class, 'has_many', Config::INHERITED);
|
2009-10-24 01:26:10 +02:00
|
|
|
if($hasMany && $classOnly) {
|
|
|
|
return preg_replace('/(.+)?\..+/', '$1', $hasMany);
|
2009-08-11 10:49:52 +02:00
|
|
|
} else {
|
2009-10-24 01:26:10 +02:00
|
|
|
return $hasMany ? $hasMany : array();
|
2009-08-11 10:49:52 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-02-10 07:04:36 +01:00
|
|
|
/**
|
|
|
|
* Return the many-to-many extra fields specification.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-02-10 07:04:36 +01:00
|
|
|
* If you don't specify a component name, it returns all
|
|
|
|
* extra fields for all components available.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2015-02-25 12:13:26 +01:00
|
|
|
* @return array|null
|
2009-02-10 07:04:36 +01:00
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public function manyManyExtraFields() {
|
2015-03-03 13:41:04 +01:00
|
|
|
return Config::inst()->get($this->class, 'many_many_extraFields', Config::INHERITED);
|
2015-02-25 12:13:26 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-15 01:10:25 +02:00
|
|
|
* Return information about a many-to-many component.
|
2007-07-19 12:40:28 +02:00
|
|
|
* The return value is an array of (parentclass, childclass). If $component is null, then all many-many
|
|
|
|
* components are returned.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2016-10-06 06:31:38 +02:00
|
|
|
* @see DataObjectSchema::manyManyComponent()
|
2015-02-25 12:13:26 +01:00
|
|
|
* @return array|null An array of (parentclass, childclass), or an array of all many-many components
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-02-25 05:32:41 +01:00
|
|
|
public function manyMany() {
|
2015-02-25 12:13:26 +01:00
|
|
|
$manyManys = (array)Config::inst()->get($this->class, 'many_many', Config::INHERITED);
|
|
|
|
$belongsManyManys = (array)Config::inst()->get($this->class, 'belongs_many_many', Config::INHERITED);
|
|
|
|
$items = array_merge($manyManys, $belongsManyManys);
|
|
|
|
return $items;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2009-10-08 03:21:24 +02:00
|
|
|
/**
|
|
|
|
* This returns an array (if it exists) describing the database extensions that are required, or false if none
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-10-08 03:21:24 +02:00
|
|
|
* This is experimental, and is currently only a Postgres-specific enhancement.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $class
|
|
|
|
* @return array|false
|
2009-10-08 03:21:24 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function database_extensions($class){
|
2012-04-12 18:26:56 +02:00
|
|
|
$extensions = Config::inst()->get($class, 'database_extensions', Config::UNINHERITED);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-05-25 07:30:01 +02:00
|
|
|
if($extensions) {
|
2009-10-08 03:21:24 +02:00
|
|
|
return $extensions;
|
2016-05-25 07:30:01 +02:00
|
|
|
} else {
|
2009-10-08 03:21:24 +02:00
|
|
|
return false;
|
2016-05-25 07:30:01 +02:00
|
|
|
}
|
2009-10-08 03:21:24 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Generates a SearchContext to be used for building and processing
|
|
|
|
* a generic search form for properties on this object.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-06 04:43:46 +02:00
|
|
|
* @return SearchContext
|
|
|
|
*/
|
|
|
|
public function getDefaultSearchContext() {
|
2008-08-11 01:29:30 +02:00
|
|
|
return new SearchContext(
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->class,
|
|
|
|
$this->scaffoldSearchFields(),
|
2008-08-11 01:29:30 +02:00
|
|
|
$this->defaultSearchFilters()
|
|
|
|
);
|
2008-08-06 04:43:46 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Determine which properties on the DataObject are
|
|
|
|
* searchable, and map them to their default {@link FormField}
|
2008-08-09 06:06:52 +02:00
|
|
|
* representations. Used for scaffolding a searchform for {@link ModelAdmin}.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2008-08-09 07:04:15 +02:00
|
|
|
* Some additional logic is included for switching field labels, based on
|
|
|
|
* how generic or specific the field type is.
|
2008-08-06 04:43:46 +02:00
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* Used by {@link SearchContext}.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-14 00:20:41 +02:00
|
|
|
* @param array $_params
|
2012-12-08 12:20:20 +01:00
|
|
|
* 'fieldClasses': Associative array of field names as keys and FormField classes as values
|
|
|
|
* 'restrictFields': Numeric array of a field name whitelist
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2008-08-06 04:43:46 +02:00
|
|
|
*/
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldSearchFields($_params = null) {
|
|
|
|
$params = array_merge(
|
|
|
|
array(
|
|
|
|
'fieldClasses' => false,
|
|
|
|
'restrictFields' => false
|
|
|
|
),
|
|
|
|
(array)$_params
|
|
|
|
);
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList();
|
2008-08-11 01:29:30 +02:00
|
|
|
foreach($this->searchableFields() as $fieldName => $spec) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if($params['restrictFields'] && !in_array($fieldName, $params['restrictFields'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-05 21:22:54 +02:00
|
|
|
// If a custom fieldclass is provided as a string, use it
|
2016-08-19 00:51:35 +02:00
|
|
|
$field = null;
|
2008-10-14 00:20:41 +02:00
|
|
|
if($params['fieldClasses'] && isset($params['fieldClasses'][$fieldName])) {
|
|
|
|
$fieldClass = $params['fieldClasses'][$fieldName];
|
2008-10-05 21:22:54 +02:00
|
|
|
$field = new $fieldClass($fieldName);
|
2008-08-11 01:29:30 +02:00
|
|
|
// If we explicitly set a field, then construct that
|
2008-10-05 21:22:54 +02:00
|
|
|
} else if(isset($spec['field'])) {
|
2008-08-11 05:13:53 +02:00
|
|
|
// If it's a string, use it as a class name and construct
|
|
|
|
if(is_string($spec['field'])) {
|
|
|
|
$fieldClass = $spec['field'];
|
|
|
|
$field = new $fieldClass($fieldName);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-11 05:13:53 +02:00
|
|
|
// If it's a FormField object, then just use that object directly.
|
|
|
|
} else if($spec['field'] instanceof FormField) {
|
|
|
|
$field = $spec['field'];
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-11 05:13:53 +02:00
|
|
|
// Otherwise we have a bug
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Bad value for searchable_fields, 'field' value: "
|
|
|
|
. var_export($spec['field'], true), E_USER_WARNING);
|
2008-08-11 05:13:53 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
// Otherwise, use the database field's scaffolder
|
|
|
|
} else {
|
|
|
|
$field = $this->relObject($fieldName)->scaffoldSearchField();
|
|
|
|
}
|
|
|
|
|
2015-09-10 05:46:23 +02:00
|
|
|
// Allow fields to opt out of search
|
|
|
|
if(!$field) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
if (strstr($fieldName, '.')) {
|
|
|
|
$field->setName(str_replace('.', '__', $fieldName));
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2008-08-11 01:29:30 +02:00
|
|
|
$field->setTitle($spec['title']);
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$fields->push($field);
|
|
|
|
}
|
2008-08-06 04:43:46 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Scaffold a simple edit form for all properties on this dataobject,
|
2008-08-11 01:29:30 +02:00
|
|
|
* based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}.
|
|
|
|
* Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}.
|
2008-08-06 04:43:46 +02:00
|
|
|
*
|
2008-10-14 00:20:41 +02:00
|
|
|
* @uses FormScaffolder
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-14 00:20:41 +02:00
|
|
|
* @param array $_params Associative array passing through properties to {@link FormScaffolder}.
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2008-08-06 04:43:46 +02:00
|
|
|
*/
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormFields($_params = null) {
|
|
|
|
$params = array_merge(
|
|
|
|
array(
|
|
|
|
'tabbed' => false,
|
|
|
|
'includeRelations' => false,
|
|
|
|
'restrictFields' => false,
|
|
|
|
'fieldClasses' => false,
|
|
|
|
'ajaxSafe' => false
|
|
|
|
),
|
|
|
|
(array)$_params
|
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
$fs = new FormScaffolder($this);
|
|
|
|
$fs->tabbed = $params['tabbed'];
|
|
|
|
$fs->includeRelations = $params['includeRelations'];
|
|
|
|
$fs->restrictFields = $params['restrictFields'];
|
|
|
|
$fs->fieldClasses = $params['fieldClasses'];
|
|
|
|
$fs->ajaxSafe = $params['ajaxSafe'];
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-05-14 05:03:22 +02:00
|
|
|
return $fs->getFieldList();
|
2008-10-03 20:38:52 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
/**
|
|
|
|
* Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields
|
|
|
|
* being called on extensions
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2013-05-16 00:34:45 +02:00
|
|
|
* @param callable $callback The callback to execute
|
|
|
|
*/
|
|
|
|
protected function beforeUpdateCMSFields($callback) {
|
|
|
|
$this->beforeExtending('updateCMSFields', $callback);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Centerpiece of every data administration interface in Silverstripe,
|
2011-10-29 06:09:12 +02:00
|
|
|
* which returns a {@link FieldList} suitable for a {@link Form} object.
|
2008-08-06 04:43:46 +02:00
|
|
|
* If not overloaded, we're using {@link scaffoldFormFields()} to automatically
|
2008-10-03 18:21:09 +02:00
|
|
|
* generate this set. To customize, overload this method in a subclass
|
2011-04-15 11:35:30 +02:00
|
|
|
* or extended onto it by using {@link DataExtension->updateCMSFields()}.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* <code>
|
2012-12-08 12:20:20 +01:00
|
|
|
* class MyCustomClass extends DataObject {
|
|
|
|
* static $db = array('CustomProperty'=>'Boolean');
|
|
|
|
*
|
|
|
|
* function getCMSFields() {
|
|
|
|
* $fields = parent::getCMSFields();
|
|
|
|
* $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty'));
|
|
|
|
* return $fields;
|
|
|
|
* }
|
2008-08-06 04:43:46 +02:00
|
|
|
* }
|
2009-03-22 23:59:14 +01:00
|
|
|
* </code>
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-09-26 06:26:06 +02:00
|
|
|
* @see Good example of complex FormField building: SiteTree::getCMSFields()
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms.
|
2008-08-06 04:43:46 +02:00
|
|
|
*/
|
2012-04-13 15:46:47 +02:00
|
|
|
public function getCMSFields() {
|
|
|
|
$tabbedFields = $this->scaffoldFormFields(array(
|
|
|
|
// Don't allow has_many/many_many relationship editing before the record is first saved
|
|
|
|
'includeRelations' => ($this->ID > 0),
|
|
|
|
'tabbed' => true,
|
|
|
|
'ajaxSafe' => true
|
2008-10-14 00:20:41 +02:00
|
|
|
));
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-03 20:38:52 +02:00
|
|
|
$this->extend('updateCMSFields', $tabbedFields);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-03 20:38:52 +02:00
|
|
|
return $tabbedFields;
|
2008-10-03 18:21:09 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* need to be overload by solid dataobject, so that the customised actions of that dataobject,
|
2011-04-15 11:35:30 +02:00
|
|
|
* including that dataobject's extensions customised actions could be added to the EditForm.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @return FieldList an Empty FieldList(); need to be overload by solid subclass
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
public function getCMSActions() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$actions = new FieldList();
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->extend('updateCMSActions', $actions);
|
|
|
|
return $actions;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2008-10-03 18:21:09 +02:00
|
|
|
/**
|
|
|
|
* Used for simple frontend forms without relation editing
|
|
|
|
* or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()}
|
|
|
|
* by default. To customize, either overload this method in your
|
2011-04-15 11:35:30 +02:00
|
|
|
* subclass, or extend it by {@link DataExtension->updateFrontEndFields()}.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-14 00:20:41 +02:00
|
|
|
* @todo Decide on naming for "website|frontend|site|page" and stick with it in the API
|
2008-10-03 18:21:09 +02:00
|
|
|
*
|
2008-10-14 00:20:41 +02:00
|
|
|
* @param array $params See {@link scaffoldFormFields()}
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList Always returns a simple field collection without TabSet.
|
2008-10-03 18:21:09 +02:00
|
|
|
*/
|
2008-10-14 00:20:41 +02:00
|
|
|
public function getFrontEndFields($params = null) {
|
|
|
|
$untabbedFields = $this->scaffoldFormFields($params);
|
2009-02-02 00:49:53 +01:00
|
|
|
$this->extend('updateFrontEndFields', $untabbedFields);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
return $untabbedFields;
|
2008-08-06 04:43:46 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of a field.
|
|
|
|
* Called by {@link __get()} and any getFieldName() methods you might create.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $field The name of the field
|
|
|
|
* @return mixed The field value
|
|
|
|
*/
|
2008-11-06 07:32:05 +01:00
|
|
|
public function getField($field) {
|
2008-08-09 04:16:46 +02:00
|
|
|
// If we already have an object in $this->record, then we should just return that
|
2015-09-04 05:49:22 +02:00
|
|
|
if(isset($this->record[$field]) && is_object($this->record[$field])) {
|
|
|
|
return $this->record[$field];
|
|
|
|
}
|
2012-04-20 00:08:17 +02:00
|
|
|
|
|
|
|
// Do we have a field that needs to be lazy loaded?
|
|
|
|
if(isset($this->record[$field.'_Lazy'])) {
|
|
|
|
$tableClass = $this->record[$field.'_Lazy'];
|
|
|
|
$this->loadLazyFields($tableClass);
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
// In case of complex fields, return the DBField object
|
2016-10-06 06:31:38 +02:00
|
|
|
if (static::getSchema()->compositeField(static::class, $field)) {
|
2015-09-10 05:46:23 +02:00
|
|
|
$this->record[$field] = $this->dbObject($field);
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return isset($this->record[$field]) ? $this->record[$field] : null;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
/**
|
2012-07-18 06:28:24 +02:00
|
|
|
* Loads all the stub fields that an initial lazy load didn't load fully.
|
2012-04-20 00:08:17 +02:00
|
|
|
*
|
2016-05-25 07:09:29 +02:00
|
|
|
* @param string $class Class to load the values from. Others are joined as required.
|
|
|
|
* Not specifying a tableClass will load all lazy fields from all tables.
|
2016-05-18 01:00:04 +02:00
|
|
|
* @return bool Flag if lazy loading succeeded
|
2012-04-20 00:08:17 +02:00
|
|
|
*/
|
2016-05-25 07:09:29 +02:00
|
|
|
protected function loadLazyFields($class = null) {
|
2016-05-18 01:00:04 +02:00
|
|
|
if(!$this->isInDB() || !is_numeric($this->ID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
if (!$class) {
|
2012-07-18 06:28:24 +02:00
|
|
|
$loaded = array();
|
|
|
|
|
|
|
|
foreach ($this->record as $key => $value) {
|
|
|
|
if (strlen($key) > 5 && substr($key, -5) == '_Lazy' && !array_key_exists($value, $loaded)) {
|
|
|
|
$this->loadLazyFields($value);
|
|
|
|
$loaded[$value] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:00:04 +02:00
|
|
|
return false;
|
2012-07-18 06:28:24 +02:00
|
|
|
}
|
2012-04-20 00:08:17 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
$dataQuery = new DataQuery($class);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// Reset query parameter context to that of this DataObject
|
|
|
|
if($params = $this->getSourceQueryParams()) {
|
2016-03-02 06:18:10 +01:00
|
|
|
foreach($params as $key => $value) {
|
|
|
|
$dataQuery->setQueryParam($key, $value);
|
|
|
|
}
|
2014-03-04 02:48:04 +01:00
|
|
|
}
|
2012-05-15 07:18:04 +02:00
|
|
|
|
2012-10-10 03:59:45 +02:00
|
|
|
// Limit query to the current record, unless it has the Versioned extension,
|
|
|
|
// in which case it requires special handling through augmentLoadLazyFields()
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
|
|
|
$baseIDColumn = $schema->sqlColumnForField($this, 'ID');
|
2016-03-02 06:18:10 +01:00
|
|
|
$dataQuery->where([
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseIDColumn => $this->record['ID']
|
2016-03-02 06:18:10 +01:00
|
|
|
])->limit(1);
|
2013-02-12 04:44:32 +01:00
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
$columns = array();
|
|
|
|
|
|
|
|
// Add SQL for fields, both simple & multi-value
|
|
|
|
// TODO: This is copy & pasted from buildSQL(), it could be moved into a method
|
2016-10-06 06:31:38 +02:00
|
|
|
$databaseFields = $schema->databaseFields($class, false);
|
|
|
|
foreach($databaseFields as $k => $v) {
|
2012-04-20 00:08:17 +02:00
|
|
|
if(!isset($this->record[$k]) || $this->record[$k] === null) {
|
|
|
|
$columns[] = $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($columns) {
|
2012-10-10 03:59:45 +02:00
|
|
|
$query = $dataQuery->query();
|
2013-02-12 04:44:32 +01:00
|
|
|
$this->extend('augmentLoadLazyFields', $query, $dataQuery, $this);
|
2012-04-20 00:08:17 +02:00
|
|
|
$this->extend('augmentSQL', $query, $dataQuery);
|
|
|
|
|
|
|
|
$dataQuery->setQueriedColumns($columns);
|
|
|
|
$newData = $dataQuery->execute()->record();
|
|
|
|
|
|
|
|
// Load the data into record
|
|
|
|
if($newData) {
|
|
|
|
foreach($newData as $k => $v) {
|
|
|
|
if (in_array($k, $columns)) {
|
|
|
|
$this->record[$k] = $v;
|
|
|
|
$this->original[$k] = $v;
|
|
|
|
unset($this->record[$k . '_Lazy']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No data means that the query returned nothing; assign 'null' to all the requested fields
|
|
|
|
} else {
|
|
|
|
foreach($columns as $k) {
|
|
|
|
$this->record[$k] = null;
|
|
|
|
$this->original[$k] = null;
|
|
|
|
unset($this->record[$k . '_Lazy']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 01:00:04 +02:00
|
|
|
return true;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the fields that have changed.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* The change level affects what the functions defines as "changed":
|
2013-10-17 01:55:58 +02:00
|
|
|
* - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones.
|
|
|
|
* - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes,
|
|
|
|
* for example a change from 0 to null would not be included.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2008-08-11 05:13:53 +02:00
|
|
|
* Example return:
|
|
|
|
* <code>
|
|
|
|
* array(
|
2013-10-17 01:55:58 +02:00
|
|
|
* 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE)
|
2008-08-11 05:13:53 +02:00
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
2015-09-04 05:49:22 +02:00
|
|
|
* @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true
|
|
|
|
* to return all database fields, or an array for an explicit filter. false returns all fields.
|
2013-10-17 01:55:58 +02:00
|
|
|
* @param int $changeLevel The strictness of what is defined as change. Defaults to strict
|
2008-08-11 05:13:53 +02:00
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-10-17 01:55:58 +02:00
|
|
|
public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) {
|
2008-08-11 05:13:53 +02:00
|
|
|
$changedFields = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-05-23 07:32:16 +02:00
|
|
|
// Update the changed array with references to changed obj-fields
|
|
|
|
foreach($this->record as $k => $v) {
|
2015-08-30 07:02:55 +02:00
|
|
|
// Prevents DBComposite infinite looping on isChanged
|
2015-09-04 05:49:22 +02:00
|
|
|
if(is_array($databaseFieldsOnly) && !in_array($k, $databaseFieldsOnly)) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-23 07:32:16 +02:00
|
|
|
if(is_object($v) && method_exists($v, 'isChanged') && $v->isChanged()) {
|
2013-10-17 01:55:58 +02:00
|
|
|
$this->changed[$k] = self::CHANGE_VALUE;
|
2009-05-23 07:32:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
if(is_array($databaseFieldsOnly)) {
|
|
|
|
$fields = array_intersect_key((array)$this->changed, array_flip($databaseFieldsOnly));
|
|
|
|
} elseif($databaseFieldsOnly) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$fieldsSpecs = static::getSchema()->fieldSpecs(static::class);
|
|
|
|
$fields = array_intersect_key((array)$this->changed, $fieldsSpecs);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$fields = $this->changed;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Filter the list to those of a certain change level
|
2013-10-17 01:55:58 +02:00
|
|
|
if($changeLevel > self::CHANGE_STRICT) {
|
2009-05-27 02:09:23 +02:00
|
|
|
if($fields) foreach($fields as $name => $level) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($level < $changeLevel) {
|
|
|
|
unset($fields[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-07-17 01:47:50 +02:00
|
|
|
if($fields) foreach($fields as $name => $level) {
|
2008-08-11 05:13:53 +02:00
|
|
|
$changedFields[$name] = array(
|
2008-10-08 04:00:12 +02:00
|
|
|
'before' => array_key_exists($name, $this->original) ? $this->original[$name] : null,
|
|
|
|
'after' => array_key_exists($name, $this->record) ? $this->record[$name] : null,
|
2008-08-11 05:13:53 +02:00
|
|
|
'level' => $level
|
|
|
|
);
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2008-08-11 05:13:53 +02:00
|
|
|
return $changedFields;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
/**
|
|
|
|
* Uses {@link getChangedFields()} to determine if fields have been changed
|
|
|
|
* since loading them from the database.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-13 03:48:48 +02:00
|
|
|
* @param string $fieldName Name of the database field to check, will check for any if not given
|
2009-05-27 02:09:23 +02:00
|
|
|
* @param int $changeLevel See {@link getChangedFields()}
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-10-17 01:55:58 +02:00
|
|
|
public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) {
|
2016-05-18 08:36:10 +02:00
|
|
|
$fields = $fieldName ? array($fieldName) : true;
|
2015-09-04 05:49:22 +02:00
|
|
|
$changed = $this->getChangedFields($fields, $changeLevel);
|
2010-10-13 03:48:48 +02:00
|
|
|
if(!isset($fieldName)) {
|
|
|
|
return !empty($changed);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2010-10-13 03:48:48 +02:00
|
|
|
else {
|
|
|
|
return array_key_exists($fieldName, $changed);
|
|
|
|
}
|
2009-05-27 02:09:23 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the value of the field
|
|
|
|
* Called by {@link __set()} and any setFieldName() methods you might create.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $fieldName Name of the field
|
2007-12-02 22:28:02 +01:00
|
|
|
* @param mixed $val New field value
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setField($fieldName, $val) {
|
2016-06-03 10:51:02 +02:00
|
|
|
$this->objCacheClear();
|
2015-06-16 18:38:34 +02:00
|
|
|
//if it's a has_one component, destroy the cache
|
|
|
|
if (substr($fieldName, -2) == 'ID') {
|
|
|
|
unset($this->components[substr($fieldName, 0, -2)]);
|
|
|
|
}
|
2015-09-04 05:49:22 +02:00
|
|
|
|
|
|
|
// If we've just lazy-loaded the column, then we need to populate the $original array
|
|
|
|
if(isset($this->record[$fieldName.'_Lazy'])) {
|
|
|
|
$tableClass = $this->record[$fieldName.'_Lazy'];
|
|
|
|
$this->loadLazyFields($tableClass);
|
|
|
|
}
|
|
|
|
|
2010-04-12 03:36:30 +02:00
|
|
|
// Situation 1: Passing an DBField
|
|
|
|
if($val instanceof DBField) {
|
2015-09-04 05:49:22 +02:00
|
|
|
$val->setName($fieldName);
|
|
|
|
$val->saveInto($this);
|
2012-04-20 00:08:17 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
// Situation 1a: Composite fields should remain bound in case they are
|
|
|
|
// later referenced to update the parent dataobject
|
2015-08-30 07:02:55 +02:00
|
|
|
if($val instanceof DBComposite) {
|
2015-09-04 05:49:22 +02:00
|
|
|
$val->bindTo($this);
|
|
|
|
$this->record[$fieldName] = $val;
|
|
|
|
}
|
2010-04-12 03:36:30 +02:00
|
|
|
// Situation 2: Passing a literal or non-DBField object
|
|
|
|
} else {
|
|
|
|
// If this is a proper database field, we shouldn't be getting non-DBField objects
|
2016-10-06 06:31:38 +02:00
|
|
|
if(is_object($val) && static::getSchema()->fieldSpec(static::class, $fieldName)) {
|
|
|
|
throw new InvalidArgumentException('DataObject::setField: passed an object that is not a DBField');
|
2009-07-23 02:31:52 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
// if a field is not existing or has strictly changed
|
2008-10-08 04:25:26 +02:00
|
|
|
if(!isset($this->record[$fieldName]) || $this->record[$fieldName] !== $val) {
|
2008-08-09 04:16:46 +02:00
|
|
|
// TODO Add check for php-level defaults which are not set in the db
|
|
|
|
// TODO Add check for hidden input-fields (readonly) which are not set in the db
|
2008-10-08 04:00:12 +02:00
|
|
|
// At the very least, the type has changed
|
2013-10-17 01:55:58 +02:00
|
|
|
$this->changed[$fieldName] = self::CHANGE_STRICT;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if ((!isset($this->record[$fieldName]) && $val)
|
|
|
|
|| (isset($this->record[$fieldName]) && $this->record[$fieldName] != $val)
|
|
|
|
) {
|
2008-10-08 04:00:12 +02:00
|
|
|
// Value has changed as well, not just the type
|
2013-10-17 01:55:58 +02:00
|
|
|
$this->changed[$fieldName] = self::CHANGE_VALUE;
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
// Value is always saved back when strict check succeeds.
|
2008-08-09 04:16:46 +02:00
|
|
|
$this->record[$fieldName] = $val;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set the value of the field, using a casting object.
|
|
|
|
* This is useful when you aren't sure that a date is in SQL format, for example.
|
|
|
|
* setCastedField() can also be used, by forms, to set related data. For example, uploaded images
|
|
|
|
* can be saved into the Image table.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $fieldName Name of the field
|
|
|
|
* @param mixed $value New field value
|
2015-09-04 05:49:22 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setCastedField($fieldName, $value) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$fieldName) {
|
|
|
|
user_error("DataObject::setCastedField: Called without a fieldName", E_USER_ERROR);
|
|
|
|
}
|
2015-09-04 05:49:22 +02:00
|
|
|
$fieldObj = $this->dbObject($fieldName);
|
|
|
|
if($fieldObj) {
|
|
|
|
$fieldObj->setValue($value);
|
2007-07-19 12:40:28 +02:00
|
|
|
$fieldObj->saveInto($this);
|
|
|
|
} else {
|
2015-09-04 05:49:22 +02:00
|
|
|
$this->$fieldName = $value;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2015-10-07 16:12:00 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function castingHelper($field) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$fieldSpec = static::getSchema()->fieldSpec(static::class, $field);
|
|
|
|
if ($fieldSpec) {
|
2015-09-04 05:49:22 +02:00
|
|
|
return $fieldSpec;
|
|
|
|
}
|
2015-10-07 16:12:00 +02:00
|
|
|
|
|
|
|
// many_many_extraFields aren't presented by db(), so we check if the source query params
|
|
|
|
// provide us with meta-data for a many_many relation we can inspect for extra fields.
|
|
|
|
$queryParams = $this->getSourceQueryParams();
|
|
|
|
if (!empty($queryParams['Component.ExtraFields'])) {
|
|
|
|
$extraFields = $queryParams['Component.ExtraFields'];
|
|
|
|
|
|
|
|
if (isset($extraFields[$field])) {
|
|
|
|
return $extraFields[$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
return parent::castingHelper($field);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Returns true if the given field exists in a database column on any of
|
|
|
|
* the objects tables and optionally look up a dynamic getter with
|
2013-05-08 13:05:05 +02:00
|
|
|
* get<fieldName>().
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $field Name of the field
|
|
|
|
* @return boolean True if the given field exists
|
|
|
|
*/
|
|
|
|
public function hasField($field) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
2009-06-02 05:43:45 +02:00
|
|
|
return (
|
2012-04-20 00:08:17 +02:00
|
|
|
array_key_exists($field, $this->record)
|
2016-10-06 06:31:38 +02:00
|
|
|
|| $schema->fieldSpec(static::class, $field)
|
|
|
|
|| (substr($field,-2) == 'ID') && $schema->hasOneComponent(static::class, substr($field,0, -2))
|
2009-06-02 05:43:45 +02:00
|
|
|
|| $this->hasMethod("get{$field}")
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-03-12 10:21:49 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given field exists as a database column
|
|
|
|
*
|
|
|
|
* @param string $field Name of the field
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasDatabaseField($field) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$spec = static::getSchema()->fieldSpec(static::class, $field, ['dbOnly']);
|
|
|
|
return !empty($spec);
|
2008-11-06 05:51:25 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the member is allowed to do the given action.
|
2010-10-15 03:12:56 +02:00
|
|
|
* See {@link extendedCan()} for a more versatile tri-state permission control.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $perm The permission to be checked, such as 'View'.
|
|
|
|
* @param Member $member The member whose permissions need checking. Defaults to the currently logged
|
|
|
|
* in user.
|
2016-03-30 23:35:07 +02:00
|
|
|
* @param array $context Additional $context to pass to extendedCan()
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean True if the the member is allowed to do the given action
|
|
|
|
*/
|
2016-03-30 23:35:07 +02:00
|
|
|
public function can($perm, $member = null, $context = array()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!isset($member)) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2008-11-05 00:31:33 +01:00
|
|
|
if(Permission::checkMember($member, "ADMIN")) return true;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if($this->getSchema()->manyManyComponent(static::class, 'Can' . $perm)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->ParentID && $this->SecurityType == 'Inherit') {
|
|
|
|
if(!($p = $this->Parent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->Parent->can($perm, $member);
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$permissionCache = $this->uninherited('permissionCache');
|
2007-09-15 01:10:25 +02:00
|
|
|
$memberID = $member ? $member->ID : 'none';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!isset($permissionCache[$memberID][$perm])) {
|
|
|
|
if($member->ID) {
|
|
|
|
$groups = $member->Groups();
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupList = implode(', ', $groups->column("ID"));
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2011-03-23 00:05:44 +01:00
|
|
|
// TODO Fix relation table hardcoding
|
2013-06-21 00:32:08 +02:00
|
|
|
$query = new SQLSelect(
|
2008-11-22 04:51:04 +01:00
|
|
|
"\"Page_Can$perm\".PageID",
|
|
|
|
array("\"Page_Can$perm\""),
|
2007-07-19 12:40:28 +02:00
|
|
|
"GroupID IN ($groupList)");
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$permissionCache[$memberID][$perm] = $query->execute()->column();
|
|
|
|
|
|
|
|
if($perm == "View") {
|
2011-03-23 00:05:44 +01:00
|
|
|
// TODO Fix relation table hardcoding
|
2013-06-21 00:32:08 +02:00
|
|
|
$query = new SQLSelect("\"SiteTree\".\"ID\"", array(
|
2008-11-22 04:51:04 +01:00
|
|
|
"\"SiteTree\"",
|
2008-11-24 00:28:16 +01:00
|
|
|
"LEFT JOIN \"Page_CanView\" ON \"Page_CanView\".\"PageID\" = \"SiteTree\".\"ID\""
|
|
|
|
), "\"Page_CanView\".\"PageID\" IS NULL");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-09 07:04:15 +02:00
|
|
|
$unsecuredPages = $query->execute()->column();
|
|
|
|
if($permissionCache[$memberID][$perm]) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$permissionCache[$memberID][$perm]
|
|
|
|
= array_merge($permissionCache[$memberID][$perm], $unsecuredPages);
|
2008-08-09 07:04:15 +02:00
|
|
|
} else {
|
|
|
|
$permissionCache[$memberID][$perm] = $unsecuredPages;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 13:10:57 +02:00
|
|
|
Config::inst()->update($this->class, 'permissionCache', $permissionCache);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($permissionCache[$memberID][$perm]) {
|
|
|
|
return in_array($this->ID, $permissionCache[$memberID][$perm]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return parent::can($perm, $member);
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2010-10-15 02:33:48 +02:00
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Process tri-state responses from permission-alterting extensions. The extensions are
|
2010-10-15 03:12:56 +02:00
|
|
|
* expected to return one of three values:
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* - false: Disallow this permission, regardless of what other extensions say
|
|
|
|
* - true: Allow this permission, as long as no other extensions return false
|
2010-10-15 02:33:48 +02:00
|
|
|
* - NULL: Don't affect the outcome
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 02:33:48 +02:00
|
|
|
* This method itself returns a tri-state value, and is designed to be used like this:
|
|
|
|
*
|
2010-10-15 03:12:56 +02:00
|
|
|
* <code>
|
2010-10-15 02:33:48 +02:00
|
|
|
* $extended = $this->extendedCan('canDoSomething', $member);
|
|
|
|
* if($extended !== null) return $extended;
|
|
|
|
* else return $normalValue;
|
2010-10-15 03:12:56 +02:00
|
|
|
* </code>
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2015-06-09 01:31:07 +02:00
|
|
|
* @param string $methodName Method on the same object, e.g. {@link canEdit()}
|
2010-10-15 03:12:56 +02:00
|
|
|
* @param Member|int $member
|
2015-06-09 01:31:07 +02:00
|
|
|
* @param array $context Optional context
|
2010-10-15 03:12:56 +02:00
|
|
|
* @return boolean|null
|
2010-10-15 02:33:48 +02:00
|
|
|
*/
|
2015-06-09 01:31:07 +02:00
|
|
|
public function extendedCan($methodName, $member, $context = array()) {
|
|
|
|
$results = $this->extend($methodName, $member, $context);
|
2010-10-15 02:33:48 +02:00
|
|
|
if($results && is_array($results)) {
|
|
|
|
// Remove NULLs
|
2013-06-13 07:53:27 +02:00
|
|
|
$results = array_filter($results, function($v) {return !is_null($v);});
|
2010-10-15 02:33:48 +02:00
|
|
|
// If there are any non-NULL responses, then return the lowest one of them.
|
2013-06-21 00:32:08 +02:00
|
|
|
// If any explicitly deny the permission, then we don't get access
|
2010-10-15 02:33:48 +02:00
|
|
|
if($results) return min($results);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canView($member = null) {
|
2013-05-28 15:25:18 +02:00
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2008-09-22 18:02:03 +02:00
|
|
|
return Permission::check('ADMIN', 'any', $member);
|
2008-08-09 08:40:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null) {
|
2013-05-28 15:25:18 +02:00
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2008-09-22 18:02:03 +02:00
|
|
|
return Permission::check('ADMIN', 'any', $member);
|
2008-08-09 08:40:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canDelete($member = null) {
|
2013-05-28 15:25:18 +02:00
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2008-09-22 18:02:03 +02:00
|
|
|
return Permission::check('ADMIN', 'any', $member);
|
2008-08-09 08:40:50 +02:00
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
/**
|
|
|
|
* @param Member $member
|
2015-06-09 01:31:07 +02:00
|
|
|
* @param array $context Additional context-specific data which might
|
|
|
|
* affect whether (or where) this object could be created.
|
2008-08-09 08:40:50 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-06-09 01:31:07 +02:00
|
|
|
public function canCreate($member = null, $context = array()) {
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member, $context);
|
2013-05-28 15:25:18 +02:00
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2009-05-18 01:15:31 +02:00
|
|
|
return Permission::check('ADMIN', 'any', $member);
|
2008-08-09 08:40:50 +02:00
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Debugging used by Debug::show()
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string HTML data representing this object
|
|
|
|
*/
|
|
|
|
public function debug() {
|
2009-05-27 05:22:52 +02:00
|
|
|
$val = "<h3>Database record: $this->class</h3>\n<ul>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->record) foreach($this->record as $fieldName => $fieldVal) {
|
2009-05-27 05:22:52 +02:00
|
|
|
$val .= "\t<li>$fieldName: " . Debug::text($fieldVal) . "</li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-05-27 05:22:52 +02:00
|
|
|
$val .= "</ul>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
return $val;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the DBField object that represents the given field.
|
2008-10-01 02:55:25 +02:00
|
|
|
* This works similarly to obj() with 2 key differences:
|
|
|
|
* - it still returns an object even when the field has no value.
|
|
|
|
* - it only matches fields and not methods
|
|
|
|
* - it matches foreign keys generated by has_one relationships, eg, "ParentID"
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $fieldName Name of the field
|
|
|
|
* @return DBField The field as a DBField object
|
|
|
|
*/
|
|
|
|
public function dbObject($fieldName) {
|
2016-08-01 11:47:39 +02:00
|
|
|
// Check for field in DB
|
2016-10-06 06:31:38 +02:00
|
|
|
$helper = static::getSchema()->fieldSpec(static::class, $fieldName, ['includeClass']);
|
2016-08-01 11:47:39 +02:00
|
|
|
if(!$helper) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-09-10 05:46:23 +02:00
|
|
|
$value = isset($this->record[$fieldName])
|
|
|
|
? $this->record[$fieldName]
|
|
|
|
: null;
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
// If we have a DBField object in $this->record, then return that
|
2016-08-01 11:47:39 +02:00
|
|
|
if($value instanceof DBField) {
|
2015-09-10 05:46:23 +02:00
|
|
|
return $value;
|
2015-09-04 05:49:22 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-08-01 11:47:39 +02:00
|
|
|
list($table, $spec) = explode('.', $helper);
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var DBField $obj */
|
2016-08-01 11:47:39 +02:00
|
|
|
$obj = Object::create_from_string($spec, $fieldName);
|
|
|
|
$obj->setTable($table);
|
|
|
|
$obj->setValue($value, $this, false);
|
|
|
|
return $obj;
|
2015-09-04 05:49:22 +02:00
|
|
|
}
|
2013-07-02 15:09:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
2008-08-09 07:04:15 +02:00
|
|
|
* Traverses to a DBField referenced by relationships between data objects.
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* The path to the related field is specified with dot separated syntax
|
2013-05-08 13:05:05 +02:00
|
|
|
* (eg: Parent.Child.Child.FieldName).
|
|
|
|
*
|
|
|
|
* @param string $fieldPath
|
|
|
|
*
|
|
|
|
* @return mixed DBField of the field on the object or a DataList instance.
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
|
|
|
public function relObject($fieldPath) {
|
2013-05-08 13:05:05 +02:00
|
|
|
$object = null;
|
|
|
|
|
2012-03-24 02:03:55 +01:00
|
|
|
if(strpos($fieldPath, '.') !== false) {
|
|
|
|
$parts = explode('.', $fieldPath);
|
|
|
|
$fieldName = array_pop($parts);
|
|
|
|
|
|
|
|
// Traverse dot syntax
|
|
|
|
$component = $this;
|
2013-05-08 13:05:05 +02:00
|
|
|
|
2012-03-24 02:03:55 +01:00
|
|
|
foreach($parts as $relation) {
|
2012-03-30 04:59:47 +02:00
|
|
|
if($component instanceof SS_List) {
|
2013-05-08 13:05:05 +02:00
|
|
|
if(method_exists($component,$relation)) {
|
|
|
|
$component = $component->$relation();
|
|
|
|
} else {
|
|
|
|
$component = $component->relation($relation);
|
|
|
|
}
|
2012-03-30 04:59:47 +02:00
|
|
|
} else {
|
|
|
|
$component = $component->$relation();
|
|
|
|
}
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2012-03-24 02:03:55 +01:00
|
|
|
|
|
|
|
$object = $component->dbObject($fieldName);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$object = $this->dbObject($fieldPath);
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-08-09 07:04:15 +02:00
|
|
|
return $object;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2012-03-24 02:03:55 +01:00
|
|
|
/**
|
|
|
|
* Traverses to a field referenced by relationships between data objects, returning the value
|
|
|
|
* The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName)
|
|
|
|
*
|
2015-09-10 05:46:23 +02:00
|
|
|
* @param $fieldName string
|
2012-12-19 04:40:13 +01:00
|
|
|
* @return string | null - will return null on a missing value
|
2012-03-24 02:03:55 +01:00
|
|
|
*/
|
2012-07-27 04:44:38 +02:00
|
|
|
public function relField($fieldName) {
|
|
|
|
$component = $this;
|
|
|
|
|
2012-12-19 04:40:13 +01:00
|
|
|
// We're dealing with relations here so we traverse the dot syntax
|
2012-07-27 04:44:38 +02:00
|
|
|
if(strpos($fieldName, '.') !== false) {
|
2012-12-19 04:40:13 +01:00
|
|
|
$relations = explode('.', $fieldName);
|
|
|
|
$fieldName = array_pop($relations);
|
|
|
|
foreach($relations as $relation) {
|
2013-06-12 23:41:24 +02:00
|
|
|
// Inspect $component for element $relation
|
|
|
|
if($component->hasMethod($relation)) {
|
|
|
|
// Check nested method
|
2012-03-30 04:59:47 +02:00
|
|
|
$component = $component->$relation();
|
2013-06-12 23:41:24 +02:00
|
|
|
} elseif($component instanceof SS_List) {
|
|
|
|
// Select adjacent relation from DataList
|
|
|
|
$component = $component->relation($relation);
|
|
|
|
} elseif($component instanceof DataObject
|
|
|
|
&& ($dbObject = $component->dbObject($relation))
|
2013-06-21 00:32:08 +02:00
|
|
|
) {
|
2013-06-12 23:41:24 +02:00
|
|
|
// Select db object
|
|
|
|
$component = $dbObject;
|
|
|
|
} else {
|
|
|
|
user_error("$relation is not a relation/field on ".get_class($component), E_USER_ERROR);
|
2012-03-30 04:59:47 +02:00
|
|
|
}
|
2012-03-24 02:03:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-12-19 04:40:13 +01:00
|
|
|
// Bail if the component is null
|
|
|
|
if(!$component) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if($component->hasMethod($fieldName)) {
|
|
|
|
return $component->$fieldName();
|
|
|
|
}
|
2012-07-27 04:44:38 +02:00
|
|
|
return $component->$fieldName;
|
2012-03-24 02:03:55 +01:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
2008-08-11 01:17:51 +02:00
|
|
|
* Temporary hack to return an association name, based on class, to get around the mangle
|
2008-08-09 06:53:34 +02:00
|
|
|
* of having to deal with reverse lookup of relationships to determine autogenerated foreign keys.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param string $className
|
|
|
|
* @return string
|
2008-08-09 07:04:15 +02:00
|
|
|
*/
|
2008-08-11 01:17:51 +02:00
|
|
|
public function getReverseAssociation($className) {
|
2015-02-25 16:21:33 +01:00
|
|
|
if (is_array($this->manyMany())) {
|
|
|
|
$many_many = array_flip($this->manyMany());
|
2008-08-11 01:17:51 +02:00
|
|
|
if (array_key_exists($className, $many_many)) return $many_many[$className];
|
|
|
|
}
|
2015-02-25 16:21:33 +01:00
|
|
|
if (is_array($this->hasMany())) {
|
|
|
|
$has_many = array_flip($this->hasMany());
|
2008-08-09 06:53:34 +02:00
|
|
|
if (array_key_exists($className, $has_many)) return $has_many[$className];
|
|
|
|
}
|
2015-02-25 16:21:33 +01:00
|
|
|
if (is_array($this->hasOne())) {
|
|
|
|
$has_one = array_flip($this->hasOne());
|
2008-08-09 06:53:34 +02:00
|
|
|
if (array_key_exists($className, $has_one)) return $has_one[$className];
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
return false;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return all objects matching the filter
|
|
|
|
* sub-classes are automatically selected and included
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $callerClass The class of objects to be returned
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array $filter A filter to be inserted into the WHERE clause.
|
|
|
|
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
|
|
|
|
* @param string|array $sort A sort expression to be inserted into the ORDER
|
|
|
|
* BY clause. If omitted, self::$default_sort will be used.
|
2013-01-09 18:20:19 +01:00
|
|
|
* @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead.
|
2008-08-09 07:57:44 +02:00
|
|
|
* @param string|array $limit A limit expression to be inserted into the LIMIT clause.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $containerClass The container class to return the results in.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @todo $containerClass is Ignored, why?
|
|
|
|
*
|
|
|
|
* @return DataList The objects matching the filter, in the class specified by $containerClass
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null,
|
2016-10-06 06:31:38 +02:00
|
|
|
$containerClass = DataList::class) {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2012-04-20 04:58:24 +02:00
|
|
|
if($callerClass == null) {
|
|
|
|
$callerClass = get_called_class();
|
2016-10-06 06:31:38 +02:00
|
|
|
if ($callerClass == self::class) {
|
2012-05-28 11:12:06 +02:00
|
|
|
throw new \InvalidArgumentException('Call <classname>::get() instead of DataObject::get()');
|
2012-04-20 04:58:24 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if($filter || $sort || $join || $limit || ($containerClass != DataList::class)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new \InvalidArgumentException('If calling <classname>::get() then you shouldn\'t pass any other'
|
|
|
|
. ' arguments');
|
2012-04-20 04:58:24 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-04-20 04:58:24 +02:00
|
|
|
$result = DataList::create(get_called_class());
|
2012-05-01 04:43:52 +02:00
|
|
|
$result->setDataModel(DataModel::inst());
|
2012-04-20 04:58:24 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2013-01-09 18:20:19 +01:00
|
|
|
|
|
|
|
if($join) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'The $join argument has been removed. Use leftJoin($table, $joinClause) instead.'
|
|
|
|
);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-03-09 04:18:27 +01:00
|
|
|
$result = DataList::create($callerClass)->where($filter)->sort($sort);
|
2012-05-28 11:12:06 +02:00
|
|
|
|
2012-03-09 04:18:27 +01:00
|
|
|
if($limit && strpos($limit, ',') !== false) {
|
|
|
|
$limitArguments = explode(',', $limit);
|
2012-12-12 05:22:45 +01:00
|
|
|
$result = $result->limit($limitArguments[1],$limitArguments[0]);
|
2012-03-09 04:18:27 +01:00
|
|
|
} elseif($limit) {
|
2012-12-12 05:22:45 +01:00
|
|
|
$result = $result->limit($limit);
|
2012-03-09 04:18:27 +01:00
|
|
|
}
|
2012-05-28 11:12:06 +02:00
|
|
|
|
2012-05-01 04:43:52 +02:00
|
|
|
$result->setDataModel(DataModel::inst());
|
2009-11-22 06:16:38 +01:00
|
|
|
return $result;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the first item matching the given query.
|
|
|
|
* All calls to get_one() are cached.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $callerClass The class of objects to be returned
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array $filter A filter to be inserted into the WHERE clause.
|
|
|
|
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param boolean $cache Use caching
|
|
|
|
* @param string $orderby A sort expression to be inserted into the ORDER BY clause.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject The first item matching the query
|
|
|
|
*/
|
|
|
|
public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") {
|
2010-04-12 04:40:50 +02:00
|
|
|
$SNG = singleton($callerClass);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$cacheComponents = array($filter, $orderby, $SNG->extend('cacheKeyComponent'));
|
|
|
|
$cacheKey = md5(var_export($cacheComponents, true));
|
|
|
|
|
2008-08-11 07:31:13 +02:00
|
|
|
// Flush destroyed items out of the cache
|
2015-09-10 05:46:23 +02:00
|
|
|
if($cache && isset(self::$_cache_get_one[$callerClass][$cacheKey])
|
|
|
|
&& self::$_cache_get_one[$callerClass][$cacheKey] instanceof DataObject
|
|
|
|
&& self::$_cache_get_one[$callerClass][$cacheKey]->destroyed) {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2015-09-10 05:46:23 +02:00
|
|
|
self::$_cache_get_one[$callerClass][$cacheKey] = false;
|
2008-08-11 07:31:13 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
$item = null;
|
2015-09-10 05:46:23 +02:00
|
|
|
if(!$cache || !isset(self::$_cache_get_one[$callerClass][$cacheKey])) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$dl = DataObject::get($callerClass)->where($filter)->sort($orderby);
|
2016-08-19 00:51:35 +02:00
|
|
|
$item = $dl->first();
|
2009-11-22 06:16:38 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($cache) {
|
2015-09-10 05:46:23 +02:00
|
|
|
self::$_cache_get_one[$callerClass][$cacheKey] = $item;
|
|
|
|
if(!self::$_cache_get_one[$callerClass][$cacheKey]) {
|
|
|
|
self::$_cache_get_one[$callerClass][$cacheKey] = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-10 05:46:23 +02:00
|
|
|
return $cache ? self::$_cache_get_one[$callerClass][$cacheKey] : $item;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-08-11 05:03:52 +02:00
|
|
|
* Flush the cached results for all relations (has_one, has_many, many_many)
|
2012-04-12 07:03:18 +02:00
|
|
|
* Also clears any cached aggregate data.
|
2012-04-12 02:47:17 +02:00
|
|
|
*
|
|
|
|
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
|
2013-06-21 00:32:08 +02:00
|
|
|
* When false will just clear session-local cached data
|
2013-02-12 11:21:15 +01:00
|
|
|
* @return DataObject $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-04-12 02:47:17 +02:00
|
|
|
public function flushCache($persistent = true) {
|
2016-10-06 06:31:38 +02:00
|
|
|
if($this->class == self::class) {
|
2015-09-10 05:46:23 +02:00
|
|
|
self::$_cache_get_one = array();
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-08-16 08:32:49 +02:00
|
|
|
}
|
2007-09-16 18:40:57 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$classes = ClassInfo::ancestry($this->class);
|
|
|
|
foreach($classes as $class) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(isset(self::$_cache_get_one[$class])) {
|
|
|
|
unset(self::$_cache_get_one[$class]);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-12-17 23:38:47 +01:00
|
|
|
$this->extend('flushCache');
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-04-12 05:31:44 +02:00
|
|
|
$this->components = array();
|
2013-02-12 11:21:15 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2012-04-12 05:31:44 +02:00
|
|
|
/**
|
|
|
|
* Flush the get_one global cache and destroy associated objects.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function flush_and_destroy_cache() {
|
2015-09-10 05:46:23 +02:00
|
|
|
if(self::$_cache_get_one) foreach(self::$_cache_get_one as $class => $items) {
|
2008-12-15 02:30:41 +01:00
|
|
|
if(is_array($items)) foreach($items as $item) {
|
|
|
|
if($item) $item->destroy();
|
|
|
|
}
|
|
|
|
}
|
2015-09-10 05:46:23 +02:00
|
|
|
self::$_cache_get_one = array();
|
2008-12-15 02:30:41 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-08-11 06:45:54 +02:00
|
|
|
/**
|
2012-04-12 05:31:44 +02:00
|
|
|
* Reset all global caches associated with DataObject.
|
2009-08-11 06:45:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function reset() {
|
2016-05-25 07:09:29 +02:00
|
|
|
// @todo Decouple these
|
2015-09-10 05:46:23 +02:00
|
|
|
DBClassName::clear_classname_cache();
|
2016-05-25 07:09:29 +02:00
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
static::getSchema()->reset();
|
2015-09-10 05:46:23 +02:00
|
|
|
self::$_cache_get_one = array();
|
|
|
|
self::$_cache_field_labels = array();
|
2009-08-11 06:45:54 +02:00
|
|
|
}
|
2008-12-15 02:30:41 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the given element, searching by ID
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param string $callerClass The class of the object to be returned
|
|
|
|
* @param int $id The id of the element
|
2009-05-27 02:33:02 +02:00
|
|
|
* @param boolean $cache See {@link get_one()}
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject The element
|
|
|
|
*/
|
2009-05-27 02:33:02 +02:00
|
|
|
public static function get_by_id($callerClass, $id, $cache = true) {
|
2013-06-21 00:32:08 +02:00
|
|
|
if(!is_numeric($id)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
user_error("DataObject::get_by_id passed a non-numeric ID #$id", E_USER_WARNING);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-05-25 07:09:29 +02:00
|
|
|
// Pass to get_one
|
|
|
|
$column = static::getSchema()->sqlColumnForField($callerClass, 'ID');
|
2013-06-21 00:32:08 +02:00
|
|
|
return DataObject::get_one($callerClass, array($column => $id), $cache);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
|
|
|
* Get the name of the base table for this object
|
2016-05-25 07:09:29 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
|
|
|
public function baseTable() {
|
2016-05-25 07:09:29 +02:00
|
|
|
return static::getSchema()->baseDataTable($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the base class for this object
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function baseClass() {
|
|
|
|
return static::getSchema()->baseDataClass($this);
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2013-02-12 04:44:32 +01:00
|
|
|
/**
|
2016-05-25 07:30:01 +02:00
|
|
|
* @var array Parameters used in the query that built this object.
|
2013-06-21 00:32:08 +02:00
|
|
|
* This can be used by decorators (e.g. lazy loading) to
|
2013-02-12 04:44:32 +01:00
|
|
|
* run additional queries using the same context.
|
|
|
|
*/
|
|
|
|
protected $sourceQueryParams;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see $sourceQueryParams
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSourceQueryParams() {
|
|
|
|
return $this->sourceQueryParams;
|
|
|
|
}
|
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* Get list of parameters that should be inherited to relations on this object
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getInheritableQueryParams() {
|
|
|
|
$params = $this->getSourceQueryParams();
|
|
|
|
$this->extend('updateInheritableQueryParams', $params);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2013-02-12 04:44:32 +01:00
|
|
|
/**
|
|
|
|
* @see $sourceQueryParams
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
public function setSourceQueryParams($array) {
|
|
|
|
$this->sourceQueryParams = $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see $sourceQueryParams
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
2013-02-12 04:44:32 +01:00
|
|
|
*/
|
|
|
|
public function setSourceQueryParam($key, $value) {
|
|
|
|
$this->sourceQueryParams[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see $sourceQueryParams
|
2016-05-25 07:30:01 +02:00
|
|
|
* @param string $key
|
|
|
|
* @return string
|
2013-02-12 04:44:32 +01:00
|
|
|
*/
|
|
|
|
public function getSourceQueryParam($key) {
|
2016-05-25 07:30:01 +02:00
|
|
|
if(isset($this->sourceQueryParams[$key])) {
|
|
|
|
return $this->sourceQueryParams[$key];
|
|
|
|
}
|
|
|
|
return null;
|
2013-02-12 04:44:32 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
//-------------------------------------------------------------------------------------------//
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the database indexes on this table.
|
|
|
|
* This array is indexed by the name of the field with the index, and
|
|
|
|
* the value is the type of index.
|
|
|
|
*/
|
2008-08-09 07:04:15 +02:00
|
|
|
public function databaseIndexes() {
|
2016-05-25 07:30:01 +02:00
|
|
|
$has_one = $this->uninherited('has_one');
|
|
|
|
$classIndexes = $this->uninherited('indexes');
|
2007-07-19 12:40:28 +02:00
|
|
|
//$fileIndexes = $this->uninherited('fileIndexes', true);
|
|
|
|
|
|
|
|
$indexes = array();
|
|
|
|
|
|
|
|
if($has_one) {
|
|
|
|
foreach($has_one as $relationshipName => $fieldType) {
|
|
|
|
$indexes[$relationshipName . 'ID'] = true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
|
|
|
if($classIndexes) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($classIndexes as $indexName => $indexType) {
|
|
|
|
$indexes[$indexName] = $indexType;
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if(get_parent_class($this) == self::class) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$indexes['ClassName'] = true;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $indexes;
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check the database schema and update it as necessary.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* @uses DataExtension->augmentDatabase()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function requireTable() {
|
2007-09-15 01:10:25 +02:00
|
|
|
// Only build the table if we've actually got fields
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
|
|
|
$fields = $schema->databaseFields(static::class, false);
|
|
|
|
$table = $schema->tableName(static::class);
|
|
|
|
$extensions = self::database_extensions(static::class);
|
2011-12-22 03:31:56 +01:00
|
|
|
|
2008-08-09 07:04:15 +02:00
|
|
|
$indexes = $this->databaseIndexes();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($fields) {
|
2016-10-06 06:31:38 +02:00
|
|
|
$hasAutoIncPK = get_parent_class($this) === self::class;
|
2016-05-25 07:09:29 +02:00
|
|
|
DB::require_table(
|
|
|
|
$table, $fields, $indexes, $hasAutoIncPK, $this->stat('create_table_options'), $extensions
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2016-05-25 07:09:29 +02:00
|
|
|
DB::dont_require_table($table);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Build any child tables for many_many items
|
2016-05-25 07:30:01 +02:00
|
|
|
if($manyMany = $this->uninherited('many_many')) {
|
|
|
|
$extras = $this->uninherited('many_many_extraFields');
|
2016-09-26 07:22:19 +02:00
|
|
|
foreach($manyMany as $component => $spec) {
|
|
|
|
// Get many_many spec
|
2016-10-06 06:31:38 +02:00
|
|
|
$manyManyComponent = $schema->manyManyComponent(static::class, $component);
|
2016-09-26 07:22:19 +02:00
|
|
|
list(
|
|
|
|
$relationClass, $parentClass, $componentClass,
|
|
|
|
$parentField, $childField, $tableOrClass
|
|
|
|
) = $manyManyComponent;
|
|
|
|
|
|
|
|
// Skip if backed by actual class
|
|
|
|
if (class_exists($tableOrClass)) {
|
|
|
|
continue;
|
2016-05-25 07:09:29 +02:00
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
|
|
|
|
// Build fields
|
2007-07-19 12:40:28 +02:00
|
|
|
$manymanyFields = array(
|
2016-09-26 07:22:19 +02:00
|
|
|
$parentField => "Int",
|
2016-05-25 07:09:29 +02:00
|
|
|
$childField => "Int",
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2016-09-26 07:22:19 +02:00
|
|
|
if(isset($extras[$component])) {
|
|
|
|
$manymanyFields = array_merge($manymanyFields, $extras[$component]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Build index list
|
|
|
|
$manymanyIndexes = array(
|
2016-09-26 07:22:19 +02:00
|
|
|
$parentField => true,
|
2016-05-25 07:09:29 +02:00
|
|
|
$childField => true,
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2016-09-26 07:22:19 +02:00
|
|
|
DB::require_table($tableOrClass, $manymanyFields, $manymanyIndexes, true, null, $extensions);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Let any extentions make their own database fields
|
|
|
|
$this->extend('augmentDatabase', $dummy);
|
|
|
|
}
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add default records to database. This function is called whenever the
|
|
|
|
* database is built, after the database tables have all been created. Overload
|
|
|
|
* this to add default records when the database is built, but make sure you
|
|
|
|
* call parent::requireDefaultRecords().
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* @uses DataExtension->requireDefaultRecords()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function requireDefaultRecords() {
|
|
|
|
$defaultRecords = $this->stat('default_records');
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!empty($defaultRecords)) {
|
2008-08-11 02:14:48 +02:00
|
|
|
$hasData = DataObject::get_one($this->class);
|
|
|
|
if(!$hasData) {
|
|
|
|
$className = $this->class;
|
|
|
|
foreach($defaultRecords as $record) {
|
2011-05-01 07:33:02 +02:00
|
|
|
$obj = $this->model->$className->newObject($record);
|
2008-08-11 02:14:48 +02:00
|
|
|
$obj->write();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-26 23:03:29 +01:00
|
|
|
DB::alteration_message("Added default records to $className table","created");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-11-07 13:18:35 +01:00
|
|
|
// Let any extentions make their own database default data
|
|
|
|
$this->extend('requireDefaultRecords', $dummy);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-06 05:43:48 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Get the default searchable fields for this object, as defined in the
|
|
|
|
* $searchable_fields list. If searchable fields are not defined on the
|
2013-05-08 13:05:05 +02:00
|
|
|
* data object, uses a default selection of summary fields.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-09 06:06:52 +02:00
|
|
|
* @return array
|
2008-08-06 05:43:48 +02:00
|
|
|
*/
|
2008-08-11 01:29:30 +02:00
|
|
|
public function searchableFields() {
|
|
|
|
// can have mixed format, need to make consistent in most verbose form
|
2008-08-09 06:06:52 +02:00
|
|
|
$fields = $this->stat('searchable_fields');
|
2008-08-11 01:29:30 +02:00
|
|
|
$labels = $this->fieldLabels();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2014-02-12 16:37:59 +01:00
|
|
|
// fallback to summary fields (unless empty array is explicitly specified)
|
|
|
|
if( ! $fields && ! is_array($fields)) {
|
2013-05-08 13:05:05 +02:00
|
|
|
$summaryFields = array_keys($this->summaryFields());
|
|
|
|
$fields = array();
|
|
|
|
|
2014-02-12 16:37:59 +01:00
|
|
|
// remove the custom getters as the search should not include them
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = static::getSchema();
|
2013-05-08 13:05:05 +02:00
|
|
|
if($summaryFields) {
|
|
|
|
foreach($summaryFields as $key => $name) {
|
2014-02-12 16:37:59 +01:00
|
|
|
$spec = $name;
|
|
|
|
|
|
|
|
// Extract field name in case this is a method called on a field (e.g. "Date.Nice")
|
|
|
|
if(($fieldPos = strpos($name, '.')) !== false) {
|
|
|
|
$name = substr($name, 0, $fieldPos);
|
|
|
|
}
|
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
if ($schema->fieldSpec($this, $name)) {
|
2013-05-08 13:05:05 +02:00
|
|
|
$fields[] = $name;
|
2016-10-06 06:31:38 +02:00
|
|
|
} elseif ($this->relObject($spec)) {
|
2014-02-12 16:37:59 +01:00
|
|
|
$fields[] = $spec;
|
2013-05-08 13:05:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-09-30 02:20:30 +02:00
|
|
|
// we need to make sure the format is unified before
|
2011-04-15 11:35:30 +02:00
|
|
|
// augmenting fields, so extensions can apply consistent checks
|
|
|
|
// but also after augmenting fields, because the extension
|
2008-09-30 02:20:30 +02:00
|
|
|
// might use the shorthand notation as well
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
// rewrite array, if it is using shorthand syntax
|
|
|
|
$rewrite = array();
|
|
|
|
foreach($fields as $name => $specOrName) {
|
|
|
|
$identifer = (is_int($name)) ? $specOrName : $name;
|
2008-09-30 02:20:30 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
if(is_int($name)) {
|
|
|
|
// Format: array('MyFieldName')
|
|
|
|
$rewrite[$identifer] = array();
|
|
|
|
} elseif(is_array($specOrName)) {
|
|
|
|
// Format: array('MyFieldName' => array(
|
|
|
|
// 'filter => 'ExactMatchFilter',
|
|
|
|
// 'field' => 'NumericField', // optional
|
2015-12-23 09:32:26 +01:00
|
|
|
// 'title' => 'My Title', // optional
|
2008-09-30 02:20:30 +02:00
|
|
|
// ))
|
2008-08-11 01:29:30 +02:00
|
|
|
$rewrite[$identifer] = array_merge(
|
|
|
|
array('filter' => $this->relObject($identifer)->stat('default_search_filter_class')),
|
|
|
|
(array)$specOrName
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Format: array('MyFieldName' => 'ExactMatchFilter')
|
|
|
|
$rewrite[$identifer] = array(
|
|
|
|
'filter' => $specOrName,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if(!isset($rewrite[$identifer]['title'])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$rewrite[$identifer]['title'] = (isset($labels[$identifer]))
|
|
|
|
? $labels[$identifer] : FormField::name_to_label($identifer);
|
2008-08-11 01:29:30 +02:00
|
|
|
}
|
|
|
|
if(!isset($rewrite[$identifer]['filter'])) {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2008-08-11 01:29:30 +02:00
|
|
|
$rewrite[$identifer]['filter'] = 'PartialMatchFilter';
|
2008-08-09 08:53:26 +02:00
|
|
|
}
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2008-09-30 02:20:30 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
$fields = $rewrite;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// apply DataExtensions if present
|
2008-08-14 01:57:53 +02:00
|
|
|
$this->extend('updateSearchableFields', $fields);
|
2008-08-11 01:29:30 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-09 08:53:26 +02:00
|
|
|
/**
|
|
|
|
* Get any user defined searchable fields labels that
|
|
|
|
* exist. Allows overriding of default field names in the form
|
|
|
|
* interface actually presented to the user.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2008-08-09 08:53:26 +02:00
|
|
|
* The reason for keeping this separate from searchable_fields,
|
2008-08-11 01:17:51 +02:00
|
|
|
* which would be a logical place for this functionality, is to
|
2008-08-09 08:53:26 +02:00
|
|
|
* avoid bloating and complicating the configuration array. Currently
|
|
|
|
* much of this system is based on sensible defaults, and this property
|
|
|
|
* would generally only be set in the case of more complex relationships
|
|
|
|
* between data object being required in the search interface.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Generates labels based on name of the field itself, if no static property
|
2008-10-03 20:38:52 +02:00
|
|
|
* {@link self::field_labels} exists.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2008-10-03 20:38:52 +02:00
|
|
|
* @uses $field_labels
|
|
|
|
* @uses FormField::name_to_label()
|
2009-04-29 02:07:39 +02:00
|
|
|
*
|
|
|
|
* @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-04-23 02:11:41 +02:00
|
|
|
* @return array|string Array of all element labels if no argument given, otherwise the label of the field
|
2008-08-09 08:53:26 +02:00
|
|
|
*/
|
2009-04-29 02:07:39 +02:00
|
|
|
public function fieldLabels($includerelations = true) {
|
2012-04-13 18:38:23 +02:00
|
|
|
$cacheKey = $this->class . '_' . $includerelations;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-04-13 18:38:23 +02:00
|
|
|
if(!isset(self::$_cache_field_labels[$cacheKey])) {
|
|
|
|
$customLabels = $this->stat('field_labels');
|
|
|
|
$autoLabels = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-04-13 18:38:23 +02:00
|
|
|
// get all translated static properties as defined in i18nCollectStatics()
|
|
|
|
$ancestry = ClassInfo::ancestry($this->class);
|
|
|
|
$ancestry = array_reverse($ancestry);
|
|
|
|
if($ancestry) foreach($ancestry as $ancestorClass) {
|
2016-10-06 06:31:38 +02:00
|
|
|
if($ancestorClass === ViewableData::class) {
|
|
|
|
break;
|
|
|
|
}
|
2012-04-13 18:38:23 +02:00
|
|
|
$types = array(
|
|
|
|
'db' => (array)Config::inst()->get($ancestorClass, 'db', Config::UNINHERITED)
|
|
|
|
);
|
|
|
|
if($includerelations){
|
2015-12-05 08:53:29 +01:00
|
|
|
$types['has_one'] = (array)Config::inst()->get($ancestorClass, 'has_one', Config::UNINHERITED);
|
|
|
|
$types['has_many'] = (array)Config::inst()->get($ancestorClass, 'has_many', Config::UNINHERITED);
|
|
|
|
$types['many_many'] = (array)Config::inst()->get($ancestorClass, 'many_many', Config::UNINHERITED);
|
|
|
|
$types['belongs_many_many'] = (array)Config::inst()->get($ancestorClass, 'belongs_many_many', Config::UNINHERITED);
|
2012-04-13 18:38:23 +02:00
|
|
|
}
|
|
|
|
foreach($types as $type => $attrs) {
|
|
|
|
foreach($attrs as $name => $spec) {
|
|
|
|
$autoLabels[$name] = _t("{$ancestorClass}.{$type}_{$name}",FormField::name_to_label($name));
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-28 04:03:16 +01:00
|
|
|
|
2012-04-13 18:38:23 +02:00
|
|
|
$labels = array_merge((array)$autoLabels, (array)$customLabels);
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->extend('updateFieldLabels', $labels);
|
2012-04-13 18:38:23 +02:00
|
|
|
self::$_cache_field_labels[$cacheKey] = $labels;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-04-13 18:38:23 +02:00
|
|
|
return self::$_cache_field_labels[$cacheKey];
|
2008-10-03 20:38:52 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-03 20:38:52 +02:00
|
|
|
/**
|
|
|
|
* Get a human-readable label for a single field,
|
|
|
|
* see {@link fieldLabels()} for more details.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-03 20:38:52 +02:00
|
|
|
* @uses fieldLabels()
|
|
|
|
* @uses FormField::name_to_label()
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-03 20:38:52 +02:00
|
|
|
* @param string $name Name of the field
|
|
|
|
* @return string Label of the field
|
|
|
|
*/
|
|
|
|
public function fieldLabel($name) {
|
|
|
|
$labels = $this->fieldLabels();
|
|
|
|
return (isset($labels[$name])) ? $labels[$name] : FormField::name_to_label($name);
|
2008-08-09 08:53:26 +02:00
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Get the default summary fields for this object.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-09 06:06:52 +02:00
|
|
|
* @todo use the translation apparatus to return a default field selection for the language
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-09 06:06:52 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-08-14 03:50:23 +02:00
|
|
|
public function summaryFields() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$fields = $this->stat('summary_fields');
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-08-09 08:29:50 +02:00
|
|
|
// if fields were passed in numeric array,
|
|
|
|
// convert to an associative array
|
|
|
|
if($fields && array_key_exists(0, $fields)) {
|
|
|
|
$fields = array_combine(array_values($fields), array_values($fields));
|
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
if (!$fields) {
|
|
|
|
$fields = array();
|
2008-08-09 08:29:50 +02:00
|
|
|
// try to scaffold a couple of usual suspects
|
2016-10-06 06:31:38 +02:00
|
|
|
if ($this->hasField('Name')) {
|
|
|
|
$fields['Name'] = 'Name';
|
|
|
|
}
|
|
|
|
if (static::getSchema()->fieldSpec($this, 'Title')) {
|
|
|
|
$fields['Title'] = 'Title';
|
|
|
|
}
|
|
|
|
if ($this->hasField('Description')) {
|
|
|
|
$fields['Description'] = 'Description';
|
|
|
|
}
|
|
|
|
if ($this->hasField('FirstName')) {
|
|
|
|
$fields['FirstName'] = 'First Name';
|
|
|
|
}
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2008-08-26 03:45:52 +02:00
|
|
|
$this->extend("updateSummaryFields", $fields);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-03 18:01:51 +02:00
|
|
|
// Final fail-over, just list ID field
|
|
|
|
if(!$fields) $fields['ID'] = 'ID';
|
2012-08-10 00:14:02 +02:00
|
|
|
|
|
|
|
// Localize fields (if possible)
|
2012-08-29 13:42:29 +02:00
|
|
|
foreach($this->fieldLabels(false) as $name => $label) {
|
2014-08-14 03:50:23 +02:00
|
|
|
// only attempt to localize if the label definition is the same as the field name.
|
|
|
|
// this will preserve any custom labels set in the summary_fields configuration
|
|
|
|
if(isset($fields[$name]) && $name === $fields[$name]) {
|
|
|
|
$fields[$name] = $label;
|
|
|
|
}
|
2012-08-29 13:42:29 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Defines a default list of filters for the search context.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-09 06:06:52 +02:00
|
|
|
* If a filter class mapping is defined on the data object,
|
|
|
|
* it is constructed here. Otherwise, the default filter specified in
|
|
|
|
* {@link DBField} is used.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-09 06:06:52 +02:00
|
|
|
* @todo error handling/type checking for valid FormField and SearchFilter subclasses?
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-09 06:06:52 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function defaultSearchFilters() {
|
|
|
|
$filters = array();
|
2012-07-22 18:11:35 +02:00
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
foreach($this->searchableFields() as $name => $spec) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if (empty($spec['filter'])) {
|
|
|
|
/** @skipUpgrade */
|
|
|
|
$filters[$name] = 'PartialMatchFilter';
|
|
|
|
} elseif($spec['filter'] instanceof SearchFilter) {
|
2012-07-22 18:11:35 +02:00
|
|
|
$filters[$name] = $spec['filter'];
|
|
|
|
} else {
|
2016-08-19 00:51:35 +02:00
|
|
|
$filters[$name] = Injector::inst()->create($spec['filter'], $name);
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2012-07-22 18:11:35 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
return $filters;
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2007-08-16 08:32:49 +02:00
|
|
|
/**
|
2008-08-09 07:04:15 +02:00
|
|
|
* @return boolean True if the object is in the database
|
|
|
|
*/
|
|
|
|
public function isInDB() {
|
|
|
|
return is_numeric( $this->ID ) && $this->ID > 0;
|
|
|
|
}
|
|
|
|
|
2008-08-12 04:51:33 +02:00
|
|
|
/*
|
|
|
|
* @ignore
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
private static $subclass_access = true;
|
|
|
|
|
2008-08-12 04:51:33 +02:00
|
|
|
/**
|
|
|
|
* Temporarily disable subclass access in data object qeur
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function disable_subclass_access() {
|
2008-08-12 04:51:33 +02:00
|
|
|
self::$subclass_access = false;
|
|
|
|
}
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function enable_subclass_access() {
|
2008-08-12 04:51:33 +02:00
|
|
|
self::$subclass_access = true;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
//-------------------------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database field definitions.
|
|
|
|
* This is a map from field names to field type. The field
|
2007-08-16 08:32:49 +02:00
|
|
|
* type should be a class that extends .
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2011-12-22 03:31:56 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = null;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Use a casting object for a field. This is a map from
|
|
|
|
* field name to class name of the casting object.
|
2016-02-25 05:32:41 +01:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2008-08-11 04:25:44 +02:00
|
|
|
"Title" => 'Text',
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-05-19 05:55:14 +02:00
|
|
|
/**
|
|
|
|
* Specify custom options for a CREATE TABLE call.
|
|
|
|
* Can be used to specify a custom storage engine for specific database table.
|
|
|
|
* All options have to be keyed for a specific database implementation,
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* identified by their class name (extending from {@link SS_Database}).
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-05-19 05:55:14 +02:00
|
|
|
* <code>
|
|
|
|
* array(
|
2012-12-08 12:20:20 +01:00
|
|
|
* 'MySQLDatabase' => 'ENGINE=MyISAM'
|
2009-05-19 05:55:14 +02:00
|
|
|
* )
|
|
|
|
* </code>
|
2009-05-19 06:44:33 +02:00
|
|
|
*
|
|
|
|
* Caution: This API is experimental, and might not be
|
|
|
|
* included in the next major release. Please use with care.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-05-19 05:55:14 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2009-05-19 05:55:14 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $create_table_options = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
'SilverStripe\ORM\Connect\MySQLDatabase' => 'ENGINE=InnoDB'
|
2009-05-19 05:55:14 +02:00
|
|
|
);
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* If a field is in this array, then create a database index
|
|
|
|
* on that field. This is a map from fieldname to index type.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $indexes = null;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Inserts standard column-values when a DataObject
|
|
|
|
* is instanciated. Does not insert default records {@see $default_records}.
|
2009-02-02 00:49:53 +01:00
|
|
|
* This is a map from fieldname to default value.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-09-23 02:17:58 +02:00
|
|
|
* - If you would like to change a default value in a sub-class, just specify it.
|
2012-09-26 23:34:00 +02:00
|
|
|
* - If you would like to disable the default value given by a parent class, set the default value to 0,'',
|
|
|
|
* or false in your subclass. Setting it to null won't work.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $defaults = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2007-09-15 01:10:25 +02:00
|
|
|
* Multidimensional array which inserts default data into the database
|
2007-07-19 12:40:28 +02:00
|
|
|
* on a db/build-call as long as the database-table is empty. Please use this only
|
|
|
|
* for simple constructs, not for SiteTree-Objects etc. which need special
|
|
|
|
* behaviour such as publishing and ParentNodes.
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Example:
|
|
|
|
* array(
|
2012-12-08 12:20:20 +01:00
|
|
|
* array('Title' => "DefaultPage1", 'PageTitle' => 'page1'),
|
|
|
|
* array('Title' => "DefaultPage2")
|
2007-07-19 12:40:28 +02:00
|
|
|
* ).
|
2007-09-15 01:10:25 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_records = null;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-24 01:27:51 +02:00
|
|
|
* One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a
|
|
|
|
* true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class.
|
|
|
|
*
|
|
|
|
* Note that you cannot have a has_one and belongs_to relationship with the same name.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = null;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-24 01:27:51 +02:00
|
|
|
/**
|
|
|
|
* A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}.
|
|
|
|
*
|
|
|
|
* This does not actually create any data structures, but allows you to query the other object in a one-to-one
|
|
|
|
* relationship from the child object. If you have multiple belongs_to links to another object you can use the
|
|
|
|
* syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use.
|
|
|
|
*
|
|
|
|
* Note that you cannot have a has_one and belongs_to relationship with the same name.
|
|
|
|
*
|
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2009-10-24 01:27:51 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_to;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-24 01:26:10 +02:00
|
|
|
* This defines a one-to-many relationship. It is a map of component name to the remote data class.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2009-10-24 01:26:10 +02:00
|
|
|
* This relationship type does not actually create a data structure itself - you need to define a matching $has_one
|
|
|
|
* relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this
|
|
|
|
* class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show
|
|
|
|
* which foreign key to use.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* many-many relationship definitions.
|
|
|
|
* This is a map from component name to data type.
|
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = null;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Extra fields to include on the connecting many-many table.
|
|
|
|
* This is a map from field name to field type.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-02-03 22:07:32 +01:00
|
|
|
* Example code:
|
|
|
|
* <code>
|
|
|
|
* public static $many_many_extraFields = array(
|
2012-12-08 12:20:20 +01:00
|
|
|
* 'Members' => array(
|
2009-02-03 22:07:32 +01:00
|
|
|
* 'Role' => 'Varchar(100)'
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many_extraFields = null;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The inverse side of a many-many relationship.
|
|
|
|
* This is a map from component name to data type.
|
|
|
|
* @var array
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = null;
|
2007-09-15 01:10:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The default sort expression. This will be inserted in the ORDER BY
|
|
|
|
* clause of a SQL query if no other sort expression is provided.
|
|
|
|
* @var string
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_sort = null;
|
2008-08-09 07:04:15 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Default list of fields that can be scaffolded by the ModelAdmin
|
|
|
|
* search interface.
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* Overriding the default filter, with a custom defined filter:
|
2008-08-09 06:06:52 +02:00
|
|
|
* <code>
|
2012-12-08 12:20:20 +01:00
|
|
|
* static $searchable_fields = array(
|
|
|
|
* "Name" => "PartialMatchFilter"
|
2008-08-09 06:06:52 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* Overriding the default form fields, with a custom defined field.
|
|
|
|
* The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}.
|
|
|
|
* The 'title' parameter will be generated from {@link DataObject->fieldLabels()}.
|
2008-08-09 06:06:52 +02:00
|
|
|
* <code>
|
2012-12-08 12:20:20 +01:00
|
|
|
* static $searchable_fields = array(
|
|
|
|
* "Name" => array(
|
|
|
|
* "field" => "TextField"
|
|
|
|
* )
|
2008-08-09 06:06:52 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
2008-08-09 07:04:15 +02:00
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* Overriding the default form field, filter and title:
|
2008-08-09 06:06:52 +02:00
|
|
|
* <code>
|
2012-12-08 12:20:20 +01:00
|
|
|
* static $searchable_fields = array(
|
|
|
|
* "Organisation.ZipCode" => array(
|
2013-06-21 00:32:08 +02:00
|
|
|
* "field" => "TextField",
|
2012-12-08 12:20:20 +01:00
|
|
|
* "filter" => "PartialMatchFilter",
|
|
|
|
* "title" => 'Organisation ZIP'
|
|
|
|
* )
|
2008-08-09 06:06:52 +02:00
|
|
|
* );
|
2008-08-09 07:04:15 +02:00
|
|
|
* </code>
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-09 06:06:52 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $searchable_fields = null;
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-09 08:53:26 +02:00
|
|
|
/**
|
|
|
|
* User defined labels for searchable_fields, used to override
|
|
|
|
* default display in the search form.
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-09 08:53:26 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $field_labels = null;
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
/**
|
|
|
|
* Provides a default list of fields to be used by a 'summary'
|
|
|
|
* view of this object.
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-09 06:06:52 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $summary_fields = null;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-29 22:07:17 +01:00
|
|
|
/**
|
|
|
|
* Collect all static properties on the object
|
|
|
|
* which contain natural language, and need to be translated.
|
|
|
|
* The full entity name is composed from the class name and a custom identifier.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2008-10-29 22:07:17 +01:00
|
|
|
* @return array A numerical array which contains one or more entities in array-form.
|
|
|
|
* Each numeric entity array contains the "arguments" for a _t() call as array values:
|
|
|
|
* $entity, $string, $priority, $context.
|
|
|
|
*/
|
|
|
|
public function provideI18nEntities() {
|
|
|
|
$entities = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-29 22:07:17 +01:00
|
|
|
$entities["{$this->class}.SINGULARNAME"] = array(
|
2008-11-01 14:23:38 +01:00
|
|
|
$this->singular_name(),
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-29 22:07:17 +01:00
|
|
|
'Singular name of the object, used in dropdowns and to generally identify a single object in the interface'
|
|
|
|
);
|
2008-11-01 14:23:38 +01:00
|
|
|
|
2008-10-29 22:07:17 +01:00
|
|
|
$entities["{$this->class}.PLURALNAME"] = array(
|
2008-11-01 14:23:38 +01:00
|
|
|
$this->plural_name(),
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
'Pural name of the object, used in dropdowns and to generally identify a collection of this object in the'
|
|
|
|
. ' interface'
|
2008-10-29 22:07:17 +01:00
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-10-29 22:07:17 +01:00
|
|
|
return $entities;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
2012-12-08 12:20:20 +01:00
|
|
|
* Returns true if the given method/parameter has a value
|
|
|
|
* (Uses the DBField::hasValue if the parameter is a database field)
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-04 06:21:06 +02:00
|
|
|
* @param string $field The field name
|
|
|
|
* @param array $arguments
|
|
|
|
* @param bool $cache
|
2012-12-08 12:20:20 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasValue($field, $arguments = null, $cache = true) {
|
2014-03-27 21:37:04 +01:00
|
|
|
// has_one fields should not use dbObject to check if a value is given
|
2016-10-06 06:31:38 +02:00
|
|
|
$hasOne = static::getSchema()->hasOneComponent(static::class, $field);
|
|
|
|
if(!$hasOne && ($obj = $this->dbObject($field))) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $obj->exists();
|
|
|
|
} else {
|
|
|
|
return parent::hasValue($field, $arguments, $cache);
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:40:57 +02:00
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
/**
|
|
|
|
* If selected through a many_many through relation, this is the instance of the joined record
|
|
|
|
*
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function getJoin() {
|
|
|
|
return $this->joinRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set joining object
|
|
|
|
*
|
|
|
|
* @param DataObject $object
|
2016-10-03 03:51:34 +02:00
|
|
|
* @param string $alias Alias
|
2016-09-26 07:22:19 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
2016-10-03 03:51:34 +02:00
|
|
|
public function setJoin(DataObject $object, $alias = null) {
|
2016-09-26 07:22:19 +02:00
|
|
|
$this->joinRecord = $object;
|
2016-10-03 03:51:34 +02:00
|
|
|
if ($alias) {
|
2016-10-06 06:31:38 +02:00
|
|
|
if (static::getSchema()->fieldSpec(static::class, $alias)) {
|
2016-10-03 03:51:34 +02:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"Joined record $alias cannot also be a db field"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$this->record[$alias] = $object;
|
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|