Zend Framework 2 - 显示数据库的内容

Zend Framework 2 - Showing the content of a database

我正在使用 Zend Framework 2 制作某种市场网站。主页上有一个滑块显示所有产品(使用 CSS3 关键帧实现)和一些文本。滑动图片和文本都是从 MySQL 数据库中读取的。但结果,我没有输出,也没有错误。 slider 获取了和数据库行一样多的图片,但是仍然没有内容回显;另外,如果我尝试更改某些内容(例如数据库凭据或模型中的 getter 函数),它会按预期抛出错误,因此它清楚地读取了数据库,问题出在其他地方。

文本数据库有 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(); ?>    

滑块的代码相似,此页面的两个部分可能有相同的问题。正如我所说,检测到数据库,表和列也是如此,它们不是空的,但没有任何回应。接口编写正确,定义了所有功能。所有视图都在 Site\view\Site\Skeleton 文件夹中。关于问题出在哪里的任何线索?谢谢。

您的代码看起来不错。我能看到的唯一问题是您正在使用 ClassMethods 水化器并且您的实体上没有安装器。

保湿器will use the entity API to hydrate the entity, if the setId, setName or setText are not callable then the values will not be set

虽然我建议添加缺少的方法,但您也可以使用 Zend\Stdlib\Hydrator\Reflection 设置实体属性而不使用 setter(通过 SPL ReflectionProperty