php - ZF2 + Doctrine: Read data from a database via TableGateway and hydrate Doctrine entity including its related objects with it -
i developing centralized management software our legacy web shops. these shops have been developed third party , quite old, use pretty old practices too. orders , related objects stored in 1 single table, therefore table has lot of fields , quite bloated. our new management system uses relational approach. i. e. have table order
store orders , table address
store addresses. each order
references shipping , billing address via foreign keys. doctrine 2 used handling these entities , relations.
obviously have import orders shops database of our management system. happens directly accessing database of each shop, querying order data , inserting in management system's own database.
i using zf2 tablegateway
retrieve data shops , hydrate entities using doctrine's doctrineobject
hydrator little processing possible. however, out of box doctrineobject
hydrator expects related objects passed in nested arrays. because have not yet figured out if tablegateway
can produce multi dimensional result, have process received data manually before passing hydrator.
// $shop_db configured \zend\db\adapter\adapter // access shop's database $orders_table = new tablegateway(['order' => 'shop_orders'], $shop_db); $order_data = $orders_table ->select( function(select $select) { $select->columns([ // note: renaming source fields // internal equivalent via aliases 'source_order_id' => 'id', 'remarks' => 'customer_remarks', 'created_at' => 'order_date', // prefix shipping address data // "shipping_address_" can // extract 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 data can // digested doctrineobject hydrator foreach($order_data $order) { // ... // extract each element has // prefix "shipping_address_" $order // , copy nested array $order['shipping_address'] // ... }
so, avoid manual processing, can think of 2 possible solutions:
- find way make
tablegatway
return multi dimensional results - find way make
doctrinehydrator
able populate related objects prefixed array elements instead of nested arrays
we have been using propel2 couple of years ago , if remember correctly, propel's hydrator automatically populate related objects single dimensional array via prefixes, quite similar how manually process received data. doctrine hydrator can't far see, though guess use strategies accomplish this.
before dive deep developing strategies or derived tablegateway
, know out-of-the-box solution? or current approach best can get?
i think current process of mapping data format hydrator expects better apposed using naming strategies (because have flat array , hydrator requires nested one).
you create composite hydrator encapsulate mapping.
class myhydrator implements hydratorinterface { // class data conversion protected $mapper; // doctrine hydrator protected $hydrator; public function hydrate($data, $object) { // return data format expected $data = $this->mapper->map($data); return $this->hydrator->hydrate($data, $object); } public function extract($object) { $data = $this->hydrator->extract($object); // reverse data flat structure return $this->mapper->unmap($data); } }
Comments
Post a Comment