Example #1
0
 private function importEntities()
 {
     $file = Singleton::create("DOMXmlFactory")->fromFile("config/service.xml");
     $root = $file->getRootElement();
     $entityEls = $root->getChildren("entity");
     $this->entities = array();
     foreach ($entityEls as $entityEl) {
         $name = $entityEl->getAttribute("name");
         $table = $entityEl->getAttribute("table");
         $entity = new Entity($name, $table);
         // Insert properties
         foreach ($entityEl->getChildren("column") as $property) {
             $propertyName = $property->getAttribute("name");
             $type = $property->getAttribute("type");
             $size = $property->getAttribute("size");
             $entityName = $property->getAttribute("entity");
             $mappingTable = $property->getAttribute("mapping-table");
             $primary = $property->getAttribute("primary") == "true";
             $autoIncrement = $property->getAttribute("auto-increment") == "true";
             if ($primary) {
                 $entity->addPKProperty(new PrimaryKeyProperty($propertyName, $type, $size, $autoIncrement));
             } elseif ($type == "Object") {
                 $entity->addOneToOneProperty(new OneToOneProperty($propertyName, $entityName));
             } elseif ($type == "Collection" && !$mappingTable) {
                 $entity->addOneManyProperty(new OneToManyProperty($propertyName, $entityName));
             } elseif ($mappingTable) {
                 $entity->addManyToManyProperty(new ManyToManyProperty($propertyName, $entityName, $mappingTable));
             } else {
                 $entity->addProperty(new Property($propertyName, $type, $size));
             }
         }
         // Insert finders
         foreach ($entityEl->getChildren("finder") as $finderEl) {
             $finderName = $finderEl->getAttribute("name");
             $unique = $finderEl->getAttribute("unique");
             $cacheable = $finderEl->getAttribute("cacheable");
             $unique = $unique === "true";
             $cacheable = !($cacheable === "false");
             $finder = new Finder($entity, $finderName, $unique, $cacheable);
             foreach ($finderEl->getChildren("finder-column") as $fcEl) {
                 $name = $fcEl->getAttribute("name");
                 $comparator = $fcEl->getAttribute("comparator");
                 if (empty($comparator)) {
                     $comparator = "=";
                 }
                 $finder->addFinderColumn(new FinderColumn($name, $comparator));
             }
             $orderEls = $finderEl->getChildren("order");
             if ($orderEls->size() > 1) {
                 throw new ServiceBuilderException("Multiple order elements defined for finder {$finderName}");
             }
             if ($orderEls->size() == 1) {
                 $orderEl = $orderEls->getFirst();
                 foreach ($orderEl->getChildren("order-column") as $ocEl) {
                     $ocName = $ocEl->getAttribute("name");
                     $ocOrderBy = $ocEl->getAttribute("order-by");
                     $finder->addOrderCol(new OrderColumn($ocName, $ocOrderBy));
                 }
             }
             $entity->addFinder($finder);
         }
         // Insert order
         $orderEls = $entityEl->getChildren("order");
         if ($orderEls->size() > 1) {
             throw new ServiceBuilderException("Multiple order elements defined for entity {$entity->getName()}");
         }
         if ($orderEls->size() == 1) {
             $orderEl = $orderEls->get(0);
             foreach ($orderEl->getChildren("order-column") as $ocEl) {
                 $ocName = $ocEl->getAttribute("name");
                 $ocOrderBy = $ocEl->getAttribute("order-by");
                 $entity->addOrderCol(new OrderColumn($ocName, $ocOrderBy));
             }
         }
         $entity->validate();
         // Add entity
         $this->entities[$entity->getName()] = $entity;
     }
 }