silverstripe-framework/model/fieldtypes/PolymorphicForeignKey.php
Damian Mooyman 9872fbef4d API Refactor CompositeDBField into an abstract class
API Refactor ClassName into DBClassName
API Update PolymorphicForeignKey to use new CompositeDBField and DBClassName

CompositeDBField is now an interface to nested fields on an underlying dataobject, allowing field manipulation to be performed at the field and dataobject level without having to synchronise them manually.
2015-09-22 10:28:07 +12:00

81 lines
1.9 KiB
PHP

<?php
/**
* A special ForeignKey class that handles relations with arbitrary class types
*
* @package framework
* @subpackage model
*/
class PolymorphicForeignKey extends CompositeDBField {
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);
}
}
}