【发布时间】:2012-01-13 18:20:38
【问题描述】:
我的 zend 框架项目中需要链接选择框:Countrys->Regions->Provinces->Towns。
我正在使用 zend 表单并打算在其中一个更改时重新提交以重新加载链接选择框的内容。我在 PHPunit 测试中编写了一些代码来模拟我在控制器中需要的内容。 我将需要在我的网站上以多种不同的形式使用这种区域结构,并计划使用 AJAX 进行增强。
我不想复制这段代码,所以我应该将它存储在哪里以及应该如何构建它,以便我可以重用它的功能。我想也许是一个动作助手?
public function testCanGetRegionalStructureFromUser() {
$user = new \Entities\User;
$user = $this->em->getRepository('Entities\User')->findOneByEmail('testuser@yahoo.com');
$town = new \Entities\Town;
$town = $user->getTowns_id();
// if user has not town input, then use the default
if (is_null($town)) {
$config = Zend_Registry::get('config');
$defaulttownid = $config->towns->defaultid;
$town = $this->em->getRepository('Entities\Town')->find($defaulttownid);
}
// get the town id
$townid = $town->getId();
//get the province
$province = $town->getProvinces_id();
$provinceid = $province->getId();
//get the region
$region = $province->getRegions_id();
$regionid = $region->getId();
//get the country
$country = $region->getCountries_id();
$countryid = $country->getId();
$countrylist = $this->em->getRepository('Entities\country')->findActiveCountries();
$regionlist = $this->em->getRepository('Entities\Region')->findActiveRegions($countryid);
$provincelist = $this->em->getRepository('Entities\Province')->findActiveProvinces($regionid);
$townlist = $this->em->getRepository('Entities\Town')->findActiveTowns($provinceid);
}
countrylist、regionlist 等已准备好作为选项注入到我的表单中,用于填充选择框。
【问题讨论】: