I always liked some kind of auto generated model to access a database.
I liked it the way Symfony does or the Zend Framework Tool.
But anytime using a generator is also a bit of mess.
The actual Zend Framework quickstart example has a 3 files way of implementing a Model, a Model Class, Model Mapper Class and Model DB Table ClassFew,
so for one DB Table you always have to create 3 classes which look almost similar, most times you just copy and paste those, and change a few things.
So i decided to create an very flexible model class, where you just need to define the properties and nothing more getter and sette methods will be called by magic functions. Even it has export function to an array.
You don“t have to create anything but you can create your own functions for getter and setter methods which would override the default ones.
In later state it could be possible to add a filter configurable in the model, but for first step i would like to keep it as slim as possible.
class Dynmodel_Model
{ protected
$_id;
protected
$_created;
public
function __construct
(array $options =
null) { if (is_array($options)) { $this->
setOptions($options);
} } // // Magic Call function // @param <type> $name // @param <type> $arguments //@return <type> // public
function __call
($name,
$arguments){ if(substr($name,
0,
3)==
"set"){ return $this->__set
(substr($name,
3),
$arguments[0]);
} if(substr($name,
0,
3)==
"get"){ return $this->__get
(substr($name,
3));
} throw
new Exception
('Method '.
$name.
' not Supported');
} // // Magic Setter Class // @param <type> $name // @param <type> $value // public
function __set
($name,
$value) { $method =
'set' .
$name;
$propertyName=
"_".lcfirst
($name);
if(method_exists($this,
$method)){ method_exists($this,
$method);
}elseif (('mapper' ==
$name) || !property_exists
($this,
$propertyName)) { throw
new Exception
('Invalid Property '.
$propertyName);
}elseif(property_exists
($this,
$propertyName)){ $this->
$propertyName=
$value;
} } // // Magic getter class // @param <type> $name // @return <type> // public
function __get
($name) { $method =
'get' .
$name;
$propertyName=
"_".lcfirst
($name);
if(method_exists($this,
$method)){ return $this->
$method();
}elseif (('mapper' ==
$name) || !property_exists
($this,
$propertyName)) { throw
new Exception
('Invalid Property '.
$propertyName);
}else{ return $this->
$propertyName;
} } public
function setOptions
(array $options) { $methods =
get_class_methods($this);
foreach ($options as $key =>
$value) { $method =
'set' .
ucfirst($key);
if (in_array($method,
$methods)) { $this->
$method($value);
} } return $this;
} // // Returning the Objects as Array // @return Array // public
function toArray
(){ $values=
array();
foreach(get_object_vars($this) as $pkey =>
$pvalue){ if(substr($pkey,
0,
1)==
"_"){ $arrkey=
substr($pkey,
1);
$call=
"get".
ucfirst(substr($arrkey,
1));
//calling value through objects get function $values[$arrkey]=
$this->
$call();
} } return $values;
} }
The Danymic DBTable and DynamicMapper class are not tested yet, will post theme here later.