Value Object with a set of possible values from a db
Posted: Mon Sep 17, 2007 8:32 am
I have been working on creating some value objects for the system I am working on and have a datetime one, a money object, and a few others, but now I have one that is called ShipType and I don't know how to handle it.
I was thinking methods something like this
I set it up so you could set the type with either the shipping abbreviation (DHL2, DHLR, PMD) or by ID. The reason for that is if we have orders imported from a CSV file or from XML the ship abbreviation will be sent, but when the order is stored in the DB then the ID is saved. Then when the order is pulled I can pull the ID and set the value with this object. As I write this I suppose that making the abbreviation be the primary key is probably the way to go, so I think I will change that.
Now here is the puzzle. In order to do validation and mapping between IDs and abbreviations, I need to do a query to pull the ship types from the database and store them in a static array within this class or some other method.
Is there a clean way to do this without having to inject the db connection or a registry into this value object? I really don't want value objects to have dependencies such as that.
I was thinking methods something like this
Code: Select all
interface FCP_ShipType
{
const FROM_ID = 1 ;
const FROM_ABBRV = 2 ;
/**
* Set the ship type
*
* @param mixed $shipType Either a ship type ID or abbreviation
*/
public function set($shipType) ;
/**
* Constructor
*
* @param mixed $shipType Either a ship type ID or abbreviation
*/
public function __construct($shipType) ;
/**
* Get the primary key of the ship type
*
* @return int
*/
public function getID() ;
/**
* Get the description for ship type
*
* @return string
*/
public function getDescription() ;
/**
* Get the abbreviation for this ship type
*
* @return string
*/
public function getAbbrv() ;
/**
* Get the carrier for this ship type
*
* @return string
*/
public function getCarrier() ;
/**
* Check if this object contains a valid ship type
*
* @return bool
*/
public function isValid() ;
}
// use
$shipType = new FCP_ShipType('PMD') ;
$otherShipType = new FCP_ShipType(3) ; // use the database ID
echo $shipType->getCarrier() ; // USPS
echo $shipType->getDescription() ; // Priority mail w/delivery confirmation
echo $otherShipType->getDescription() ; // DHL ground @home
// use as a member of another object
$order->getShipType()->getAbbrv() ;Now here is the puzzle. In order to do validation and mapping between IDs and abbreviations, I need to do a query to pull the ship types from the database and store them in a static array within this class or some other method.
Is there a clean way to do this without having to inject the db connection or a registry into this value object? I really don't want value objects to have dependencies such as that.