9 class GenModels
implements Controller
13 function __construct(Connection
$pq) {
17 function __invoke(array $args = null) {
18 $tables = $this->pq
->exec("SELECT tablename FROM pg_tables WHERE schemaname='public'");
19 /* @var $tables \pq\Result */
20 foreach ($tables->fetchAllCols("tablename") as $table) {
21 $this->genModel($table);
25 function genModel($entity) {
26 $title = ucwords($entity);
27 $single = substr($title, -3) == "ies"
28 ?
substr($title, 0, -3)."y"
30 $this->genTable($entity, $title, $single."Collection");
31 $this->genRowset($single."Collection", $single);
32 $this->genRow($single);
35 function genTable($name, $class, $collection) {
36 $ns = explode("\\", $class);
39 $class = array_pop($ns);
40 $dir = implode("/", $ns);
41 $ns = "\\".implode("\\", $ns);
47 $file = __DIR__
."/../Model/$dir/$class.php";
49 if (!file_exists($file)) {
50 file_put_contents($file, <<<EOD
53 namespace app\\Model$ns;
55 use pq\\Gateway\\Table;
57 class $class extends Table
59 protected \$name = "$name";
60 protected \$rowset = "app\\\\Model$ns\\\\$collection";
68 function genRowset($class, $row) {
69 $ns = explode("\\", $class);
72 $class = array_pop($ns);
73 $dir = implode("/", $ns);
74 $ns = "\\".implode("\\", $ns);
80 $file = __DIR__
."/../Model/$dir/$class.php";
82 if (!file_exists($file)) {
83 file_put_contents($file, <<<EOD
86 namespace app\\Model$ns;
88 use pq\\Gateway\\Rowset;
90 class $class extends Rowset
92 protected \$row = "app\\\\Model$ns\\\\$row";
99 function genRow($class) {
100 $ns = explode("\\", $class);
102 if (count($ns) > 1) {
103 $class = array_pop($ns);
104 $dir = implode("/", $ns);
105 $ns = "\\".implode("\\", $ns);
111 $file = __DIR__
."/../Model/$dir/$class.php";
113 if (!file_exists($file)) {
114 file_put_contents($file, <<<EOD
117 namespace app\\Model$ns;
119 use pq\\Gateway\\Row;
121 class $class extends Row