Data mapper - need to map an array of values to db w/filters
Posted: Mon Jan 14, 2008 4:35 pm
I am writing an order management console (I think I have mentioned that a few times
). I need to create an import system that is easy to extend. For now, I want to accept an array of values and map them to my data model. The thing is, I need to do things to certain columns:
- I need to filter some of the values (data comes in as YYYY-MM-DDTHH:MM:SS-(TIMEZONE-OFFSET) and it needs to map to Order::date as a YYYY-MM-DD field)
- I need to map parts of an input column to more than one model param (for instance if I get a full name for input--like "Luke Visinoni"--I need a function to break it apart and map it to Order::shipping_first_name and Order::shipping_last_name)
- Sometimes I need to do it the other way too... I need to map multiple input columns to one model param (If I get a shipping fee, a shipping tax, and a shipping discount, I need them added together and mapped to Order::shipping_fee)
I have begun this process, but I'm finding it difficult to come up with a good system that is extensible and easy to understand. I won't always be the one writing the importers, so I'd like it to be pretty straight-forward. Any ideas?
Oh, I should also mention (although it doesn't really matter because the concepts are the same) that I'm using cakephp. I should also mention that many times the data will map to several different models. For instance, the following would map to 3 seperate models (Order, OrderItem, and OrderCharge, and possibly even Customer although we aren't using it here):
I am not looking for anybody to write any code for me. I'm simply asking for inspiration. What design patterns would you use here? Why?
- I need to filter some of the values (data comes in as YYYY-MM-DDTHH:MM:SS-(TIMEZONE-OFFSET) and it needs to map to Order::date as a YYYY-MM-DD field)
- I need to map parts of an input column to more than one model param (for instance if I get a full name for input--like "Luke Visinoni"--I need a function to break it apart and map it to Order::shipping_first_name and Order::shipping_last_name)
- Sometimes I need to do it the other way too... I need to map multiple input columns to one model param (If I get a shipping fee, a shipping tax, and a shipping discount, I need them added together and mapped to Order::shipping_fee)
I have begun this process, but I'm finding it difficult to come up with a good system that is extensible and easy to understand. I won't always be the one writing the importers, so I'd like it to be pretty straight-forward. Any ideas?
Oh, I should also mention (although it doesn't really matter because the concepts are the same) that I'm using cakephp. I should also mention that many times the data will map to several different models. For instance, the following would map to 3 seperate models (Order, OrderItem, and OrderCharge, and possibly even Customer although we aren't using it here):
Code: Select all
abstract class MC2_Importer
{
/**
* Maps fields to their respective function / value in this class
*/
protected $_map = array();
/**
* Holds field values
*/
protected $_data = array ();
public function __construct($data) {
$this->_data = $data;
}
/**
* Maps input columns to functions / values
*/
public function _map() {
}
}
class MC2_Amazon_Importer extends MC2_Importer
{
protected $_map = array(
'order-id' => '',
'order-item-id' => '',
'purchase-date' => '',
'payments-date' => '',
'buyer-email' => '',
'buyer-name' => '',
'buyer-phone-number' => '',
'sku' => '',
'product-name' => '',
'quantity-purchased' => '',
'item-price' => '',
'item-tax' => '',
'shipping-price' => '',
'shipping-tax' => '',
'ship-service-level' => '',
'recepient-name' => '',
'ship-address-1' => '',
'ship-address-2' => '',
'ship-address-3' => '',
'ship-city' => '',
'ship-state' => '',
'ship-postal-code' => '',
'ship-country' => '',
'item-promotion-discount' => '',
'item-promotion-id' => '',
'ship-promotion-discount' => '',
'ship-promotion-id' => '',
);
}