Welcome Guest, Not a member yet? Register   Sign In
Model Extender Form
#1

[eluser]Kyle Johnson[/eluser]
Hey Everyone,

I've been using this for a little while since I have to work with about 100 tables, but for the most part this gets me setup properly.

It is a very simple form that allows you to enter the class name ("User_table"), and then the fields associated with that table ("user_id first_name last_name"

Please modify it as you see fit.

Usually I would post a link to it, but there still remains much to be done with the application, but this tool has aided me along the way.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
    &lt;title&gt;Models Made Simply&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form method="post" action="table.php"&gt;
        <fieldset>
            <legend>Model Generator</legend>
            <label for="table">Table Name</label>&lt;input type="text" id="table" name="table" /&gt;&lt;br />
            <label for="cols">Columns(Comma Separated)</label>&lt;input type="text" id="cols" name="cols" /&gt;&lt;br />
            &lt;input type="submit" value="Submit" /&gt;
        </fieldset>
        <fieldset>
            <legend>Result</legend>
            &lt;textarea rows="25" cols="150"&gt;
            &lt;?php
            $tablename = $_POST['table'];
            $cols = $_POST['cols'];
            $cols = str_replace(", ", ",", $cols); // replace all comma-spaces with comma
            $cols = explode(",", $_POST['cols']); // split into array
            for($i = 0; $i<count($cols); $i++)
            {
                $cols[$i] = str_replace(" ", "_", trim($cols[$i])); // trim extra spaces and then convert internal spaces to underscore
            }
            echo '&lt;?php'."\n"; // change the &lt; to "& lt ;" without spaces to make it work.  Otherwise it prints out nothing.
            echo "class " . $tablename . " extends Model\n{\n"; //change this part to change what is extended.
            foreach($cols as $v)
            {
                /*
                 * The following line echoes "private var $x"
                 * The getters/setters should be used instead of directly modifying the variable
                 * However, you can remove the "private" word if it bothers you since PHP isn't very strict.
                 */
                echo "\tprivate var $" . $v . ";\n";
            }
            echo "\n";
            echo "\tfunction " . $tablename . "()\n\t{\n\t\tparent::Model();\n\t}\n\n";
            
            foreach($cols as $v)
            {
                echo "\tfunction get_" . $v . "()\n\t{\n\t\treturn ".'$this->'.$v.";\n\t}\n\n";
                echo "\tfunction set_" . $v . "($".$v.")\n\t{\n\t\t".'$this->'.$v." = ".'$'.$v.";\n\t\treturn ".'$this'.";\n\t}\n\n";
            }
            
            echo "}\n";
            echo '?&gt;';
            ?&gt;
            &lt;/textarea&gt;
        </fieldset>
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]R. Oerlemans[/eluser]
Thanks, this will be very usefull!




Theme © iAndrew 2016 - Forum software by © MyBB