Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 模型映射器,抽象常用代码的最佳方法是什么,即帮助器、装饰器_Php_Zend Framework_Model View Controller_Mapper - Fatal编程技术网

Php 模型映射器,抽象常用代码的最佳方法是什么,即帮助器、装饰器

Php 模型映射器,抽象常用代码的最佳方法是什么,即帮助器、装饰器,php,zend-framework,model-view-controller,mapper,Php,Zend Framework,Model View Controller,Mapper,使用Zend(1或2),在模型映射器中抽象公共代码的最佳方法是什么?在控制器/视图中有相关的帮助程序,在映射程序中如何…实际上Zend Framework不提供任何内置的ORM。只有Zend\u Db\u Table(和Zend\u Db\u Table\u Row)实现了模式 在ZF项目中使用数据模型的常见方法之一是使用。第一个版本的条令(条令1)实现了模式,第二个版本使用 在本节的Zend Framework 1 Quick Start中,描述了数据映射器模式可能的自我开发实现: class

使用Zend(1或2),在模型映射器中抽象公共代码的最佳方法是什么?在控制器/视图中有相关的帮助程序,在映射程序中如何…

实际上Zend Framework不提供任何内置的ORM。只有
Zend\u Db\u Table
(和
Zend\u Db\u Table\u Row
)实现了模式

在ZF项目中使用数据模型的常见方法之一是使用。第一个版本的条令(条令1)实现了模式,第二个版本使用

在本节的Zend Framework 1 Quick Start中,描述了数据映射器模式可能的自我开发实现:

class Application_Model_Guestbook
{
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;

    public function __set($name, $value);
    public function __get($name);

    public function setComment($text);
    public function getComment();

    public function setEmail($email);
    public function getEmail();

    public function setCreated($ts);
    public function getCreated();

    public function setId($id);
    public function getId();
}

class Application_Model_GuestbookMapper
{
    public function save(Application_Model_Guestbook $guestbook);
    public function find($id);
    public function fetchAll();
}

只需在库中或存储公共代码的任何地方创建一个类,例如:

//library/My/Model/Mapper/Abstract.php
<?php

abstract class My_Model_Mapper_Abstract
{
    /**
     * Instance of Zend_Db_Table_Abstract
     *
     * @var Zend_Db_Table_Abstract $tableGateway
     */
    protected $tableGateway = null;
    /**
     * sets up the identity map
     */
    protected $map          = array();

    /**
     * Will accept a DbTable model passed or will instantiate
     * a Zend_Db_Table_Abstract object from table name.
     *
     * @param Zend_Db_Table_Abstract $tableGateway
     */
    public function __construct(Zend_Db_Table_Abstract $tableGateway = null)
    {
        if (is_null($tableGateway)) {
            $this->tableGateway = new Zend_Db_Table($this->tableName);
        } else {
            $this->tableGateway = $tableGateway;
        }
    }

    /**
     * Get the default database table adapter.
     *
     * @return Zend_Db_Table_Abstract
     */
    protected function getGateway()
    {
        return $this->tableGateway;
    }

    //truncated for space

    public function findById($id)
    {
        if ($this->getMap($id)) {
            return $this->getMap($id);
        }

        $select = $this->getGateway()->select();
        $select->where('id = ?', $id);

        $row = $this->getGateway()->fetchRow($select);

        $entity = $this->createEntity($row);

        $this->setMap($row->id, $entity);

        return $entity;
    }

    //truncated for space

    /**
     * Abstract method to be implemented by concrete mappers.
     */
    abstract protected function createEntity($row);
}
//library/My/Model/Mapper/Abstract.php

我倾向于以两个示例为基础绘制地图:

  • 帕德雷克·布雷迪
  • Padraic允许每个映射器组合其他映射器,保持代码干燥,并将每个映射器的焦点精确地放在它需要了解的数据库字段上

    Dasprid使用一个基本映射器类来包含处理db连接的公共代码

    总之,应该有足够的地方保存通用代码


    当然,通过配置来管理其中的大部分内容。在我自己做了几次半满意的绘图尝试之后,我发现自己对学说更满意。

    水合器看起来是一个有趣的解决方案

    谢谢,但这并不能回答我的问题。在映射程序中,有用于多个映射的重复代码,提取此代码的最佳方法是什么?它可以在多个映射程序中的多个位置使用?谢谢您的回答。我希望使用设计模式而不是私有/受保护的方法在多个位置使用映射。因为它也将在不同的映射类中使用。将其移动到抽象类也不理想。很抱歉,您的答复对我来说毫无意义。例如:\Mapper\Item uses\Entity\Item\Mapper\User uses\Entity\Item。因此,这两个映射器使用相同的实体(以及其他实体)&将包含非常相似的映射代码,即$itemEntity->setName($name)x5+行。我想把这5+行抽象出来,这样所有的制图者都能从这段代码中受益。这不是我说的吗?抽象映射器包含所有映射器通用的所有代码,但允许实现具体实例以允许偏差。此外,我还遗漏了实体方面的内容,因为这不是你的问题。映射器主要由不同的实体设置器/获取器组成,将其移动到父类是不对的-这将滥用继承/污染父类。Dasprid通知我,他的博客的svn repo不再公开访问。(悲伤)
    //application/modules/users/model/mappers/User.php
    <?php
    
    class Users_Model_Mapper_User extends My_Model_Mapper_Abstract
    {
        protected $tableName = 'users';
    
        public function __construct(Zend_Db_Table_Abstract $tableGateway = null)
        {
            if (is_null($tableGateway)) {
                //TODO: inject this resource
                $tableGateway = new Application_Model_DbTable_User();
            } else {
                $tableGateway = $tableGateway;
            }
    
            parent::__construct($tableGateway);
        }
    
        protected function createEntity($row)
        {
            $data = array(
                'id'       => $row->id,
                'username' => $row->username,
                'password' => $row->password
            );
            $user = new Users_Model_User($data);
    
            return $user;
        }
    
        private function hashPassword($password)
        {
            return Password::createPasswordHash($password);
        }
    
        public function saveUser(My_Model_Entity_Abstract $user)
        {
            if (!is_null($user->id)) {
    
                $select = $this->getGateway()->select();
                $select->where('id = ?', $user->id);
    
                $row = $this->getGateway()->fetchRow($select);
            } else {
                $row = $this->getGateway()->createRow();
                $row->password = $this->hashPassword($user->password);
            }
            $row->username = $user->username;
    
            $row->save();
            return $row;
        }
    }