silverstripe-framework/model/FieldType/DBForeignKey.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

79 lines
2.0 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use DataList;
use UploadField;
use DropdownField;
use NumericField;
use DataObject;
/**
* A special type Int field used for foreign keys in has_one relationships.
* @uses ImageField
* @uses SimpleImageField
* @uses FileIFrameField
* @uses DropdownField
*
* @param string $name
* @param DataObject $object The object that the foreign key is stored on (should have a relation with $name)
*
* @package framework
* @subpackage model
*/
class DBForeignKey extends DBInt {
/**
* @var DataObject
*/
protected $object;
private static $default_search_filter_class = 'ExactMatchFilter';
public function __construct($name, $object = null) {
$this->object = $object;
parent::__construct($name);
}
public function scaffoldFormField($title = null, $params = null) {
if(empty($this->object)) {
return null;
}
$relationName = substr($this->name,0,-2);
$hasOneClass = $this->object->hasOneComponent($relationName);
if(empty($hasOneClass)) {
return null;
}
$hasOneSingleton = singleton($hasOneClass);
if($hasOneSingleton instanceof File) {
$field = new UploadField($relationName, $title);
if($hasOneSingleton instanceof Image) {
$field->setAllowedFileCategories('image/supported');
}
return $field;
}
// Build selector / numeric field
$titleField = $hasOneSingleton->hasField('Title') ? "Title" : "Name";
$list = DataList::create($hasOneClass);
// Don't scaffold a dropdown for large tables, as making the list concrete
// might exceed the available PHP memory in creating too many DataObject instances
if($list->count() < 100) {
$field = new DropdownField($this->name, $title, $list->map('ID', $titleField));
$field->setEmptyString(' ');
} else {
$field = new NumericField($this->name, $title);
}
return $field;
}
public function setValue($value, $record = null, $markChanged = true) {
if($record instanceof DataObject) {
$this->object = $record;
}
parent::setValue($value, $record, $markChanged);
}
}