Loading values into Kohana’s ORM

Often I need to load values from a form into a database using Kohana’s ORM layer. I created a method to ease this.

Put the following in MY_ORM.php

class ORM extends ORM_Core {
	public function load_values($data)
	{
		foreach ($data as $field=>$value)
		{
			if(array_key_exists($field,self::$fields[$this->table]))
			{
				$this->$field=$value;
			}
		}
 
	}
}

In combination with Forge you can do this.

$form=new Forge();
$form->input('title')->add_rules('required');
$form->submit('');
if($form->validate())
{
$article=new Article_Model; //instance of ORM
$article->load_values($form->as_array());
$article->save();
//will set $article->title to the form value
}

Leave a Comment