mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-09 03:01:18 +02:00
81 lines
1.9 KiB
PHP
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);
|
|
}
|
|
}
|
|
}
|