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 writeFindProcedure(Table $table) { $procedureName = 'find' . $table->getName(); $alias = $table->getName()[0]; $pdo = PDOS::getInstance(); $proto = ''; $where = ''; $signature = ''; $count = 1; foreach ($table->getPrimaryKey()->getFields() as $field) { $proto .= $field->getName() . ' ' . $field->getType() . ', '; $where .= $alias . "." . $field->getName() . ' = $' . $count++ . ' AND '; $signature .= $field->getType() . ', '; } $proto = substr($proto, 0, -2); $where = substr($where, 0, -5); $signature = substr($signature, 0, -2); $pdo->exec("CREATE OR REPLACE function " . $procedureName . "(" . $proto . ")\n RETURNS " . $table->getName() . " AS\n 'SELECT * FROM " . $table->getName() . " " . $alias . " WHERE " . $where . "'\n LANGUAGE sql VOLATILE\n COST 100;\n ALTER function " . $procedureName . "(" . $signature . ")\n OWNER TO \"" . DBUSER . "\";"); return $procedureName; }