public function getValidators() { $validators = parent::getValidators(); if ($this->validation === null) { $validators[] = new Validator\Enum(); } return $validators; }
/** * Returns a column type according to ScalarField object. * * @param Entity\ScalarField $field Type "source". * * @return string */ public function getColumnTypeByField(Entity\ScalarField $field) { if ($field instanceof Entity\IntegerField) { return 'number(18)'; } elseif ($field instanceof Entity\FloatField) { $scale = $field->getScale(); return 'number' . ($scale !== null ? "(*," . $scale . ")" : ""); } elseif ($field instanceof Entity\DatetimeField) { return 'date'; } elseif ($field instanceof Entity\DateField) { return 'date'; } elseif ($field instanceof Entity\TextField) { return 'clob'; } elseif ($field instanceof Entity\BooleanField) { $values = $field->getValues(); if (preg_match('/^[0-9]+$/', $values[0]) && preg_match('/^[0-9]+$/', $values[1])) { return 'number(1)'; } else { return 'varchar2(' . max(strlen($values[0]), strlen($values[1])) . ' char)'; } } elseif ($field instanceof Entity\EnumField) { return 'varchar2(' . max(array_map('strlen', $field->getValues())) . ' char)'; } else { // string by default $defaultLength = false; foreach ($field->getValidators() as $validator) { if ($validator instanceof Entity\Validator\Length) { if ($defaultLength === false || $defaultLength > $validator->getMax()) { $defaultLength = $validator->getMax(); } } } return 'varchar2(' . ($defaultLength > 0 ? $defaultLength : 255) . ' char)'; } }
/** * Converts values to the string according to the column type to use it in a SQL query. * * @param mixed $value Value to be converted. * @param Entity\ScalarField $field Type "source". * * @return string Value to write to column. * @throws \Bitrix\Main\ArgumentTypeException */ public function convertToDb($value, Entity\ScalarField $field) { if ($value === null) { return "NULL"; } if ($value instanceof SqlExpression) { return $value->compile(); } if ($field instanceof Entity\DatetimeField) { if (empty($value)) { $result = "NULL"; } elseif ($value instanceof Type\Date) { if ($value instanceof Type\DateTime) { $value = clone $value; $value->setDefaultTimeZone(); } $result = $this->getCharToDateFunction($value->format("Y-m-d H:i:s")); } else { throw new Main\ArgumentTypeException('value', '\\Bitrix\\Main\\Type\\Date'); } } elseif ($field instanceof Entity\DateField) { if (empty($value)) { $result = "NULL"; } elseif ($value instanceof Type\Date) { $result = $this->getCharToDateFunction($value->format("Y-m-d")); } else { throw new Main\ArgumentTypeException('value', '\\Bitrix\\Main\\Type\\Date'); } } elseif ($field instanceof Entity\IntegerField) { $result = "'" . intval($value) . "'"; } elseif ($field instanceof Entity\FloatField) { if (($scale = $field->getScale()) !== null) { $result = "'" . round(doubleval($value), $scale) . "'"; } else { $result = "'" . doubleval($value) . "'"; } } elseif ($field instanceof Entity\StringField) { $result = "'" . $this->forSql($value, $field->getSize()) . "'"; } else { $result = "'" . $this->forSql($value) . "'"; } return $result; }
/** * Returns a column type according to ScalarField object. * * @param Entity\ScalarField $field Type "source". * * @return string */ public static function getColumnTypeByField(Entity\ScalarField $field) { if ($field instanceof Entity\IntegerField) { return 'int'; } elseif ($field instanceof Entity\FloatField) { return 'float'; } elseif ($field instanceof Entity\DatetimeField) { return 'datetime'; } elseif ($field instanceof Entity\DateField) { return 'date'; } elseif ($field instanceof Entity\TextField) { return 'text'; } elseif ($field instanceof Entity\BooleanField) { $values = $field->getValues(); if (preg_match('/^[0-9]+$/', $values[0]) && preg_match('/^[0-9]+$/', $values[1])) { return 'int'; } else { return 'varchar(' . max(strlen($values[0]), strlen($values[1])) . ')'; } } elseif ($field instanceof Entity\EnumField) { return 'varchar(' . max(array_map('strlen', $field->getValues())) . ')'; } else { // string by default $defaultLength = false; foreach ($field->getValidators() as $validator) { if ($validator instanceof Entity\Validator\Length) { if ($defaultLength === false || $defaultLength > $validator->getMax()) { $defaultLength = $validator->getMax(); } } } return 'varchar(' . ($defaultLength > 0 ? $defaultLength : 255) . ')'; } }
function getEntityReferences($userfield, \Bitrix\Main\Entity\ScalarField $entityField) { if ($userfield['SETTINGS']['HLBLOCK_ID']) { $hlblock = \Bitrix\Highloadblock\HighloadBlockTable::getById($userfield['SETTINGS']['HLBLOCK_ID'])->fetch(); if ($hlblock) { $hlentity = \Bitrix\Highloadblock\HighloadBlockTable::compileEntity($hlblock); return array(new \Bitrix\Main\Entity\ReferenceField($entityField->getName() . '_REF', $hlentity, array('=this.' . $entityField->getName() => 'ref.ID'))); } } return array(); }
/** * @deprecated * @return null|string */ public function getDataType() { return $this->valueField->getDataType(); }