/** * Load the flex-fields into the model */ public function afterLoadModel(One_Scheme $scheme, One_Model $model) { $ignoreAttributes = array_merge(self::$_ignoreAttributes, $scheme->getForeignKeys()); // ignore all local foreign key attributes as well $bOptions = $scheme->get('behaviorOptions.' . strtolower($this->getName())); $flexfield = $bOptions['flexfield']; $json = json_decode($model->{$flexfield}, true); if (is_array($json)) { foreach ($json as $key => $value) { //echo $key; if (!in_array($key, $ignoreAttributes) && null === $scheme->getAttribute($key)) { $model->{$key} = $value; } } } unset($model->{$flexfield}); }
/** * When the model is loaded, add a slug-field to the model that is composed of the specified fields * * @param One_Scheme $scheme * @param One_Model $model */ public function afterLoadModel(One_Scheme $scheme, One_Model $model) { if (null !== $scheme->getAttribute('slug')) { // don't create the slug if the attribute "slug" actually exists return; } $options = $scheme->get('behaviorOptions.slug'); $createFrom = $options['createFrom']; $parts = preg_split('/\\+/', $createFrom); $mangled = array(); foreach ($parts as $part) { if (preg_match('/^([a-z0-9\\_\\-]+):([a-z0-9\\_\\-]+)$/', $part, $matches) > 0) { $scheme = $model->getScheme(); $link = $scheme->getLink($matches[1]); if (!is_null($link)) { if ($link->getAdapterType() == 'manytoone') { $related = $model->getRelated($matches[1]); if (!is_null($related)) { $targetPart = $matches[2]; $mangle = !is_null($related->{$targetPart}) ? trim($this->mangle($related->{$targetPart})) : NULL; if (!is_null($mangle)) { $mangled[] = $mangle; } } } } } else { $mangle = !is_null($model->{$part}) ? trim($this->mangle($model->{$part})) : NULL; if (!is_null($mangle)) { $mangled[] = $mangle; } } } if (count($mangled) > 0) { $model->slug = implode('_', $mangled); } }