Version: current

     1 <?php
     2 /**
     3  * A special type Int field used for foreign keys in has_one relationships.
     4  * @uses ImageField
     5  * @uses SimpleImageField
     6  * @uses FileIFrameField
     7  * @uses DropdownField
     8  *
     9  * @param string $name
    10  * @param DataOject $object The object that the foreign key is stored on (should have a relation with $name)
    11  *
    12  * @package sapphire
    13  * @subpackage model
    14  */
    15 class ForeignKey extends Int {
    16
    17     /**
    18      * @var DataObject
    19      */
    20     protected $object;
    21
    22     public static $default_search_filter_class = 'ExactMatchMultiFilter';
    23
    24     function __construct($name, $object = null) {
    25         $this->object = $object;
    26         parent::__construct($name);
    27     }
    28
    29     public function scaffoldFormField($title = null, $params = null) {
    30         $relationName = substr($this->name,0,-2);
    31         $hasOneClass = $this->object->has_one($relationName);
    32
    33         if($hasOneClass && singleton($hasOneClass) instanceof Image) {
    34             if(isset($params['ajaxSafe']) && $params['ajaxSafe']) {
    35                 $field = new ImageField($relationName, $title, $this->value);
    36             } else {
    37                 $field = new SimpleImageField($relationName, $title, $this->value);
    38             }
    39         } elseif($hasOneClass && singleton($hasOneClass) instanceof File) {
    40             if(isset($params['ajaxSafe']) && $params['ajaxSafe']) {
    41                 $field = new FileIFrameField($relationName, $title, $this->value);
    42             } else {
    43                 $field = new FileField($relationName, $title, $this->value);
    44             }
    45         } else {
    46             $titleField = (singleton($hasOneClass)->hasField('Title')) ? "Title" : "Name";
    47             $map = new SQLMap(singleton($hasOneClass)->extendedSQL(), "ID", $titleField);
    48             $field =  new DropdownField($this->name, $title, $map, null, null, ' ');
    49         }
    50
    51         return $field;
    52     }
    53 }
    54
    55 ?>
    56

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -