[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">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Models Made Simply</title>
</head>
<body>
<form method="post" action="table.php">
<fieldset>
<legend>Model Generator</legend>
<label for="table">Table Name</label><input type="text" id="table" name="table" /><br />
<label for="cols">Columns(Comma Separated)</label><input type="text" id="cols" name="cols" /><br />
<input type="submit" value="Submit" />
</fieldset>
<fieldset>
<legend>Result</legend>
<textarea rows="25" cols="150">
<?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 '<?php'."\n"; // change the < 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 '?>';
?>
</textarea>
</fieldset>
</form>
</body>
</html>