JetBrains DataGrip + Doctrine 2

generování entit z datagrip

Pokud je PHPStorm tvůj oblíbený pro vývoj v PHP, budeš mile překvapen z programu DataGrip od JetBrains.

Výčet všech pozitiv proti používání SSMC pro MS SQL asi nemá smysl, prostě vše, co tě už roky iriguje a co microsoft nedokáže ani částečně opravit, je tady vyřešeno. Od pohodlného generování changeskriptů, verzování, přes takové blbosti jako shift+shift pro rychlé nalezení požadované tabulky.

K tomuto všemu je ještě navíc DataGrip možné rozšířit o pluginy, stejně tak jako phpstorm, bohužel ale těch zatím moc není, jedná se poměrně mladý produtk, snad se i toto brzy zlepší.

Hledal jsme plugin, který by mi z tabulky dokázal vygenerovat doctrine-entitu, takový ale neexistuje.
Avšak dataGrip nabízí jednoducou možnost skriptovat výstup pomocí jazyka Groovy.

Výsledný skript (obsahující pravidla pro mé potřeby) zde:

import com.intellij.database.model.DasTable import com.intellij.database.model.ObjectKind import com.intellij.database.util.Case import com.intellij.database.util.DasUtil /* * Available context bindings: * SELECTION Iterable * PROJECT project * FILES files helper */ typeMapping = [ (~/(?i)int/) : "integer", (~/(?i)smallint|tinyint/) : "smallint", (~/(?i)decimal|numeric/) : "decimal", (~/(?i)datetime2/) : "datetime", (~/(?i)datetime/) : "datetime_old", (~/(?i)date/) : "date", (~/(?i)bit/) : "boolean", (~/(?i)/) : "string" ] FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir -> SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generate(it, dir) } } def generate(table, dir) { def originTableName = table.getName() def className = nameTable(table.getName()) def fields = calcFields(table) def bundle = bundleName(table.getName()) new File(dir, className + ".php").withPrintWriter('UTF-8') { out -> generate(out, className, originTableName, bundle, fields) } } def generate(out, className, originTableName, bundle, fields) { out.println " 0) out.print ", precision=${it.precision}" if (it.type == "decimal" && it.scale > 0) out.print ", scale=${it.scale}" out.println ")" } else { out.println " * @ORM\\ManyToOne(targetEntity=\"Miele\\Bundle\\BaseBundle\\Entity\\User\")" out.println " * @ORM\\JoinColumn(name=\"CreatedBy\", referencedColumnName=\"UID\")" } if (it.name == "dateCreated") out.println " * @Gedmo\\Timestampable(on=\"create\")" if (it.name == "id") { out.println " * @ORM\\Id" out.println " * @ORM\\GeneratedValue(strategy=\"AUTO\")" } else { if (it.nullable != "true" && it.name != "dateCreated") out.println " * @Assert\\NotBlank()" if (it.type == "string" && it.size < 10000) out.println " * @Assert\\Length(max=\"${it.size}\")" if (it.type == "decimal" && it.precision > 0 && it.scale == 0) out.println " * @Assert\\Length(max=\"${it.precision}\")" } if (it.name == "createdBy") out.println " * @Gedmo\\Blameable(on=\"create\")" out.println " */" out.println " private \$${it.name};" out.println "" } out.println "}" } def calcFields(table) { DasUtil.getColumns(table).reduce([]) { fields, col -> def spec = Case.LOWER.apply(col.getDataType().getSpecification()) def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value def nullableDb = col.isNotNull() ? "" : "true" fields += [[ name : nameColumn(col.getName()), dbname : col.getName(), type : typeStr, comments: col.comment, nullable: nullableDb, size: col.getDataType().getLength(), precision: col.getDataType().getPrecision(), scale: col.getDataType().getScale(), ]] } } def nameTable(str) { def s = str.replaceAll("__", "") } def nameColumn(str) { def s = str.replaceAll("__", "") Case.LOWER.apply(s[0]) + s[1..-1] } def columnComment(str) { def s = str.replaceAll("\\n", " * ") } def bundleName(str) { if (str.contains("__")) { def s = str.split("__") s = s[0] } }

PHP Symfony duben 2018