Introduction to MVC Programming using Agavi, part 2nd: Adding Forms and database support using Agavi and Doctrine 2
Get Database records
Now that communication between Agavi, doctrine and MySQL is clear, you need to write a viewaction to get and display the list of cars from the MySQL database. First, populate the listing table with some sample records, which is handy for testing during the initial development phase of the operation:
Mysql> INSERT into listing (RecordID, Recorddate, OwnerName, Ownertel,
Owneremail, Vehiclemanufacturerid, Vehiclemodel, Vehicleyear, Vehiclecolor,
Vehiclemileage, vehicleisfirstowned, Vehicleaccessorybit, Vehicleiscertified,
Vehiclecertificationdate, Vehiclesalepricemin, Vehiclesalepricemax,
Vehiclesalepriceisnegotiable, note, ownercity, Ownercountryid, Displaystatus,
Displayuntildate) VALUES (1, ' 2009-06-08 ', ' John Doe ', ' 00123456789876 ',
' John@wasp.example.com ', 2, ' Boxster ', 15457, 1, 23, 1,
' 2008-01-01 ', 35000, 40000, 1, ' the cared for. In good shape, no scratches
or bumps. has prepaid annual service contract till 2009. ', ' London ', 2,
1, ' 2009-10-15 ');
Query OK, 1 row affected (0.05 sec)
Mysql> INSERT into listing (RecordID, Recorddate, OwnerName, Ownertel,
Owneremail, Vehiclemanufacturerid, Vehiclemodel, Vehicleyear, Vehiclecolor,
Vehiclemileage, vehicleisfirstowned, Vehicleaccessorybit, Vehicleiscertified,
Vehiclecertificationdate, Vehiclesalepricemin, Vehiclesalepricemax,
Vehiclesalepriceisnegotiable, note, ownercity, Ownercountryid, Displaystatus,
Displayuntildate) VALUES (2, ' 2009-06-08 ', ' Jane Doe ', ' 00987654321236 ',
' Jane@wasp.example.com ', 2, ' 911 Turbo ', 2003, ' Black ', 17890, 1, 23, 1,
' 2008-06-19 ', 17000, 25000, 1, ', ' Cambridge ', 2, 1, ' 2009-10-15 ');
Query OK, 1 row Affected (0.00 sec)
Now, use the following steps to add the necessary functionality to the WASP application:
Step 1: Create placeholder classes
The list of cars can be viewed as a self-contained component of the Wasp application, so the operations and views associated with this component should be placed in another stand-alone module. Start the Agavi build script and create a new model, as follows:
shell> agavi module-create
...
Module name: Listing
When you are done, add a new displayaction to handle the display of the list. To connect actions to the Displayerrorview and Displaysuccessview views, you need to provide the following values when prompted:
shell> agavi action-wizard
...
Module name: Listing
Action name: Display
Space-separated list of views to create for Display [Success]: Error Success
Now, Agavi will generate the necessary class files and put them in the right place.
Step 2: Define the route
Add a route that references the most recently created operation, as shown in Listing 15:
Listing 15. Listing/displayaction Routing Definition
<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0"
xmlns="http://agavi.org/agavi/config/parts/routing/1.0">
<ae:configuration>
<routes>
...
<!-- action for listing pages "/listing" -->
<route name="listing" pattern="^/listing" module="Listing">
<route name=".display" pattern="^/display/(id:\d+)$" action="Display" />
</route>
</routes>
</ae:configuration>
</ae:configurations>