Skip to content

Instantly share code, notes, and snippets.

@zeelot
Created April 6, 2011 18:15
Show Gist options
  • Save zeelot/906199 to your computer and use it in GitHub Desktop.
Save zeelot/906199 to your computer and use it in GitHub Desktop.
Wrapper for ORM models to add view logic on top
<?php defined('SYSPATH') or die('No direct script access.');
class View_Model
{
public static function factory(Model $model)
{
// Get the associated View_ name (ex: View_Model_User)
$class = 'View_'.get_class($model);
if (class_exists($class))
{
$reflection = new ReflectionClass($class);
$instance = $reflection->newInstance($model);
}
else
{
$instance = new View_Model($model);
}
return $instance;
}
protected $_model;
public function __construct(Model $model)
{
$this->_model = $model;
}
public function __get($key)
{
return method_exists($this, $key)
? $this->{$key}()
: $this->_model->{$key};
}
public function __call($method, $args)
{
return call_user_func_array(array($this->_model, $method), $args);
}
public function __isset($column)
{
return ($this->_model->__isset($column) OR
((property_exists($this, $column) OR method_exists($this, $column)) AND
$this->{$column} !== NULL));
}
public function save()
{
throw new Kohana_Exception('Model in read-only mode');
}
public function delete()
{
throw new Kohana_Exception('Model in read-only mode');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment