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

标签 php mysql database pdo zend-framework2

我正在使用 Zend Framework 2 制作某种市场网站。首页有一个 slider ,显示所有产品(使用 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);    
    }    
}    
?>    

Controller

<?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")    
        ));    
    }    
}    
?>    

最后, View :

<?php echo $this->home->getText(); ?>    

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

最佳答案

您的代码看起来不错。我能看到的唯一问题是您正在使用 ClassMethods hydrator,并且您的实体上没有 setter。

保湿器 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)

关于php - Zend Framework 2 - 显示数据库的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816759/

相关文章:

java - 将 to_date(CurrentDate) 与时间戳进行比较

php - 如何为客户创建登录?

php - 在同一页面上重用MYSQL查询

database - 按字符前缀的 Postgres 分区

MySQL COUNT IF 和 GROUP BY

MySQL - 索引 - 我做错了什么?

php - 使用 MySQL 从另一个表复制数据时缺少行

php - 64 位 Linux/Ubuntu 和 openssl 问题(无法读取符号 : Bad value)

php - [Route : login] [URI: {locale}/login] when i redirect as a guest 缺少必需的参数

mysql - 替换 MySQL varchar 中的字符