我有一个从数据库读取的服务,接受SQL查询的参数,我需要为参数的不同值调用它,但在同一视图中。实际上,这会导致始终使用最后一个值调用它。示例(在我的控制器中):
public function someAction () {
return new ViewModel(array (
"welcome" => $this->homeService->findText("welcome"),
"address" => $this->homeService->findText("address"),
"map" => $this->homeService->findText("map"),
));
}
这样做会调用我的 homeService,因为它的参数始终是"map"。
我尝试在我的module.config中设置它.php(Site''Service''HomeServiceInterface当然是我的homeService):
'service_manager' => array(
'factories' => array(
'Site'Service'HomeServiceInterface' => 'Site'Factory'HomeServiceFactory',
),
'shared' => array (
'Site'Service'HomeServiceInterface' => false
),
),
因为我读到"共享"数组应该允许拥有同一服务的多个实例,但这不起作用。因此,除了创建另一个服务(我觉得很糟糕)之外,我无法想象该怎么做。有什么想法吗?谢谢。
编辑
家服务(界面有所需的一切)
use Site'Model'Home;
use Site'Model'HomeInterface;
use Site'Mapper'TextMapperInterface;
class HomeService implements HomeServiceInterface {
protected $textMapper;
public function __construct (TextMapperInterface $textMapper) {
$this->textMapper = $textMapper;
}
public function findText($name) {
return $this->textMapper->find($name);
}
}
TextMapper如下:
use Site'Model'HomeInterface;
use Zend'Db'Adapter'AdapterInterface;
use Zend'Db'Adapter'Driver'ResultInterface;
use Zend'Stdlib'Hydrator'HydratorInterface;
use Zend'Db'ResultSet'HydratingResultSet;
use Zend'Db'Sql'Sql;
use Zend'Stdlib'Hydrator'ClassMethods;
class TextMapper implements TextMapperInterface {
protected $homePrototype;
protected $adapter;
protected $hydrator;
public function __construct(AdapterInterface $adapter, HomeInterface $homePrototype, HydratorInterface $hydrator) {
$this->adapter = $adapter;
$this->homePrototype = $homePrototype;
$this->hydrator = $hydrator;
}
public function find($name) {
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from("mono");
$select->where(array("name = ?" => $name));
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if ($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {
return $this->hydrator->hydrate($result->current(), $this->homePrototype);
}
throw new 'InvalidArgumentException("{$name} doesn't exist.");
}
}
HomeInterface 是模型,它有简单的 getter 和 setter 方法。
好吧,我想我解决了,所以我会发布我的想法,这肯定工作正常。当然,textMapper 只被__construct ()
实例化一次,所以我想每次在同一个动作中调用它时,它都会以相同的方式发生。我决定修改一切。我有一个包含 3 行的数据库:id
、name
(直到现在我一直在我的 find($name)
函数中使用)和 text
.我的模型是这样的:
class Home implements HomeInterface {
protected $id;
protected $name;
protected $text;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getText() {
return $this->text;
}
public function setText ($text) {
$this->text = $text;
}
}
现在我更改了表格:字段是 id
、 home
、certification
、about
、welcome
、address
、map
(最后 3 个是我在这种情况下需要的字段)。所以现在我的新Home
有了新的getter和setter来适应这种新方式。(getHome()
、setHome($home)
等)Mapper的find()
不再需要$name
参数。现在,当我在视图中时,我可以使用所需的 getter 来显示我想要的文本。我现在的行动很简单:
public function someAction () {
return new ViewModel(array (
"welcome" => $this->homeService->findText("welcome"),
));
}
在 some.phtml 页面中:
<?php echo $this->welcome->getWelcome(); ?>
<?php echo $this->welcome->getAddress(); ?>
<?php echo $this->welcome->getMap(); ?>
我想这是一个比以前更好的设计。