/** * Creates an DBSchema object - an object representation of database schema based on ORM-related * entity graph * * @return DBSchema */ function build() { foreach ($this->ormDomain->getClasses() as $class) { if (!$class->hasDao()) { continue; } $this->ormClass = $class; $this->importClass($class); $this->ormClass = null; } foreach ($this->ormDomain->getClasses() as $class) { if (!$class->hasDao()) { continue; } $this->ormClass = $class; $this->dbTable = $this->dbSchema->getTable($this->ormClass->getTable()); $this->ormIdentifier = $class->getIdentifier(); foreach ($class->getProperties() as $property) { if ($this->ormIdentifier !== $property) { $this->ormProperty = $property; $this->importProperty(); $this->ormProperty = null; } } $this->ormClass = $class; } return $this->dbSchema; }
/** * Resolution order: * - IDaoRelated (check a class existance within the global scope and withing the domain scope) --> AssociationPropertyType * - IOrmRelated --> CompositionPropertyType (not implemented) * - IOrmPropertyAssignable --> IOrmPropertyAssignable::getOrmProperty() * - IBoxable --> BoxablePropertyType * - DBType * - any type with public ctor * @return OrmPropertyType */ private function getPropertyType($name, AssociationMultiplicity $multiplicity, array $parameters = array()) { if ($this->ormDomain->classExists($name)) { $class = $this->ormDomain->getClass($name); if ($class->hasDao() && $class->getIdentifier()) { return new AssociationPropertyType($class, $multiplicity, AssociationBreakAction::cascade()); } else { return new CompositePropertyType($class); } } else { if (DBType::hasMember($name)) { $parameters['id'] = $name; if (!isset($parameters['nullable'])) { $parameters['nullable'] = $multiplicity->isNullable() ? 'true' : 'false'; } $dbType = $this->makeObject('DBType', $parameters); return $dbType->getOrmPropertyType(); } else { if (class_exists($name, true)) { if (TypeUtils::isChild($name, 'IDaoRelated')) { return new AssociationPropertyType(call_user_func(array($name, 'orm')), $multiplicity, AssociationBreakAction::cascade()); } else { if (TypeUtils::isChild($name, 'IOrmRelated')) { $orm = call_user_func(array($name, 'orm')); return new CompositePropertyType($orm); } else { if (TypeUtils::isChild($name, 'IOrmPropertyAssignable')) { return call_user_func(array($name, 'getOrmPropertyType'), $multiplicity); } else { if (TypeUtils::isChild($name, 'IBoxable')) { $parameters['id'] = DBType::VARCHAR; if (!isset($parameters['nullable'])) { $parameters['nullable'] = $multiplicity->isNullable() ? 'true' : 'false'; } return new BoxablePropertyType($name, $this->makeObject('DBType', $parameters)); } else { if (TypeUtils::isChild($name, 'ISqlType')) { // for RawSqlType return new PrimitivePropertyType($this->makeObject($name, $parameters)); } else { //$this->makeObject($name, $parameters); } } } } } } } } throw new OrmModelIntegrityException('do not know how to map ' . $name); }
/** * @return OrmProperty */ private function generateContainer(OrmClass $type, SimpleXMLElement $xmlContainer) { $referredTypeName = (string) $xmlContainer['type']; if (!($referredType = $this->importEntity($referredTypeName))) { if (TypeUtils::isExists($referredTypeName) && TypeUtils::isInherits($referredTypeName, 'IDaoRelated')) { $referredType = call_user_func(array($referredTypeName, 'orm')); } else { $referrer = $type->getName() . '.' . (string) $xmlContainer['name']; throw new OrmModelIntegrityException($referrer . ' refers to unknown entity ' . $referredTypeName); } } try { // one-to-many $referredProperty = $referredType->getProperty((string) $xmlContainer['refs']); $propertyType = new OneToManyContainerPropertyType($type, $referredType, $referredProperty); } catch (OrmModelIntegrityException $e) { // many to many try { $proxy = $this->ormDomain->getClass((string) $xmlContainer['refs']); } catch (OrmModelIntegrityException $e) { $proxy = new OrmClass(); $proxy->setHasDao(true); $proxy->setName((string) $xmlContainer['refs']); $this->ormDomain->addClass($proxy); } $propName = strtolower($type->getEntityName()); try { $mtmType = new AssociationPropertyType($type, AssociationMultiplicity::exactlyOne(), AssociationBreakAction::cascade()); $type_property = new OrmProperty($propName, $this->makeFields($type->getTable(), $mtmType), $mtmType, OrmPropertyVisibility::full(), AssociationMultiplicity::exactlyOne()); $proxy->addProperty($type_property); } catch (OrmModelIntegrityException $e) { $type_property = $proxy->getProperty($propName); } $propName = strtolower($referredType->getEntityName()); try { $mtmType = new AssociationPropertyType($referredType, AssociationMultiplicity::exactlyOne(), AssociationBreakAction::cascade()); $referredType_property = new OrmProperty($propName, $this->makeFields($referredType->getTable(), $mtmType), $mtmType, OrmPropertyVisibility::full(), AssociationMultiplicity::exactlyOne()); $proxy->addProperty($referredType_property); } catch (OrmModelIntegrityException $e) { $referredType_property = $proxy->getProperty($propName); } $propertyType = new ManyToManyContainerPropertyType($proxy, $type_property, $referredType_property); } $property = new OrmProperty((string) $xmlContainer['name'], array(), $propertyType, new OrmPropertyVisibility(OrmPropertyVisibility::READONLY), AssociationMultiplicity::zeroOrOne(), false); if (isset($xmlContainer['db-columns'])) { $property->setDBColumnNames($this->getFields($xmlContainer['db-columns'])); } return $property; }
/** * Generats auxiliary classes for ORM-related entities defined within the domain * * @param OrmDomain $ormDomain * * @return void */ function generate(OrmDomain $ormDomain) { foreach ($ormDomain->getClasses() as $class) { $this->generateClassFiles($class); } }