AJAX In Action [55]
We’re handling our lists of colors and sizes in a fairly inefficient manner at present, by storing comma-separated lists in fields in the Garments table. If we normalize our data in keeping with a good relational model, we ought to have a Licensed to jonathan zheng Web server MVC 97 Garments Colors 003 deerstalker 175 shocking pink fez 004 176 battleship gray Garments_to_Colors 005 beret 177 lemon 003 175 178 blueberry 003 178 009 178 017 183 Figure 3.7 A many-to-many relationship in a database model. The table Colors lists all available colors for all garments, and the table Garments no longer lists any color information. separate table of all available colors, and a bridging table linking garments to colors (what the database wonks call a many-to-many relationship). Figure 3.7 illustrates the use of a many-to-many relationship of this sort. To determine the available colors for our deerstalker hat, we look up the Garments_to_Colors table on the foreign key garment_id. Relating the color_id column back to the primary key in the Colors table, we can see that the hat is available in shocking pink and blueberry but not battleship gray. By running the query in reverse, we could also use the Garments_to_Colors table to list all garments that match a given color. We’re making better use of our database now, but the SQL required to fetch all the information begins to get a little hairy. Rather than having to construct elaborate join queries by hand, it would be nice to be able to treat our garments as objects, containing an array of colors and sizes. Object-relational Mapping tools Fortunately, there are tools and libraries that can do that for us, known as ObjectRelational Mapping (ORM) tools. An ORM automatically translates between database data and in-memory objects, taking the burden of writing raw SQL off the developer. PHP programmers might like to take a look at PEAR DB_DataObject, Easy PHP Data Objects (EZPDO), or Metastorage. Java developers are relatively spoiled for choice, with Hibernate (also ported to .NET) currently a popular choice. ORM tools are a big topic, one that we’ll have to put aside for now. Looking at our application in MVC terms, we can see that adopting an ORM has had a happy side effect, in that we have the beginnings of a genuine Model on Licensed to jonathan zheng 98 CHAPTER 3 Introducing order to Ajax our hands. We now can write our XML-generator routine to talk to the Garment object and leave the ORM to mess around with the database. We’re no longer bound to a particular database’s API (or its quirks). Listing 3.7 shows the change in our code after switching to an ORM. In this case, we define the business objects (that is, the Model) for our store example in PHP, using the Pear::DB_DataObject, which requires our classes to extend a base DB_DataObject class. Different ORMs do it differently, but the point is that we’re creating a set of objects that we can talk to like regular code, abstracting away the complexities of SQL statements. Listing 3.7 Object model for our garment store require_once "DB/DataObject.php"; class GarmentColor extends DB_DataObject { var $id; var $garment_id; var $color_id; } class Color extends DB_DataObject { var $id; var $name; } class Garment extends DB_DataObject { var $id; var $title; var $description; var $price; var $colors; var $category; function getColors(){ if (!isset($this->colors)){ $linkObject=new GarmentColor(); $linkObject->garment_id = $this->id; $linkObject->find(); $colors=array(); while ($linkObject->fetch()){ $colorObject=new