使用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'',
''john@wasp.example.com'', 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'',
''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)
现在,通过以下步骤给 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 "/
|