2013-07-10 02:44:24 +02:00
|
|
|
<?php
|
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
namespace SilverStripe\Model\FieldType;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use DataObject;
|
|
|
|
use ClassInfo;
|
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* A special ForeignKey class that handles relations with arbitrary class types
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-07-10 02:44:24 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBPolymorphicForeignKey extends DBComposite {
|
2013-07-10 02:44:24 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
private static $composite_db = array(
|
|
|
|
'ID' => 'Int',
|
|
|
|
'Class' => 'DBClassName("DataObject")'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
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
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-07-10 02:44:24 +02:00
|
|
|
* @return string Name of a subclass of DataObject
|
|
|
|
*/
|
|
|
|
public function getClassValue() {
|
2015-09-04 05:49:22 +02:00
|
|
|
return $this->getField('Class');
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* Set the value of the "Class" this key points to
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-04 05:49:22 +02:00
|
|
|
* @param string $value Name of a subclass of DataObject
|
2013-07-10 02:44:24 +02:00
|
|
|
* @param boolean $markChanged Mark this field as changed?
|
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setClassValue($value, $markChanged = true) {
|
|
|
|
$this->setField('Class', $value, $markChanged);
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* Gets the value of the "ID" this key points to
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-07-10 02:44:24 +02:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getIDValue() {
|
2015-09-04 05:49:22 +02:00
|
|
|
return $this->getField('ID');
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* Sets the value of the "ID" this key points to
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-04 05:49:22 +02:00
|
|
|
* @param integer $value
|
2013-07-10 02:44:24 +02:00
|
|
|
* @param boolean $markChanged Mark this field as changed?
|
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setIDValue($value, $markChanged = true) {
|
|
|
|
$this->setField('ID', $value, $markChanged);
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setValue($value, $record = null, $markChanged = true) {
|
2015-09-04 05:49:22 +02:00
|
|
|
// Map dataobject value to array
|
2013-07-10 02:44:24 +02:00
|
|
|
if($value instanceof DataObject) {
|
2015-09-04 05:49:22 +02:00
|
|
|
$value = array(
|
|
|
|
'ID' => $value->ID,
|
|
|
|
'Class' => $value->class
|
2013-07-10 02:44:24 +02:00
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
parent::setValue($value, $record, $markChanged);
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
public function getValue() {
|
2015-09-04 05:49:22 +02:00
|
|
|
$id = $this->getIDValue();
|
|
|
|
$class = $this->getClassValue();
|
|
|
|
if($id && $class && is_subclass_of($class, 'DataObject')) {
|
|
|
|
return DataObject::get_by_id($class, $id);
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|