Created
December 29, 2010 18:11
-
-
Save lukemorton/758822 to your computer and use it in GitHub Desktop.
Information on using ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Information on getting started with ORM | |
* | |
* [!!] You must initialise ORM in your bootstrap | |
* | |
* @see http://kohanaframework.org/guide/tutorials.orm | |
* @see http://kohanaframework.org/guide/security.validation | |
* @see http://kohanaframework.org/guide/security.database | |
* @see https://github.com/kohana/orm/blob/3.0.8/classes/kohana/orm.php | |
*/ | |
// config/databases.php | |
return array( | |
'default' => array( | |
'type' => 'mysql', | |
'connection' => array( | |
'persistent' => FALSE, | |
), | |
'table_prefix' => '', | |
'charset' => 'latin-1', | |
'profiling' => FALSE, | |
), | |
'data_systems' => array( | |
'connection' => array( | |
'hostname' => 'localhost', | |
'username' => 'dbuser', | |
'password' => 'mypassword', | |
'database' => 'db', | |
), | |
) | |
); | |
// classes/model/task.php | |
class Model_Task extends ORM { | |
protected $_db = 'data_systems'; | |
protected $_table_name = 'something_tasks'; // if this is not set "tasks" is the table name | |
protected $_rules = array( | |
'username' => array( | |
'not_empty' => NULL, | |
) | |
); | |
} | |
// classes/controller/task.php | |
class Controller_Task extends Controller { | |
public function action_new() | |
{ | |
$view = Kostache::factory('task/new'); | |
if ($_POST) | |
{ | |
$task = ORM::factory('task'); | |
// Success | |
if ($task->values($_POST)->check()) | |
{ | |
$this->request->redirect($this->request->uri(array( | |
'action' => 'edit', | |
'id' => $task->pk(), | |
))); | |
} | |
// Error | |
$view->set('errors', $task->validate()->errors()); | |
} | |
$this->request->response = $view; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment