PHP Scaffold Generator
UPDATE:
—————
I have done a little reworking of the scaffold generator, you can still download it here. I made a new built in search functionality and took out the status page, now there is just a notice on the main listing page after you take an action. I also cleaned the code up a bit. And since I was called a lazy bastard here is a simple demo. Play with that all you want and tear apart the code.
—————-
Here is a little tool I created for myself when building or working on MySQL databases. It is a scaffold generator similar in functionality to the Ruby on Rails generator and only needs to know how to connect to your database to get up and running. It creates a CRUD (Create, Read, Edit, Delete) admin system for your databases. There are no config files or any of that to mess with. I find it pretty handy and thought I would share it since it is a pain to manually code forms and functionality when you change a field name or add a few fields. It works in PHP 4.3 and up, so pretty much on any server..(although I have no idea about 6.0 when it comes out).
Some basic database structure is assumed for the scaffold to run smoothly..
- Table names are all plural and lowercase.
- Primary keys are all named ‘id’.
- Field names are lowercase and can be underscored .. ie-> ‘first_name’.
- Timestamps named ‘created_at’ and ‘updated_at’ cannot be edited, they will be updated automatically.
- Foreign keys are named as {table_name_singluar_id}, so.. ‘author_id’ references table ‘authors’, ‘book_id’ references table ‘books’, ‘category_id’ references table ‘categories’ and so on.
- Foreign key tables also ARE ASSUMED TO have a field ‘name’.
- Special case for odd plurals can be put in the configuration so ‘person_id’ can reference table ‘people’.
All the details are in the file.
Some nice features of the scaffold..
- Automatically build drop down menus for foreign keys.
- Automatically assign foreign key relationship between singular ‘_id’ and plural table name.. category_id relates to categories and so on.
- Create custom plurals.. ie-> person_id for people
- Limit what is displayed on the listing page.
- Set record count for pagination.
- Set listing table width to drop into a page layout.
- Automatically create reader friendly names from the underscores
All you have to do is include this file on your page
<?php include("path_to/scaffold.php"); ?>
and create the scaffold.
<?php new Scaffold("TABLE_NAME"); ?>
This will setup a fully functional admin system. Throw in a simple stylesheet like this and wrap the scaffold in a centered div and you can pass it off as a custom built system!
body {
margin:2em;
font-family: arial, veranda, helvetica, san-serif;
font-size:1em;
color:#333;
}
You can also call the scaffold like this to set up a custom listing page.
<?php new Scaffold("TABLE_NAME", 10, array('fields', 'that', 'will', 'show'), FALSE, 400); ?>
This will generate listings for your table with 10 records per page, only show the fields you sent in the array, will turn off html safe display and make your table width 400.
Download the PHP Scaffold Generator and feel free to use it how ever you wish. It is licensed under the Creative Commons License so just keep the original credit in tact. Let me know of any suggestions or problems.