【问题标题】:Zend Framework 2 - Showing the content of a databaseZend Framework 2 - 显示数据库的内容
【发布时间】: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


    【解决方案1】:

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

    补水器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 设置实体属性而无需设置器(通过 SPL ReflectionProperty

    【讨论】:

    • 所以你是说 ClassMethods() 强迫我添加 setter 方法?我可以让它们空一会儿,直到我可以设计所有用户部分吗?我也会尝试使用 Reflection 来实现,尽管这个项目稍后需要管理员支持
    • 您需要添加它们才能为ClassMethods hydrator 工作。
    • 它看起来微不足道,但效果很好。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多