Exemple #1
0
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before a test is executed.
  */
 protected function setUp()
 {
     $this->raw = array(array("id" => 1, "name" => "lazer"));
     $class = "\\flowcode\\demo\\domain\\Weapon";
     $mappingFilePath = dirname(__FILE__) . "/../ceibo-mapping-test.xml";
     $mapping = simplexml_load_file($mappingFilePath);
     $mapper = MapperBuilder::buildFromName($mapping, "weapon");
     $this->object = new Collection($class, $this->raw, $mapper);
 }
Exemple #2
0
 public function testBuildFromName_withTypes_testOk()
 {
     $instance = new Weapon(1, "lazer");
     $mapping = simplexml_load_file($this->mappingFilePath);
     $mapper = $this->object->buildFromName($mapping, "weapon");
     $raw = array("id" => 1, "name" => "lazer");
     $this->assertEquals(get_class($instance), $mapper->getClass());
     $this->assertEquals("weapon", $mapper->getName());
     $property = $mapper->getProperty("Name");
     $this->assertNotNull($mapper->createObject($raw));
     $this->assertEquals("string", $property->getType());
 }
Exemple #3
0
 /**
  * Find an entity bu its id.
  * @param type $class
  * @param type $id
  * @return \flowcode\ceibo\support\class 
  */
 public function findById($class, $id)
 {
     $mapper = MapperBuilder::buildFromMapping($this->mapping, $class);
     $newEntity = NULL;
     $query = "SELECT * FROM `" . $mapper->getTable() . "` WHERE id='{$id}'";
     $result = $this->conn->executeQuery($query);
     if ($result) {
         $class = $mapper->getClass();
         $newEntity = new $class();
         $this->populateEntity($newEntity, $result[0], $mapper);
         // populate relations
         foreach ($mapper->getRelations() as $relation) {
             $relMapper = MapperBuilder::buildFromName($this->mapping, $relation->getEntity());
             $queryRel = $this->buildSelectRelation($newEntity, $mapper, $relation, $relMapper);
             $resRel = $this->conn->executeQuery($queryRel);
             $classRel = $relMapper->getClass();
             $method = "set" . $relation->getName();
             if ($resRel) {
                 foreach ($resRel as $row) {
                     $newEntityRel = new $classRel();
                     $this->populateEntity($newEntityRel, $row, $relMapper, $relation->getForeignColumn());
                     $array[] = $newEntityRel;
                 }
                 $newEntity->{$method}($array);
             }
         }
     }
     return $newEntity;
 }
Exemple #4
0
 /**
  * Finds entitys by its generic filter defined in the configured mapping.
  * @param type $name
  * @param type $filter
  * @param type $page
  * @param type $orderColumn
  * @param type $orderType
  * @return Pager
  */
 public function findByGenericFilter($name, $filter = null, $page = 1, $orderColumn = null, $orderType = null)
 {
     $this->load();
     $mapper = MapperBuilder::buildFromName($this->mapping, $name);
     $selectQuery = "";
     $whereQuery = "";
     $orderQuery = "";
     $selectQuery .= "SELECT * FROM `" . $mapper->getTable() . "` ";
     $filterList = array();
     if (!is_null($filter)) {
         $filterList = explode(" ", $filter);
     }
     if (!is_null($filter)) {
         $whereQuery .= " WHERE 1=2 ";
         foreach ($filterList as $searchedWord) {
             foreach ($mapper->getFilter("generic")->getColumns() as $filteredColumn) {
                 $whereQuery .= " OR {$filteredColumn} LIKE '%" . $searchedWord . "%'";
             }
         }
     } else {
         $whereQuery .= " WHERE 1 ";
     }
     if (!is_null($orderColumn)) {
         $orderQuery .= "ORDER BY {$orderColumn} ";
         if (!is_null($orderType)) {
             $orderQuery .= "{$orderType}";
         } else {
             $orderQuery .= "ASC";
         }
     }
     $from = ($page - 1) * $mapper->getFilter("generic")->getItemsPerPage();
     $pageQuery = " LIMIT {$from} , " . $mapper->getFilter("generic")->getItemsPerPage();
     $query = $selectQuery . $whereQuery . $orderQuery . $pageQuery;
     $result = $this->getDataSource()->query($query);
     if ($result) {
         $collection = new Collection($mapper->getClass(), $result, $mapper);
     } else {
         $collection = new Collection($mapper->getClass(), array(), $mapper);
     }
     $selectCountQuery = "SELECT count(*) as total FROM `" . $mapper->getTable() . "` ";
     $query = $selectCountQuery . $whereQuery;
     $result = $this->getDataSource()->query($query);
     $itemCount = $result[0]["total"];
     $pager = new Pager($collection, $itemCount, $mapper->getFilter("generic")->getItemsPerPage(), $page);
     return $pager;
 }
Exemple #5
0
 public static function getInsertRelation($entity, $relation)
 {
     $relQuery = "";
     $getid = "getId";
     if ($relation->getCardinality() == Relation::$manyToMany) {
         $m = "get" . $relation->getName();
         foreach ($entity->{$m}() as $rel) {
             $relQuery .= "INSERT INTO " . $relation->getTable() . " (" . $relation->getLocalColumn() . ", " . $relation->getForeignColumn() . ") ";
             $relQuery .= "VALUES ('" . $entity->{$getid}() . "', '" . $rel->{$getid}() . "');";
         }
     }
     if ($relation->getCardinality() == Relation::$oneToMany) {
         $relMapper = MapperBuilder::buildFromName($this->mapping, $relation->getEntity());
         $m = "get" . $relation->getName();
         foreach ($entity->{$m}() as $rel) {
             $setid = "set" . $relMapper->getNameForColumn($relation->getForeignColumn());
             $rel->{$setid}($entity->{$getid}());
             $relQuery .= $this->buildInsertQuery($rel);
         }
     }
     return $relQuery;
 }