使用Agavi進行MVC編程簡介,第2部分:使用Agavi和Doctrine添加表單和數據庫支持2
獲取數據庫記錄
現在,Agavi、Doctrine和MySQL之間的通信已經暢通,接下來需要編寫一個ViewAction,以從MySQL數據庫獲取並顯示汽車列表。首先,使用一些示例記錄填充 listing 表;這方便您在操作的初始開發階段對其進行測試:
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',
'[email protected]', 2, 'Boxster', 2005, 'Yellow', 15457, 1, 23, 1,
'2008-01-01', 35000, 40000, 1, 'Well 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',
'[email protected]', 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)
現在,通過以下步驟給 WASP 應用程序添加必要的功能:
步驟 1:創建占位符類
汽車列表可以看作是WASP 應用程序的一個功能獨立的組件,因此與這個組件相關的操作和視圖應該放在另一個獨立的模塊中。啟動 Agavi 構建腳本並創建一個新的模型,如下所示:
shell> agavi module-create
...
Module name: Listing
完成之後,添加一個新的 DisplayAction 來處理列表的顯示。為了將操作與 DisplayErrorView和DisplaySuccessView視圖連接起來,那麼需要在得到提示時提供下列值:
shell> agavi action-wizard
...
Module name: Listing
Action name: Display
Space-separated list of views to create for Display [Success]: Error Success
現在,Agavi 將生成必要的類文件並將它們放到正確的位置中。
步驟 2:定義路由
添加一個引用最新創建的操作的路由,如 清單 15所示:
清單 15. Listing/DisplayAction 路由定義
<?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>