/** * Add full text search field indexes. * @param null $class * @param null $extension * @return array */ public function extraStatics($class = null, $extension = null) { // just get the enabled ones, field names are keys, if value is false then skip it $fulltextFields = array_keys(array_filter(static::config()->get('fulltext_fields') ?: [])); $statics = parent::extraStatics($class, $extension) ?: []; if ($fulltextFields) { $statics = array_merge($statics, ['create_table_options' => ['MySQLDatabase' => 'ENGINE=MyISAM'], 'indexes' => [static::SearchIndex => ['type' => 'fulltext', 'name' => static::SearchIndex, 'value' => implode(',', $fulltextFields)]]]); } return $statics; }
public function onBeforeWrite() { parent::onBeforeWrite(); if ($this->fieldName) { $fieldName = $this->fieldName; if ($value = $this()->{$fieldName}) { if (\DataObject::get($this()->ClassName)->filter($fieldName, $value)->count()) { throw new \ValidationException("One or more records with {$this->fieldName} '{$value}' already exist and it should be unique"); } } } }
/** * If static.SingleFieldName && static.SingleFieldSchema are set add them to db array. * * @param null $class * @param null $extension * @return mixed */ public function extraStatics($class = null, $extension = null) { return array_merge_recursive(parent::extraStatics($class, $extension) ?: [], static::SingleFieldName && static::SingleFieldSchema ? ['db' => [static::SingleFieldName => static::SingleFieldSchema]] : []); }