Models are classes designed to work with information transmitted or requested by the controller. For example, if you have a guest book, the controller queries the model for the last ten entries; the model returns them; the controller transmits this data to mind. The controller can also add new records using the model and update or delete existing ones.
Kohana::modules(array( // 'auth' => MODPATH.'auth', // Basic authentication // 'codebench' => MODPATH.'codebench', // Benchmarking tool 'database' => MODPATH.'database', // Database access // 'image' => MODPATH.'image', // Image manipulation // 'orm' => MODPATH.'orm', // Object Relationship Mapping // 'pagination' => MODPATH.'pagination', // Paging of results // 'userguide' => MODPATH.'userguide', // User guide and API documentation ));
<?php defined('SYSPATH') OR die('No direct access allowed.'); return array ( 'default' => array ( 'type' => 'mysql', 'connection' => array( /** * The following options are available for MySQL: * * string hostname * integer port * string socket * string username * string password * boolean persistent * string database */ 'hostname' => 'localhost', 'username' => 'root', 'password' => FALSE, 'persistent' => FALSE, 'database' => 'mykohana3', ), 'table_prefix' => '', 'charset' => 'utf8', 'caching' => FALSE, 'profiling' => TRUE, ), 'alternate' => array( 'type' => 'pdo', 'connection' => array( /** * The following options are available for PDO: * * string dsn * string username * string password * boolean persistent * string identifier */ 'dsn' => 'mysql:host=localhost;dbname=mykohana3', 'username' => 'root', 'password' => FALSE, 'persistent' => FALSE, ), 'table_prefix' => '', 'charset' => 'utf8', 'caching' => FALSE, 'profiling' => TRUE, ), );
CREATE TABLE `posts` ( `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) DEFAULT NULL, `post` TEXT, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
<?php defined('SYSPATH') or die('No direct script access.'); class Model_Post extends Kohana_Model { /** * Get the last 10 posts * @return ARRAY */ public function getLastTenPosts() { $sql = 'SELECT *'."\n". 'FROM `posts`'."\n". 'ORDER BY `id` DESC'."\n". 'LIMIT 0, 10'; return $this->_db->query(Database::SELECT, $sql, FALSE) ->as_array(); } }
$sql = 'SELECT *'."\n". 'FROM `posts`'."\n". 'ORDER BY `id` DESC'."\n". 'LIMIT 0, 10';
return $this->_db->query(Database::SELECT, $sql, FALSE) ->as_array();
public function action_posts() { $posts = new Model_Post(); $ko3 = array(); $this->template->title = 'Kohana 3.0 Model Test'; $this->template->meta_keywords = 'PHP, Kohana, KO3, Framework, Model'; $this->template->meta_description = 'A test of of the KO3 framework Model'; $this->template->styles = array(); $this->template->scripts = array(); // Get the last 10 posts $ko3['posts'] = $posts->getLastTenPosts(); $this->template->content = View::factory('pages/posts', $ko3); }
<?php foreach($posts as $post):?> <h1><?php echo $post['title'];?></h1> <?php echo $post['post'];?> <hr /> <?php endforeach;?>
insert into `posts`(`id`,`title`,`post`) values (1,'Test Post','This is some sample text.'); insert into `posts`(`id`,`title`,`post`) values (2,'Another post','Some more text');
public function addPost($title, $post) { $sql = sprintf('INSERT INTO `posts`'."\n". 'SET `title` = %s,'."\n". ' `post` = %s', $this->_db->escape($title), $this->_db->escape($post)); $this->_db->query(Database::INSERT, $sql, FALSE); }
<?php if(!empty($msg)):?> <?php echo $msg.'<br />';?> <?php endif;?> <?php foreach($posts as $post):?> <h1><?php echo $post['title'];?></h1> <?php echo $post['post'];?> <hr /> <?php endforeach;?> <form method="POST" action="<?php echo url::base();?>ko3/posts/"> <table> <tr> <td> Title </td> <td> <input type="text" name="title" style="border: 1px solid #000000;"/> </td> </tr> <tr> <td> Post </td> <td> <textarea cols="20" rows="5" name="post"></textarea> <input type="submit" name="submit" value="Submit"/> </td> </table> </form>
private function _addPost($title, $post_content) { // Load model $post = new Model_Post(); // Check required fields if(empty($title)) { return(array('error' => 'Please enter a title.')); } elseif(empty($post_content)) { return(array('error' => 'Please enter a post.')); } // Add to DB $post->addPost($title, $post_content); return TRUE; }
public function action_posts() { // Load model $posts = new Model_Post(); // Setup view stuff $ko3 = array(); $this->template->title = 'Kohana 3.0 Model Test'; $this->template->meta_keywords = 'PHP, Kohana, KO3, Framework, Model'; $this->template->meta_description = 'A test of of the KO3 framework Model'; $this->template->styles = array(); $this->template->scripts = array(); $ko3['msg'] = ""; // Handle POST if($_POST) { $ret = $this->_addPost((isset($_POST['title']) ? $_POST['title'] : ""), (isset($_POST['post']) ? $_POST['post'] : "")); if(isset($ret['error'])) { $ko3['msg'] = $ret['error']; } else { $ko3['msg'] = 'Saved.'; } } // Get the last 10 posts $ko3['posts'] = $posts->getLastTenPosts(); // Display it. $this->template->content = View::factory('pages/posts', $ko3); }
public function addPost($title, $post) { DB::insert('posts', array('title','post')) ->values(array($title, $post)) ->execute(); }
Source: https://habr.com/ru/post/111871/
All Articles