getting php type from mysql type value
Posted: Wed Sep 27, 2006 1:18 pm
I am building an active record library. In my library, each field in a record is represented by an object. Each object has an array of rules that it must adhere to before $model->save() is allowed. One of the rules it must adhere to is a type-check. The problem is that there are a lot more types in mysql than there are in php. I'm just looking for advice on the best way to convert a type retrieved from mysql to a php type... here is the code if that doesn't make sense....
I was considering just defining an array for each php type and filling them with corresponding mysql types like this:
And then just checking that array to validate... does this seem logical, or do you think there is a better way to accomplish this?
Code: Select all
class Mysql_Model_Field_Rule_type extends Mysql_Model_Field_Rule{
/**
* Type in format retrieved from a DESCRIBE TABLE query ie: "bigint" or "blob"
* the width has been stripped from this value... ie: for int(11) the (11) part is
* stripped before being sent to this class
*/
private $type = '';
/**
* Class Constructor
* @param string
*/
public function __construct($type){
$this->type = $type;
}
/**
* Check if value adheres to this rule
* @param string
* @return bool
*/
public function isValid($value){
// What to do here
}
}Code: Select all
$types = array(
'integer' => array('int', 'bigint', 'smallint', 'etc'),
'string' => array('blob', 'text', 'etc')
);