I’ve put together a database admin application using the database functionality provided by the Zend Framework. Basically, it’s a generic set of classes that allows people to quickly add, edit, delete, and view data in a database table. The nice thing is that this uses the Zend_DB_Table classes, so it should presumably work with just about any database supported by the Zend Framework. I’ve tried this on mySQL and sqlite, and it worked just fine on both. There are some limitations of course. Right now this will only work on tables that have auto-increment primary keys. I don’t quite know how this would work with databases that don’t have auto-increment such as Oracle – maybe this could have support for defining the sequence that should be used when inserting data. This is a PHP5 only class due to it’s reliance on the Zend Framework.
The intention of Zdbform is to have a quick way to put up a database admin page for webapp control panel that is easier to use than tools such as phpMyAdmin. Something that can provide validation, customization of the view, integration within other pages,
You can see an example of this running or check out the API docs.
Zdbform has the following features:
- Simple maintenance of database tables
- View table data with pagination, searching, and sorting of columns
- Ability to change the display names of columns
- Ability to hide columns in view, add, and edit
- Extendable set of widgets to show data in table / forms
- Extendable set of validators to validate column data
- Ability to add row validators that validate the full contents of a newly inserted or edited row
Other items that I would like to add in the future are:
- Custom callbacks on add / edit / delete
- Think about defining Zdbform as extension of Zend_DB_Form
- If definition of class settings is externalized it could open up opportunities for export of data, ajax editing, ajax validation, etc.
- Rename classes to fit within naming standards of Zend Framework. Think about how this fits into the MVC parts of the Zend Framework.
The page that defined the example is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php require_once 'Zend/Db.php'; require_once 'Zend/Db/Table.php'; require_once 'zdbform.class.php'; require_once 'zdbform_widgets.class.php'; require_once 'zdbform_validations.php'; $params = array ( 'host' => 'localhost', 'username' => 'test', 'password' => 'test', 'dbname' => 'delicious.db' ); $db = Zend_Db::factory('pdoSqlite', $params); Zend_Db_Table::setDefaultAdapter($db); class Posts extends Zend_Db_Table {} $dbform = new Zdbform('Posts'); $dbform->setWidget('posted', 'timestamp'); $dbform->setWidget('description', 'html'); $dbform->setWidget('href', 'link'); $dbform->setDisplayName('id', 'ID'); $dbform->setDisplayName('href', 'Link'); $dbform->hideColumns('posted'); $dbform->setWidgetOption('href','width',70); $dbform->setHelpText('posted', 'm/d/y format'); $dbform->setHelpText('tags', 'Enter one or more tags, separated by commas'); $dbform->tableTitle = "Backed up delicious links"; $dbform->editTitle = "Edit link information"; $dbform->addTitle = "Add new link"; $dbform->addColumnValidator('tags','validate_minlength',array('minlength' => 4)); $dbform->addColumnValidator('href,description','validate_required',array()); $dbform->rowsPerPage = 15; $dbform->processForms(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>ZDBFORM: Manage Posts Table</title> <link type="text/css" rel="stylesheet" href="zdbform.css" /> </head> <body> <?php $dbform->showForms(); $dbform->showTable(); ?> </body> </html> |
Here are the main steps:
- Lines 2-6 include the required libraries for this to work. It’s important to note that you need to have the Zend Framework classes in your include path for PHP. Alternatively, you can use the set_include_path at the top of the script.
- Lines 7-13 sets up database parameters and then creates a Zend DB Adapter using the options provided. In this case it’s a sqlite database. Typically this stuff would probably just be defined in one place in your application, and then included, but for this case I’ve put everything on one page
- Lines 14-15 set the newly created Zend DB Adapter to be the default adapter for Zend DB Table and then defines ‘Posts’ a class that extends Zend_DB_Table. The name of the class is important – it is the name of the database table we want to manage.
- Line 17 is the first place where we see Zdbform. Here, we create an instance of Zdbform, passing it the name of the table we want to manage.
- Lines 18-32 set various options for our table. For example, Line 18 sets the posted column’s widget to timestamp, which will show a dropdown for selection of a date. Line 19 sets the description column’s widget to html, which will use FCKeditor (a JavaScript HTML editor) when adding and editing data. Lines 30 and 31 set various validators that will validate data on edit or insert. All of these steps are optional and only enhance the behavior / look and feel of Zdbform.
- Line 33 calls Zdbform::processForms(), which will process any data that has been submitted for add, edit, or delete
- Finally, lines 47 and 48 display the forms and display the table of data
March 25th, 2006 at 4:39 pm
Very nice pice of work. I think extending Zend_DB_Form is the way to go.
March 26th, 2006 at 5:30 pm
Neat. You keep putting up useful scripts. Thanks.
I wish you read/replied to your comments though
April 3rd, 2006 at 6:33 am
I´m developing a “personal” framework for a while.
Is very similar to this in the definition. The advantage of mine is that I can put a custom validation for each fields using PHP and JS in the client side.
in the definition
‘age’=>array(‘titulo’=>’EDAD’, ‘tipo’=>’NUMERO’, ‘validaphp’=>’entero_positivo’ , ‘error_php’=>’numero invalido’) ;
this piece of code made all the magic in javascript and in the server side the function “enter0_positivo ” made the validation (if ($v >=0) return true; else return false ; )
presenting the text “error_php” in the form.
Sorry my english,very interesting this blog entry.
greetings
April 12th, 2006 at 8:42 am
You have an error in your modal type. When you view the database and you click delete the modal popups up, but if you click cancel it still deletes the data row. Tested in Firefox 1.5 and IE 6.0.2800 with SP2.
April 22nd, 2006 at 6:14 am
“Right now this will only work on tables that have auto-increment primary keys. I don’t quite know how this would work with databases that don’t have auto-increment such as Oracle”
Oracle supports INSERT TRIGGERS so that’s what you will use I suppose.
It being PHP5 only means you wont see wide acceptance on the WEB for at least two more years..a pity.
April 22nd, 2006 at 12:16 pm
Not seeing wide acceptance on the web may be a blessing – with wide acceptance comes bugs, support, and lots of time spent!
January 21st, 2007 at 10:55 pm
Nice work
would be good to see it working with the MVC
and the widgets created as view helpers
February 19th, 2007 at 4:03 pm
Greattttt !
Nice work !
Thank you very much !
February 21st, 2007 at 5:27 pm
[quote]
T D says…
April 12th, 2006 at 8:42 am
You have an error in your modal type. When you view the database and you click delete the modal popups up, but if you click cancel it still deletes the data row. Tested in Firefox 1.5 and IE 6.0.2800 with SP2.[/quote]
I have the same problem but it’s easy to resolve this.
Just replace this :
onClick=”confirm(‘Are you sure you want to delete this row?’);”
by this :
onClick=”if(!confirm(‘Are you sure you want to delete this row?’)) return false;”
on the line 538 in the zdbform.class.php file.
Clean working solution (tested).
February 21st, 2007 at 5:45 pm
Explanation :
if the “confirm” function returns false (so the user clicked on Cancel or X to close), we return false (= we stop the link action (href)) and so, no call to the delete action in the php file.
If javascript is not activated, no problem. Simply no confirmation, just delete the record directly (also tested).
April 8th, 2007 at 10:41 pm
nice work, but i think it need somethings to have better performance, i use an xml for every module,controller, action and in that i recomende needed form, datagrid and list show
with this xml i can use multi table join for add, delete edit form too,
i think this is better for big project
April 12th, 2007 at 12:22 am
Hi ,
Looks great , but how about file uploads…………….
November 28th, 2007 at 4:16 am
First, looks a very good new class indeed.
What do you not fix your example from the “Cancel Delete” bug ?
Also, what about a more complex example with 2 tables linked to each other ?
November 28th, 2007 at 4:33 am
Despite the line number 31
$dbform->addColumnValidator(‘href,description’,'validate_required’,array());
the href field is not checked, you can enter anything in this field !
Do you know what is the workaround for this ?
November 28th, 2007 at 4:35 am
Despite the line number 31
$dbform->addColumnValidator(’href,description’,’validate_required’,array());
the href field is not checked, you can enter anything in this field !
Do you know what is the workaround for this ?
Also, is this the last version of your code ?
Thanks
February 11th, 2008 at 6:19 am
How can I display the value of a related field instead of category_id ?
April 2nd, 2009 at 4:24 am
[...] Framework’s robust database classes (such as Zend_Db and Zend_Db_Table), you can check out zdbform. It’s a short yet effective library that let’s you perform simple administration tasks [...]
April 26th, 2009 at 7:07 pm
please help me .
I connected to my database , my page ok when I get data form database .but when I use zdbform . I have a problem with zdbform .
my page display error :
Fatal error: Uncaught exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘Array’ in ‘order clause” in C:\wamp\www\1\library\Zend\Db\Statement\Pdo.php:238 Stack trace: #0 C:\wamp\www\1\library\Zend\Db\Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 C:\wamp\www\1\library\Zend\Db\Adapter\Abstract.php(433): Zend_Db_Statement->execute(Array) #2 C:\wamp\www\1\library\Zend\Db\Adapter\Pdo\Abstract.php(230): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array) #3 C:\wamp\www\1\library\Zend\Db\Table\Abstract.php(1330): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select)) #4 C:\wamp\www\1\library\Zend\Db\Table\Abstract.php(1158): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select)) #5 C:\wamp\www\1\library\zdbform\zdbform.class.php(469): Zend_Db_Table_Abstract->fetchAll(NULL, ‘Array ASC’, 15, 0) #6 C:\wamp\www\1\library\zdbform\zdbform.class.php(478): Zdbform->getAllRows() #7 C:\wamp\www\1\application\views\scripts\admin\faq in C:\wamp\www\1\library\Zend\Db\Statement\Pdo.php on line 238
thanks !
July 15th, 2010 at 12:53 am
i have the same problem!!!
my page display error :
Fatal error: Uncaught exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘Array’ in ‘order clause” in C:\wamp\www\1\library\Zend\Db\Statement\Pdo.php:238 Stack trace: #0 C:\wamp\www\1\library\Zend\Db\Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 C:\wamp\www\1\library\Zend\Db\Adapter\Abstract.php(433): Zend_Db_Statement->execute(Array) #2 C:\wamp\www\1\library\Zend\Db\Adapter\Pdo\Abstract.php(230): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array) #3 C:\wamp\www\1\library\Zend\Db\Table\Abstract.php(1330): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select)) #4 C:\wamp\www\1\library\Zend\Db\Table\Abstract.php(1158): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select)) #5 C:\wamp\www\1\library\zdbform\zdbform.class.php(469): Zend_Db_Table_Abstract->fetchAll(NULL, ‘Array ASC’, 15, 0) #6 C:\wamp\www\1\library\zdbform\zdbform.class.php(478): Zdbform->getAllRows() #7 C:\wamp\www\1\application\views\scripts\admin\faq in C:\wamp\www\1\library\Zend\Db\Statement\Pdo.php on line 238
thanks !