silverstripe-framework/model/FieldType/DBPolymorphicForeignKey.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

87 lines
2.0 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use DB;
use DataObject;
use ClassInfo;
/**
* A special ForeignKey class that handles relations with arbitrary class types
*
* @package framework
* @subpackage model
*/
class DBPolymorphicForeignKey extends DBComposite {
private static $composite_db = array(
'ID' => 'Int',
'Class' => 'DBClassName("DataObject")'
);
public function scaffoldFormField($title = null, $params = null) {
// Opt-out of form field generation - Scaffolding should be performed on
// the has_many end, or set programatically.
// @todo - Investigate suitable FormField
return null;
}
/**
* Get the value of the "Class" this key points to
*
* @return string Name of a subclass of DataObject
*/
public function getClassValue() {
return $this->getField('Class');
}
/**
* Set the value of the "Class" this key points to
*
* @param string $value Name of a subclass of DataObject
* @param boolean $markChanged Mark this field as changed?
*/
public function setClassValue($value, $markChanged = true) {
$this->setField('Class', $value, $markChanged);
}
/**
* Gets the value of the "ID" this key points to
*
* @return integer
*/
public function getIDValue() {
return $this->getField('ID');
}
/**
* Sets the value of the "ID" this key points to
*
* @param integer $value
* @param boolean $markChanged Mark this field as changed?
*/
public function setIDValue($value, $markChanged = true) {
$this->setField('ID', $value, $markChanged);
}
public function setValue($value, $record = null, $markChanged = true) {
// Map dataobject value to array
if($value instanceof DataObject) {
$value = array(
'ID' => $value->ID,
'Class' => $value->class
);
}
parent::setValue($value, $record, $markChanged);
}
public function getValue() {
$id = $this->getIDValue();
$class = $this->getClassValue();
if($id && $class && is_subclass_of($class, 'DataObject')) {
return DataObject::get_by_id($class, $id);
}
}
}