/** * Get the name of the id field. * * @return string Name of id field. */ protected function getId() { $class = Utilities::getClassName($this); if (preg_match('/^(.+)MetaSchema$/', $class, $matches) !== 1) { throw new InvalidMixinException('Invalid meta class name format.'); } return lcfirst($matches[1]) . 'Id'; }
/** * Construct active model. * * @param Schema $schema * Schema. * @throws InvalidActiveModelException If model is incorrectly defined. * @throws InvalidTableException If table not found. * @throws InvalidAssociationException If association models are invalid. * @throws InvalidMixinException If a mixin is invalid. */ public final function __construct(Schema $schema) { $this->name = Utilities::getClassName(get_class($this)); $this->schema = $schema; if (!isset($this->table)) { $this->table = $this->name; } $table = $this->table; if (!isset($this->schema->{$table})) { throw new InvalidTableException('Table "' . $table . '" not found in schema'); } $this->source = $this->schema->{$table}; $this->definition = $this->source->getDefinition(); if (!isset($this->definition)) { throw new InvalidTableException('Definition for table "' . $table . '" not found'); } $pk = $this->definition->getPrimaryKey(); if (count($pk) == 1) { $pk = $pk[0]; $this->primaryKey = $pk; $type = $this->definition->{$pk}; if ($type->isInteger() and $type->autoIncrement) { $this->aiPrimaryKey = $pk; } } else { throw new InvalidActiveModelException('ActiveModel does not support multi-field primary keys'); } $this->nonVirtualFields = $this->definition->getFields(); $this->fields = $this->nonVirtualFields; foreach ($this->virtual as $field) { $this->fields[] = $field; $this->virtualFields[] = $field; } $this->validator = new ValidatorBuilder($this, $this->validate); $this->definition->createValidationRules($this->validator); foreach ($this->nonVirtualFields as $field) { $type = $this->definition->{$field}; if (isset($type->default)) { $this->defaults[$field] = $type->default; } } if (isset($this->record)) { Utilities::assumeSubclassOf($this->record, 'Jivoo\\Data\\ActiveModel\\ActiveRecord'); } $this->attachEventListener($this); foreach ($this->mixins as $mixin => $options) { if (!is_string($mixin)) { $mixin = $options; $options = array(); } if (class_exists('Jivoo\\Data\\ActiveModel\\' . $mixin . 'Mixin')) { $mixin = 'Jivoo\\Data\\ActiveModel\\' . $mixin . 'Mixin'; } elseif (class_exists($mixin . 'Mixin')) { $mixin .= 'Mixin'; } elseif (!class_exists($mixin)) { throw new InvalidMixinException('Mixin class not found: ' . $mixin); } Assume::isSubclassOf($mixin, 'Jivoo\\Data\\ActiveModel\\ActiveModelMixin'); $mixin = new $mixin($this->app, $this, $options); $this->attachEventListener($mixin); $this->mixinObjects[] = $mixin; foreach ($mixin->getMethods() as $method) { $this->methods[$method] = array($mixin, $method); } } $this->init(); }