- Code: Select all
abstract class DooModel{
public function __construct($assocArray = NULL) {
if ($assocArray != NULL) {
$this->update($assocArray);
}
}
public function update($assocArray = NULL) {
$myAttributes = get_object_vars($this);
foreach ($myAttributes as $attribute => $value) {
if( isset($assocArray[$attribute]) ) {
$this->$attribute = $assocArray[$attribute];
}
}
}
public function save() {
return $this->db()->insert($this);
}
public function delete() {
return $this->db()->delete($this);
}
public function getById() {
return Doo::db()->find($this, array('limit'=>1));
}
public function getAll() {
return Doo::db()->find(get_class($this));
}
public function getByField($fieldName, $fieldValue) {
// to do
}
}
So as you can see this will simplify retrieving, deleting and saving objects. Also you can create object from a associative array which could be your form data.
so instead of saying $article = new Article; $article->name = $_POST['name']; $article->text = $_POST['text']; you can say $article = new Article($_POST);
Also right here we can implement caching (this is another issue).
DB Caching
Concept is simple for now,
When u getById, cache result, when u delete/save, invalidate the entry
Waiting for feedback.
