AJAX In Action [56]
$colorObject->id=$linkObject->color_id;
$colorObject->find();
while ($colorObject->fetch()){
$colors[] = clone($colorObject);
}
}
}
return $colors;
}
}
Licensed to jonathan zheng Web server MVC 99 As well as the central Garment object, we’ve defined a Color object and a method of the Garment for fetching all Colors that it is available in. Sizes could be implemented similarly but are omitted here for brevity. Because this library doesn’t directly support many-to-many relationships, we need to define an object type for the link table and iterate through these in the getColors() method. Nonetheless, it represents a fairly complete and readable object model. Let’s see how to make use of that model in our page. Using the revised model We’ve generated a data model from our cleaner database structure. Now we need to use it inside our PHP script. Listing 3.8 revises our main page to use the ORMbased objects. Listing 3.8 Revised page using ORM to talk to the database header("Content-type: application/xml"); echo "\n"; include "garment_business_objects.inc" $garment=new Garment; $garment->category = $_GET["cat"]; $number_of_rows = $garment->find(); echo " while ($garment->fetch()) { printf(" ." $garment->id, $garment->title, $garment->description, $garment->price); $colors=$garment->getColors(); if (count($colors)>0){ echo " for($i=0;$i } echo " } echo " } echo " ?> We include the object model definitions and then talk in terms of the object model. Rather than constructing some ad hoc SQL, we create an empty Garment Licensed to jonathan zheng 100 CHAPTER 3 Introducing order to Ajax object and partly populate it with our search criteria. Because the object model is included from a separate file, we can reuse it for other searches, too. The XML View is generated against the object model now as well. Our next refactoring step is to separate the format of the XML from the process of generating it. 3.4.3 Separating content from presentation Our View code is still rather tangled up with the object, inasmuch as the XML format is tied up in the object-parsing code. If we’re maintaining several pages, then we want to be able to change the XML format in only one place and have that apply everywhere. In the more complex case where we want to maintain more than one format, say one for short and detailed listings for display to customers and another for the stock-taking application, then we want to define each format only once and provide a centralized mapping for them. Template-based systems One common approach to this is a template language, that is, a system that accepts a text document containing some special markup notation that acts as a placeholder for real variables during execution. PHP, ASP, and JSP are themselves templating languages of sorts, written as web page content with embedded code, rather than the code with embedded content seen in a Java servlet or traditional CGI script. However, they expose the full power of the scripting language to the page, making it easy to tangle up business logic and presentation. In contrast, purpose-built template languages, such as PHP Smarty and Apache Velocity (a Java-based system, ported to .NET as NVelocity), offer a more limited ability to code, usually limiting control flow to simple branching (for example, if) and looping (for example, for, while) constructs. Listing 3.9 shows a PHP Smarty template for generating our XML. Listing 3.9 PHP Smarty template for our XML output {section name=garment loop=$garments} {if count($garment.getColors())>0}