contributions/instancereference/model/base/fields/dbfield.instancereference.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * A field holding a reference to an instance 00004 */ 00005 class DBFieldInstanceReference extends DBFieldText { 00006 public function __construct($name, $policy = self::NOT_NULL) { 00007 parent::__construct($name, 255, null, $policy); 00008 } 00009 00010 /** 00011 * Returns true, if the value passed fits the fields restrictions 00012 * 00013 * @param mixed $value 00014 * @return Status 00015 */ 00016 public function validate($value) { 00017 $ret = new Status; 00018 if (is_null($value)) { 00019 $ret->merge(parent::validate($value)); 00020 } 00021 else if (!$value instanceof IDataObject) { 00022 $ret->append(tr( 00023 '%field must be a data object', 00024 'instancereference', 00025 array( 00026 '%field' => tr($this->get_field_name(), 'global'), 00027 ) 00028 )); 00029 } 00030 return $ret; 00031 } 00032 00033 /** 00034 * Reformat passed value to DB format 00035 * 00036 * @param mixed $value 00037 * @return string 00038 */ 00039 public function format($value) { 00040 if (is_null($value)) { 00041 return parent::format($value); 00042 } 00043 00044 $value_to_format = ''; 00045 if ($value instanceof IDataObject) { 00046 $value_to_format = InstanceReferenceSerializier::instance_to_string($value); 00047 } 00048 else if (is_string($value)) { 00049 $value_to_format = $value; 00050 } 00051 return parent::format($value_to_format); 00052 } 00053 00054 /** 00055 * Transform result from SELECT to native 00056 * 00057 * @param mixed $value 00058 * @return mixed 00059 */ 00060 public function convert_result($value) { 00061 $ret = InstanceReferenceSerializier::string_to_instance($value); 00062 return $ret; 00063 } 00064 }