ModelGenerator does not work #30

Open
vasvalis opened this Issue Mar 2, 2013 · 9 comments

Comments

Projects
None yet
2 participants

vasvalis commented Mar 2, 2013

I am trying to use agile tookit model generator. As described in the documentation I am deriving my page class from Page_ModelGenerator: My page is defined like this

<?php
class page_ModelGeneratorAre extends Page_ModelGenerator {
}

But I am getting the following error:

/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php:55 [2] Missing argument 1 for Page_ModelGenerator::findModels(), called in /volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php on line 15 and defined
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php:55 [2] Missing argument 2 for Page_ModelGenerator::findModels(), called in /volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php on line 15 and defined
Application Error: Method is not defined for this object

Exception_Logic, code: 0

Additional information:

class: DB
method: getAll
arguments:
0: show tables
/volume1/web/sportin/atk4/lib/AbstractObject.php:783
Member

DarkSide666 commented Mar 2, 2013

As I mentioned here http://stackoverflow.com/questions/15167249/agile-toolkit-modelgenerator-gives-errors
I fixed that exact issue in 8e6f562 commit. You should download newst version of addons and try then. I'm not sure that you'll not get any more errors (because ModelGenerator is quite old and probably a bit obsolete), but at least not this particular one :)

Please let me know if this helps and if I can close this issue.

vasvalis commented Mar 2, 2013

Dear DarkSide666,

:) two first warnings are now gone but there is some more error information:

Application Error: Method is not defined for this object

Exception_Logic, code: 0

Additional information:

class: DB
method: getAll
arguments:
0: show tables
/volume1/web/sportin/atk4/lib/AbstractObject.php:783

Stack trace:
File Object Name Stack Trace
/volume1/web/sportin/atk4/lib/BaseException.php :39 Exception_Logic Exception_Logic->collectBasicData(Null)
/volume1/web/sportin/atk4/lib/AbstractObject.php :493 Exception_Logic Exception_Logic->__construct("Method is not defined for this object", Null)
/volume1/web/sportin/atk4/lib/AbstractObject.php :783 sample_project_db DB->exception("Method is not defined for this object", "Logic")
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :56 sample_project_db DB->__call("getAll", Array(1))
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :56 sample_project_db DB->getAll("show tables")
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :15 sample_project_ModelGeneratorAre page_ModelGeneratorAre->findModels()
/volume1/web/sportin/atk4/lib/AbstractObject.php :289 sample_project_ModelGeneratorAre page_ModelGeneratorAre->init()
/volume1/web/sportin/atk4/lib/ApiFrontend.php :124 sample_project Frontend->add("page_ModelGeneratorAre", "ModelGeneratorAre", "Content")
/volume1/web/sportin/atk4/lib/ApiWeb.php :408 sample_project Frontend->layout_Content()
/volume1/web/sportin/atk4/lib/ApiFrontend.php :38 sample_project Frontend->addLayout("Content")
/volume1/web/sportin/atk4/lib/ApiWeb.php :276 sample_project Frontend->initLayout()
/volume1/web/sportin/index.php :15 sample_project Frontend->main()
Note: To hide this information from your users, add $config['logger']['web_output']=false to your config.php file. Refer to documentation on 'Logger' for alternative logging options

Member

DarkSide666 commented Mar 2, 2013

OK, can you test now after 6b55c43 commit?

vasvalis commented Mar 2, 2013

another error:

Application Error: Database Query Failed

Exception_DB, code: 0

Additional information:

pdo_error: SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''
mode:
params:
query: desc template: desc
/volume1/web/sportin/atk4/lib/DB/dsql.php:1513

Stack trace:
File Object Name Stack Trace
/volume1/web/sportin/atk4/lib/BaseException.php :39 Exception_DB Exception_DB->collectBasicData(Null)
/volume1/web/sportin/atk4/lib/AbstractObject.php :493 Exception_DB Exception_DB->__construct("Database Query Failed", Null)
/volume1/web/sportin/atk4/lib/DB/dsql.php :1513 sample_project_db_db_dsql_mysql_4 DB_dsql_mysql->exception("Database Query Failed")
/volume1/web/sportin/atk4/lib/DB/dsql.php :1631 sample_project_db_db_dsql_mysql_4 DB_dsql_mysql->execute()
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :62 sample_project_db_db_dsql_mysql_4 DB_dsql_mysql->get()
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :15 sample_project_ModelGeneratorAre page_ModelGeneratorAre->findModels()
/volume1/web/sportin/atk4/lib/AbstractObject.php :289 sample_project_ModelGeneratorAre page_ModelGeneratorAre->init()
/volume1/web/sportin/atk4/lib/ApiFrontend.php :124 sample_project Frontend->add("page_ModelGeneratorAre", "ModelGeneratorAre", "Content")
/volume1/web/sportin/atk4/lib/ApiWeb.php :408 sample_project Frontend->layout_Content()
/volume1/web/sportin/atk4/lib/ApiFrontend.php :38 sample_project Frontend->addLayout("Content")
/volume1/web/sportin/atk4/lib/ApiWeb.php :276 sample_project Frontend->initLayout()
/volume1/web/sportin/index.php :15 sample_project Frontend->main()
Note: To hide this information from your users, add $config['logger']['web_output']=false to your config.php file. Refer to documentation on 'Logger' for alternative logging options

Member

DarkSide666 commented Mar 3, 2013

Maybe you can try to take out backtick symbols around $table in ModelGenerator.php line 62.
Not sure if that helps, but you can try.

I'm off to sleep now.

vasvalis commented Mar 3, 2013

this is the code which executes without errors: now I'll try to use it ...

<?php

class Page_ModelGenerator Extends Page {
    private $capitalize = true;
    private $postfix = "Core";
    protected $skip_pages = false;

    function init(){
        parent::init();
        /* dirty. will clean up later, but working well */
        $this->add("Text")->set("Welcome. This is Model Creator Kit. It will use mysql database to create models for you");
        $c=$this->add('Columns');
        $f=$c->addColumn('50%')->add('Form');
        $l=$this->api->locatePath('php','Model');
        list($tables, $fields) = $this->findModels();
        $tables = array("-1" => "All") + $tables;
        $f->addField('dropdown','table')->setValueList($tables);
        $f->addButton('create')->js("click", $f->js()->submit());
        $r=$c->addColumn('50%');
        $output_object=$r->add('HtmlElement');
        $create_object=$r->add('HtmlElement')->setElement('pre');
        if($f->isSubmitted()){
            $code = "";
            if ($f->get("table") == -1){
                foreach ($tables as $k => $table){
                    if ($k > -1){
                        $code .= htmlspecialchars($this->generateModel($table, $fields[$table], $tables));

                    }
                }
            } else {
                $code = $this->generateModel($tables[$f->get("table")], $fields[$tables[$f->get("table")]], $tables);
            }
            $create_object->js()->html($code)->execute();
            return;
        }

    }
    function resolveFieldType($type){
        $cast = array(
            "int" => "int(11)",
            "money" => "decimal(10,2)",
            "datetime" => "datetime",
            "date" => "date",
            "string" => "varchar(255)",
            "text" => "text",
            "boolean" => "enum('Y','N')",
        );
        $ret = array_search($type, $cast);
        return $ret?$ret:"string";
    }
    function resolveFieldName($field){
        return $field->name();
    }
    function findModels($dir=null, &$models=null, $prefix = null){
        $r = $this->api->db->dsql()->expr('show tables')->get();

        $tables = array();
        foreach ($r as $row){
            $arrVals = array_values($row);
            $tables[] = $arrVals[0];
        }

        foreach ($tables as $table){
            if ($table){
                $fields[$table] = $this->api->db->dsql()->expr("desc `$table`")->get();
            }
        }
        return array($tables, $fields);
    }
    function generateModel($table, $fields, $tables){
        $ignore = $this->api->getConfig("mg/ignore", array());
        if ($ignore){
            foreach ($ignore as $pattern){
                if (preg_match("/$pattern/", $table)){
                    return "Ignoring $table ($pattern)\n";
                }
            }
        }
        $v = $this->add("View", null, null, array("view/model"));
        $v->template->set("php", "<?php");
        $v->template->set("class_name", "Model_" . $this->getModelByTable($table) ."_" . $this->postfi . $this->postfix);
        $v->template->set("entity_code", $table);
        $v->template->set("extends", "Model_Table");
        $v->template->set("table_alias", "al_" . substr($table, 0, 2));
        $l = $v->add("Lister", null, "field_lister", array("view/model", "field_lister"));
        $l->safe_html_output = false;
        foreach ($fields as $k => $field){
            if ($field["Field"] == "id"){
                unset($fields[$k]);
                continue;
            }
            $fields[$k]["datatype"] = $this->resolveFieldType($field["Type"]);
            if ((array_search(substr($field["Field"], 0, -3), $tables) !== false) && (substr($field["Field"], -2) == "id")){
                $fields[$k]["aux"] .= "->refModel(\"Model_" . $this->getModelByTable(substr($field["Field"], 0, -3)) ."\")";
            } else {
                $fields[$k]["aux"] .= "";
            }
            if ($field["Field"] == "deleted"){
                $fields[$k]["aux"] .= "->system(true)->visible(false)";
            }
        }
        $l->setStaticSource($fields);
        $m = (string)$v;
        $lbase = "lib/Model";
        $pbase = "page";
        $chunks = explode("_", $table);
        $model_name = $this->uc($chunks[count($chunks)-1]);
        $page_name = strtolower($this->uc($chunks[count($chunks)-1]));
        $auto_model_name = $this->uc($model_name) . "_" . $this->postfix;
        foreach ($chunks as $chunk){
            $chunk = $this->uc($chunk);
            /* create model dir */
            $dir=$lbase ."/". $chunk;
            if (!file_exists($dir)){
                $out .= "Created dir $dir\n";
                mkdir($dir);
            }
            $lbase = $lbase ."/" . $chunk;
            /* create page dir */
            if (!$this->skip_pages){
                if ($model_name != $chunk){
                    $dir = $pbase ."/". strtolower($chunk);
                    if (!file_exists($dir)){
                        $out .= "Created dir $dir\n";
                        mkdir($dir);
                    }
                    $pbase = $pbase ."/" . strtolower($chunk);
                }
            }
        }
        $fid = fopen($file=$lbase . "/" . $this->postfix . ".php", "w");
        $out .= "Created $file\n";
        fputs($fid, $m);
        fclose($fid);
        if (!file_exists($file=$lbase . ".php")){
            $out .= "Created $file\n";
            $v = $this->add("View", null, null, array("view/model_core"));
            $v->template->set("php", "<?php");
            $v->template->set("class_name", "Model_" . $this->getModelByTable($table));
            $v->template->set("extends", "Model_" . $this->getModelByTable($table) ."_" . $this->postfix);
            $fid = fopen($lbase . ".php", "w");
            fputs($fid, (string)$v);
            fclose($fid);
        }
        if (!$this->skip_pages){
            if (!file_exists($file=$pbase."/".$page_name. ".php")){
                $out .= "Created $file\n";
                $v = $this->add("View", null, null, array("view/page"));
                $v->template->set("model", $this->getModelByTable($table));
                $v->template->set("pmodel", strtolower($table));
                $v->template->set("php", "<?php");

                $fid = fopen($pbase."/" . $page_name . ".php", "w");
                fputs($fid, (string)$v);
                fclose($fid);
            }
        }
        return $out;
    }
    function getModelByTable($table){
        if (!$this->capitalize){
            return $table;
        }
        $table=str_replace('_',' ',$table);
        $table=ucwords($table);
        $table=str_replace(' ','_',$table);
        return $table;
    }
    function uc($p){
        if (!$this->capitalize){
            return $p;
        }
        return ucfirst($p);
    }
}
Member

DarkSide666 commented Mar 3, 2013

OK, thanks! I'll check it out I guess tomorrow.

vasvalis commented Mar 4, 2013

So if I try to choose a table and I hit create I am getting the following error:

Error in AJAX response: SyntaxError: Unexpected token <

Application Error: File not found

Exception_PathFinder, code: 0

Additional information:

file: view/model.html
type: template
attempted_locations:
0: /var/services/web/sportin/templates/default/view/model.html
1: /volume1/web/sportin/atk4/templates/default/view/model.html
2: /volume1/web/sportin/atk4/templates/shared/view/model.html
/volume1/web/sportin/atk4/lib/PathFinder.php:207

Stack trace:
File Object Name Stack Trace
/volume1/web/sportin/atk4/lib/BaseException.php :39 Exception_PathFinder Exception_PathFinder->collectBasicData(Null)
/volume1/web/sportin/atk4/lib/AbstractObject.php :493 Exception_PathFinder Exception_PathFinder->__construct("File not found", Null)
/volume1/web/sportin/atk4/lib/PathFinder.php :207 sample_project_pathfinder PathFinder->exception("File not found")
/volume1/web/sportin/atk4/lib/ApiCLI.php :248 sample_project_pathfinder PathFinder->locate("template", "view/model.html", "path")
/volume1/web/sportin/atk4/lib/SMlite.php :428 sample_project Frontend->locatePath("template", "view/model.html")
/volume1/web/sportin/atk4/lib/SMlite.php :455 sample_project_smlite_12 SMlite->findTemplate("view/model")
/volume1/web/sportin/atk4/lib/AbstractView.php :198 sample_project_smlite_12 SMlite->loadTemplate("view/model")
/volume1/web/sportin/atk4/lib/AbstractObject.php :281 sample_project_ModelGeneratorAre_view View->initializeTemplate(Null, Array(1))
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :80 sample_project_ModelGeneratorAre page_ModelGeneratorAre->add("View", Null, Null, Array(1))
/volume1/web/sportin/atk4-addons/mvc/Page/ModelGenerator.php :32 sample_project_ModelGeneratorAre page_ModelGeneratorAre->generateModel("competition", Array(3), Array(9))
/volume1/web/sportin/atk4/lib/AbstractObject.php :289 sample_project_ModelGeneratorAre page_ModelGeneratorAre->init()
/volume1/web/sportin/atk4/lib/ApiFrontend.php :124 sample_project Frontend->add("page_ModelGeneratorAre", "ModelGeneratorAre", "Content")
/volume1/web/sportin/atk4/lib/ApiWeb.php :408 sample_project Frontend->layout_Content()
/volume1/web/sportin/atk4/lib/ApiFrontend.php :38 sample_project Frontend->addLayout("Content")
/volume1/web/sportin/atk4/lib/ApiWeb.php :276 sample_project Frontend->initLayout()
/volume1/web/sportin/index.php :15 sample_project Frontend->main()
Note: To hide this information from your users, add $config['logger']['web_output']=false to your config.php file. Refer to documentation on 'Logger' for alternative logging options

Member

DarkSide666 commented Mar 12, 2013

Did you manage to fix this? If so, then maybe you can make pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment