public static function defineClass($className, $extends = '') { // $namespace = 'Eddmash\PowerOrm\Migration\Model'; $namespace = ''; $use = ''; $extendedClass = ''; if (empty($extends) || Model::isModelBase($extends)) { $extends = Model::getFullClassName(); } else { $extendedClass = sprintf('%s%s', ClassHelper::getFormatNamespace($namespace, true), $extends); $use = sprintf('use %s;', $extendedClass); $extends = trim(substr($extends, strripos($extends, '\\')), '\\'); } if (!StringHelper::isEmpty($extendedClass) && !ClassHelper::classExists($extendedClass, $namespace)) { self::$deferedClasses[$extends][] = ['class' => $className, 'extends' => $extends]; return false; } $extends = ClassHelper::getNameFromNs($extends, $namespace); $class = sprintf(self::getTemplate(), $namespace, $use, $className, $extends); $className = sprintf('%s%s', ClassHelper::getFormatNamespace($namespace, true), $className); if (ArrayHelper::hasKey(self::$deferedClasses, $className)) { foreach (self::$deferedClasses[$className] as $deferedClass) { self::defineClass($deferedClass['class'], $deferedClass['extends']); } } if (!ClassHelper::classExists($className, $namespace)) { eval($class); } return $className; }
public function getConstructorArgs() { $constructorArgs = parent::getConstructorArgs(); if (isset($constructorArgs['meta']) && empty($constructorArgs['meta'])) { unset($constructorArgs['meta']); } if (isset($constructorArgs['extends'])) { if (StringHelper::isEmpty($constructorArgs['extends']) || Model::isModelBase($constructorArgs['extends'])) { unset($constructorArgs['extends']); } else { $constructorArgs['extends'] = ClassHelper::getNameFromNs($constructorArgs['extends'], BaseOrm::getModelsNamespace()); } } return $constructorArgs; }
/** * Takes a model returns a ModelState representing it. * * @param Model $model * @param bool|false $excludeRels * * @return static * * @throws TypeError * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public static function fromModel($model, $excludeRels = false) { $fields = []; /** @var $field Field */ foreach ($model->meta->localFields as $name => $field) { try { $fields[$name] = $field->deepClone(); } catch (\Exception $e) { throw new TypeError(sprintf("Couldn't reconstruct field %s on %s: %s", $name, $model->meta->modelName)); } } if ($excludeRels == false) { foreach ($model->meta->localManyToMany as $name => $field) { try { $fields[$name] = $field->deepClone(); } catch (\Exception $e) { throw new TypeError(sprintf("Couldn't reconstruct field %s on %s: %s", $name, $model->meta->modelName)); } } } $overrides = $model->meta->getOverrides(); $meta = []; $ignore = ['registry']; foreach ($overrides as $name => $value) { if (in_array($name, $ignore)) { continue; } $meta[$name] = $value; } $extends = ''; $parent = $model->getParent(); if (!$parent->isAbstract() && !Model::isModelBase($parent->getName())) { $extends = ClassHelper::getNameFromNs($parent->getName(), BaseOrm::getModelsNamespace()); } $kwargs = ['meta' => $meta, 'extends' => $extends]; return new static($model->meta->modelName, $fields, $kwargs); }
/** * @param Model $model * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function prepare($model) { if (empty($this->primaryKey)) { if (!empty($this->parents)) { $field = current(array_values($this->parents)); $field->primaryKey = true; $this->setupPrimaryKey($field); } else { $field = AutoField::createObject(['verboseName' => 'ID', 'primaryKey' => true, 'autoCreated' => true]); $model->addToClass('id', $field); } } }
/** * @ignore * * @return string */ public function __toString() { return $this->scopeModel->getFullClassName() . '->' . $this->name; }
/** * Creates an intermediary model. * * @param ManyToManyField $field * @param Model $model * * @return Model * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function createManyToManyIntermediaryModel($field, $model) { $modelName = $model->meta->modelName; if (is_string($field->relation->toModel)) { $toModelName = Tools::resolveRelation($model, $field->relation->toModel); } else { $toModelName = $field->relation->toModel->meta->modelName; } $className = sprintf('%1$s_%2$s', $modelName, $field->name); $intermediaryClass = 'class %1$s extends %2$s{ public function fields(){} }'; $intermediaryClass = sprintf($intermediaryClass, $className, Model::getFullClassName()); if (!class_exists($className, false)) { eval($intermediaryClass); } $from = strtolower($modelName); $to = strtolower($toModelName); if ($from == $to) { $to = sprintf('to_%s', $to); $from = sprintf('from_%s', $from); } $fields = [$from => ForeignKey::createObject(['to' => $modelName, 'dbConstraint' => $field->relation->dbConstraint, 'onDelete' => Delete::CASCADE]), $to => ForeignKey::createObject(['to' => $toModelName, 'dbConstraint' => $field->relation->dbConstraint, 'onDelete' => Delete::CASCADE])]; $meta = ['dbTable' => $field->_getM2MDbTable($model->meta), 'verboseName' => sprintf('%s-%s relationship', $from, $to), 'uniqueTogether' => [$from, $to], 'autoCreated' => true]; $className = '\\' . $className; /** @var $intermediaryObj Model */ $intermediaryObj = new $className(); $intermediaryObj->init($fields, ['meta' => $meta, 'registry' => $field->scopeModel->meta->registry]); return $intermediaryObj; }