public function action_new()
{
$view = new View_Admin_Post_New;
$view->post_obj = $post = new Model_Post;
if ($_POST)
{
$post->values(arr::extract($_POST, array('title', 'text')));
$post->user_id = Auth::instance()->get_user()->id;
$post->generate_slug();
if ($post->check())
{
$post->save();
$this->request->redirect('admin/posts');
}
}
$this->request->response = $view;
}
public $post_obj;
public function errors()
{
return $this->post_obj->validate()->errors();
}
public function post_title()
{
return $this->post_obj->title;
}
public function action_foo(){
// This instantiates the Model
Model::instance('model/foo')
->set_state('limit', 5)
->set_state('ordering', 'DESC');
}
class Views_Foo extends Kostache{
public function categories(){
// This does not instantiate the model, but gets the instance created by the controller.
return Model::instance('model/foo')->get_list();
}
}
$this->_partials += array('name'=>'path');
public function before()
{
parent::before();
$this->_partials += array('name'=>'path');
}
public function __construct($template = null, $view = null, $partials = null)
{
parent::__construct($template, $view, $partials);
$this->_partials += array('name'=>'path');
}
class Model extends Kohana_Model {
public static function instance($path, $namespace = 'default')
{
static $instances;
// If instances is null, that means we have to inialize it as an array
if (is_null($instances))
{
$instances = array($namespace => array());
}
// Let's get the class name based on the path
$classname = str_replace('/', '_', $path);
if (class_exists($classname))
{
if (isset($instances[$namespace][$path]) AND $instances[$namespace][$path] instanceof $classname) {
return $instances[$namespace][$path];
}
return $instances[$namespace][$path] = new $classname;
}
else
{
throw new Kohana_Exception('Class :classname does not exist',
array(':classname' => $classname));
}
}
}
class Model_Test {
protected $calls = 0;
public function hello()
{
$this->calls++;
return 'called me '.$this->calls.'x';
}
}
echo Model::instance('model/test')->hello();
echo Model::instance('model/test')->hello();
echo Model::instance('model/test')->hello();
Model::instance('test')->hello(); It looks like you're new here. If you want to get involved, click one of these buttons!