【发布时间】:2015-06-13 08:45:24
【问题描述】:
我正在使用 Zend Framework 2 制作某种市场网站。主页上有一个滑块,显示所有产品(使用 CSS3 关键帧实现)和一些文本。滑动图片和文字都是从 MySQL 数据库中读取的。但结果,我没有得到任何输出,但也没有错误。滑块获取与数据库行一样多的图片,但仍然没有回显内容;另外,如果我尝试更改某些内容(例如模型中的数据库凭据或 getter 函数),它会按预期抛出错误,因此它清楚地读取了数据库并且问题出在其他地方。
文本的 Db 有 3 个字段: ID 姓名 文字
文本模型(Home.php;有一个 HomeInterface.php 定义了所有功能)
<?php
namespace Site\Model;
class Home implements HomeInterface {
protected $id;
protected $name;
protected $text;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getText() {
return $this->text;
}
}
?>
文本映射器
<?php
namespace Site\Mapper;
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} non esiste.");
}
}
?>
文本映射器有一个工厂,因为它有依赖关系:
<?php
namespace Site\Factory;
use Site\Mapper\TextMapper;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Site\Model\Home;
use Zend\Stdlib\Hydrator\ClassMethods;
class TextMapperFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
return new TextMapper($serviceLocator->get("Zend\Db\Adapter\Adapter"), new Home(), new ClassMethods(false));
}
}
?>
文本服务:
<?php
namespace Site\Service;
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);
}
}
?>
该服务的工厂:
<?php
namespace Site\Factory;
use Site\Service\HomeService;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class HomeServiceFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $serviceLocator) {
$textMapper = $serviceLocator->get("Site\Mapper\TextMapperInterface");
return new HomeService($textMapper);
}
}
?>
控制器
<?php
namespace Site\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Site\Service\HomeServiceInterface;
use Zend\View\Model\ViewModel;
class SkeletonController extends AbstractActionController {
protected $homeService;
public function __construct(HomeServiceInterface $homeService) {
$this->homeService = $homeService;
}
public function indexAction() {
return new ViewModel(array (
"home" => $this->homeService->findText("home")
));
}
}
?>
最后是视图:
<?php echo $this->home->getText(); ?>
滑块的代码相似,并且此页面的两个部分都可能存在相同的问题。正如我所说,检测到 db、表和列,它们不是空的,但没有任何响应。接口编写得当,定义了所有功能。所有视图都位于 Site\view\Site\Skeleton 文件夹中。关于问题出在哪里的任何线索?谢谢。
【问题讨论】:
标签: php mysql database pdo zend-framework2