Esempio n. 1
0
 private function T_ModelToString(Table $table)
 {
     $variables = array();
     $variables['tClassName'] = $table->getT_ClassName();
     $variables['finalClassName'] = $table->getClassName();
     $variables['tableName'] = $table->getName();
     $str = '';
     foreach ($table->getPrimaryKey()->getFields() as $field) {
         $str .= '\'' . $field->getName() . '\', ';
     }
     $variables['primaryKey'] = substr($str, 0, -2);
     $str = '';
     foreach ($table->getFields() as $field) {
         $str .= '\'' . $field->getName() . '\', ';
     }
     $variables['field_list'] = substr($str, 0, -2);
     $variables['fields'] = $this->removeJoinsFields($table->getFields(), $table->getManyToOneJoins());
     $variables['manyToOne'] = $table->getManyToOneJoins();
     $otm = $table->getOneToManyJoins();
     if (!is_null($otm)) {
         foreach ($table->getOneToManyJoins() as $o) {
             $cleanName = $o->getTargetField();
             if (substr($cleanName, strlen($cleanName) - 3) == '_id') {
                 $cleanName = substr($cleanName, 0, -3);
             } else {
                 if (substr($cleanName, strlen($cleanName) - 2) == 'Id') {
                     $cleanName = substr($cleanName, 0, -2);
                 }
             }
             $cleanName = strtolower($cleanName);
             $o->setCleanField($cleanName);
             $procedureName = $this->driver->writeOneToManyProcedure($o->getTargetTable(), $o->getTargetField(), $cleanName, $table->getName(), $table->getField($o->getField())->getType());
             $o->setProcedureName($procedureName);
         }
     }
     $variables['oneToMany'] = is_null($otm) ? array() : $table->getOneToManyJoins();
     //Find
     $find_params = '';
     $find_proto = '';
     $find_placeholder = '';
     $find_checkNull = '';
     $find_result = '$result';
     foreach ($table->getPrimaryKey()->getFields() as $f) {
         $find_params .= " * @param \$" . $f->getName() . "\n";
         $find_proto .= "\$" . $f->getName() . ", ";
         $find_placeholder .= ":" . $f->getName() . ", ";
         $find_checkNull .= '!is_null(' . $find_result . '[\'' . $f->getName() . '\']) && ';
     }
     $variables['find_params'] = substr($find_params, 0, -1);
     $variables['find_proto'] = substr($find_proto, 0, -2);
     $variables['find_placeholder'] = substr($find_placeholder, 0, -2);
     $variables['find_checkNull'] = substr($find_checkNull, 0, -4);
     $variables['find_result'] = $find_result;
     $variables['SPGetAll'] = $this->driver->writeAllProcedure($table);
     $variables['SPTake'] = $this->driver->writeTakeProcedure($table);
     $variables['SPCount'] = $this->driver->writeCountProcedure($table);
     $variables['pkFields'] = $table->getPrimaryKey()->getFields();
     $variables['findProcedureName'] = $this->driver->writeFindProcedure($table);
     $str = '';
     foreach ($table->getFields() as $field) {
         if (!is_null($field->getSequence())) {
             $str .= '\'' . $field->getName() . '\' => \'' . $field->getSequence() . '\', ';
         }
     }
     $variables['sequencesArray'] = substr($str, 0, -2);
     return $this->twig->render('t_model.php.twig', $variables);
 }
 /**
  * @param Table $table
  * @return string Name of the stored procedure
  */
 public function writeCountProcedure(Table $table)
 {
     $pdo = PDOS::getInstance();
     $sTableName = $table->getName();
     $procedureName = 'count' . $sTableName;
     $pdo->exec("\n          CREATE OR REPLACE function " . $procedureName . "()\n          RETURNS bigint AS\n          'SELECT COUNT(*) FROM " . $sTableName . "'\n          LANGUAGE sql VOLATILE\n          COST 100;\n          ALTER function count" . $sTableName . "()\n          OWNER TO \"" . DBUSER . "\";\n          ");
     return $procedureName;
 }