Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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 ZF2和x2B;条令:通过TableGateway从数据库读取数据,并创建条令实体,包括与其相关的对象_Php_Doctrine Orm_Zend Framework2 - Fatal编程技术网

Php ZF2和x2B;条令:通过TableGateway从数据库读取数据,并创建条令实体,包括与其相关的对象

Php ZF2和x2B;条令:通过TableGateway从数据库读取数据,并创建条令实体,包括与其相关的对象,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,我正在为我们的传统网络商店开发一个集中管理软件。这些商店是由第三方开发的,非常古老,所以它们也使用非常古老的做法。订单及其相关对象存储在一个表中,因此该表有很多字段,并且非常臃肿。我们的新管理系统使用关系方法。即。我有一个表order来存储订单,还有一个表address来存储所有地址。每个订单通过外键引用其发货和账单地址。条令2用于处理这些实体及其关系 很明显,我必须将商店的订单导入我们管理系统的数据库。这是通过直接访问每个商店的数据库、查询订单数据并将其插入管理系统自己的数据库来实现的 我正在

我正在为我们的传统网络商店开发一个集中管理软件。这些商店是由第三方开发的,非常古老,所以它们也使用非常古老的做法。订单及其相关对象存储在一个表中,因此该表有很多字段,并且非常臃肿。我们的新管理系统使用关系方法。即。我有一个表
order
来存储订单,还有一个表
address
来存储所有地址。每个
订单
通过外键引用其发货和账单地址。条令2用于处理这些实体及其关系

很明显,我必须将商店的订单导入我们管理系统的数据库。这是通过直接访问每个商店的数据库、查询订单数据并将其插入管理系统自己的数据库来实现的

我正在使用ZF2
TableGateway
从商店检索数据,我希望使用Doctrine的
DoctrineObject
hydrator尽可能少地处理我的实体。但是,
DoctrineObject
doctor希望相关对象以嵌套数组的形式传递。因为我还没有弄清楚
TableGateway
是否可以产生多维结果,所以我必须在将接收到的数据传递给编辑器之前手动处理这些数据

// $shop_db is a configured \Zend\Db\Adapter\Adapter to
// access the shop's database
$orders_table = new TableGateway(['order' => 'shop_orders'], $shop_db);

$order_data = $orders_table
    ->select(
        function(Select $select) {
            $select->columns([
                // Note: I am renaming the source fields to 
                // their internal equivalent via aliases
                'source_order_id' => 'id',
                'remarks' => 'customer_remarks',
                'created_at' => 'order_date',
                // Prefix the shipping address data with
                // "shipping_address_" so we can easily 
                // extract it later
                'shipping_address_firstname' => 'safirst',
                'shipping_address_lastname' => 'salast',
                'shipping_address_company' => 'sacomp',
                'shipping_address_street' => 'sastreet',
                'shipping_address_zip_code' => 'sazip',
                'shipping_address_city' => 'sacity'
            ]);
            $select->order('id');
        }
    );

// Process each order so its data can be
// digested by the DoctrineObject hydrator
foreach($order_data as $order)
{
    // ...
    // extract each element that has the 
    // prefix "shipping_address_" from the $order
    // and copy it to a nested array $order['shipping_address']
    // ...
}
因此,为了避免手动处理,我可以想出两种可能的解决方案:

  • 找到一种使
    表格通道
    返回多维结果的方法
  • 找到一种方法,使
    DoctrineHydrator
    能够通过前缀数组元素而不是嵌套数组填充相关对象
  • 几年前,我们一直在使用Propel2,如果我没记错的话,Propel的水合器可以通过前缀从一维数组中自动填充相关对象,这与我手动处理接收到的数据的方式非常相似。就我所知,这个人不能做到这一点,尽管我想你可以用策略来实现这一点


    在我深入研究开发策略或衍生的
    TableGateway
    之前,有人知道现成的解决方案吗?或者我当前的方法是最好的吗?

    我认为,与使用命名策略相比,您当前将数据映射到Hydrotor期望的格式的过程会更好(因为您有一个平面数组,Hydrotor需要一个嵌套数组)

    您甚至可以创建一个复合映射器来封装映射

    class MyHydrator implements HydratorInterface
    {
        // The class that does the data conversion
        protected $mapper;
    
        // The doctrine hydrator
        protected $hydrator;
    
        public function hydrate($data, $object)
        {
            // Return the data to the format that is expected
            $data = $this->mapper->map($data);
    
            return $this->hydrator->hydrate($data, $object);
        }
    
        public function extract($object)
        {
            $data = $this->hydrator->extract($object);
    
            // Reverse the data into a flat structure
            return $this->mapper->unmap($data);
        }
    }
    

    我现在采取了这种方法,我的其他想法对于手头的任务来说确实太复杂了