Thu 30 Mar 2006
I’ve been using phpMyAdmin to generate database schemas. Since I use phpMyAdmin pretty much every day, it’s very convenient as opposed to DBDesigner, which resides on a separate server and has the overhead of connecting to and opening another app (just for one function).
The schema diagrams generated by phpMyAdmin are pretty, with colorful straight lines to represent table/field relations. However, the major pitfall is that these schema diagrams don’t show the data type. It’s usually helpful to know whether a field is an int, float, char, or text. This is one thing that DBDesigner does quite well.
Given that we have a couple meetings next week involving schemas (one of them being the start of a series of schema meetings), I decided to hack the phpMyAdmin code a bit so that it shows the data types side-by-side with the field names. This way I can continue to use phpMyAdmin to generate diagrams without the comprising of less information.
Beware, the rest of the post gets a bit techinical (aka geeky).
The only file I hacked was pdf_schema.php. This is located in the root dir of the phpmyadmin install.
This file contains the PMA_RT_Table class, which is where all the hacking was done.
class PMA_RT_Table {
// lots of code...
}
First, I added a new private var to store an array of types. This corresponds to the existing array of fieldnames for the class.
class PMA_RT_Table {
var $fields = array(); // existing code
var $types = array(); // my code
}
Next, in the constructor function, I added a line of code to populate the types array:
function PMA_RT_Table(...) {
// ...snip
// load fields
while ($row = PMA_DBI_fetch_row($result)) {
$this->types[] = $row[1]; // my code
$this->fields[] = $row[0];
}
// ...snip
}
I then added code for the table width calculation to ensure that the drawn table diagrams would be wide enough to display both the field name and data type:
function PMA_RT_Table_setWidth($ff) {
// ...snip
foreach ($this->fields AS $key => $field) {
// srh hack to set width for field and type
$type_arr = split(" +",$this->types[$key]);
$type = $type_arr[0];
if ('enum' == substr($type,0,4))
$type = 'enum';
if ('set' == substr($type,0,3))
$type = 'set';
$field .= " [{$type}]";
// ...snip
}
Finally, I added the same code to actually splice the field name with the data type:
function PMA_RT_Table_draw(...) {
// ...snip
foreach ($this->fields AS $key => $field) {
// srh hack to show data types next to field names
$type_arr = split(" +",$this->types[$key]);
$type = $type_arr[0];
if ('enum' == substr($type,0,4))
$type = 'enum';
if ('set' == substr($type,0,3))
$type = 'set';
$field .= " [{$type}]";
// ...snip
}
I won’t bother going into depth about how and why I hacked the code. Doing so would be hard without presenting more context from the pdf_schema.php file.
The main purpose of this post is to provide me with a memory refresher so I can come back and reference it later at a critical time (i.e. upgrading phpmyadmin). Plus it’s always good to document work like this, no matter how you choose to document it. I chose the blog, and by doing so I am making visible a minor part of my work that would otherwise remain situated in oblivion.
View the difference!
2 Responses to “Hacking Schema Diagrams in phpMyAdmin”
Leave a Reply
You must be logged in to post a comment.


March 30th, 2006 at 3:09 pm
Shaun,
Nicely done. I like the output better than DBDesiger and as you stated in your article, it works with less overhead than DBDesigner which forces you to use X11.
Just a couple of questions:
1. Is there a way, other than arrows sense, to show what sort of relationship each table has to another? Is that what the colored lines are used for?
2. Will phpMyAdmin allow one to design a DB schema and then use that schema to build the table structure as DBDesigner does?
March 30th, 2006 at 3:23 pm
Hi Jerry,
Here’s the short answer (for all three questions above): No.
Now for the longer answers
1. As you can see, the arrows are uni-directional. They imply a one-to-many relationship, where the ‘many’ is at the start of the arrow, and the ‘one’ is at the end of the arrow (where it points to). The nice thing about these arrows is that, unlike in DBDesigner, they point directly to the field names in the tables. This makes it easier to visualize those relations.
However, sometimes a field may be really ‘popular’ (it has lots of arrows pointing to it or from it). To help make these arrows easier to distinguish, phpMyAdmin uses…
2. …colors! The colored lines are purely decorative.
3. DBDesigner has the advantage of building a schema from scratch with its GUI. While phpMyAdmin uses a nice ’scratch board’ to layout the tables and fine-tune their coordinates, it requires that all tables must exist in the database. It does not build table structures for you with its GUI.
Of course, you can always build table structures in phpMyAdmin the conventional way (html input forms!)… and this is how I primarily build new tables anyway.