I'm trying to do a simple MVC
interface using Kohana framework 3.3.
I've modified the bootstrap to:
'controller' => 'Album',
'action' => 'index',
album controller:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Controller_Album extends Controller
{
private $album_model;
private $genre_model;
private $list_view;
private $create_view;
private $update_view;
public function __construct(Request $request, Response $response)
{
parent::__construct($request, $response);
$this->album_model = new Model_Album;
$this->genre_model = new Model_Genre;
$this->list_view = new View('List');
$this->update_view = new View('Update');
$this->create_view = new View('Create');
}
public function action_index()
{
$this->show_albums_list();
}
private function show_albums_list()
{
$albums_list = $this->album_model->get_list();
$this->list_view->set('albums_list',$albums_list);
$this->list_view->render(TRUE);
}
public function show_create_editor()
{
$this->create_view->set('genres_list',$this->get_genres_list());
$this->create_view->render(TRUE);
}
public function show_update_editor($id)
{
$album_data = $this->album_model->read($id);
$this->update_view->set('album_id',$album_data[0]->id);
$this->update_view->set('name',$album_data[0]->name);
$this->update_view->set('author',$album_data[0]->author);
$this->update_view->set('genre_id',$album_data[0]->genre_id);
$this->update_view->set('genres_list',$this->get_genres_list());
$this->update_view->render(TRUE);
}
public function create()
{
$album_data=array(
'name' => $this->input->post('name'),
'author' => $this->input->post('author'),
'genre_id' => $this->input->post('genre_id')
);
$this->album_model->create($album_data);
url::redirect('Album');
}
public function update()
{
$album_data = array(
'name' => $this->input->post('name'),
'author' => $this->input->post('author'),
'genre_id' => $this->input->post('genre_id')
);
$this->album_model->update($this->input->post('album_id'),$album_data);
url::redirect('Album');
}
public function delete($id)
{
$this->album_model->delete($id);
url::redirect('Album');
}
private function get_genres_list()
{
$db_genres_list = $this->genre_model->get_list();
$genres_list = array();
if(sizeof($db_genres_list) >= 1)
{
foreach($db_genres_list as $item)
{
$genres_list[$item->id] = $item->name;
}
}
return $genres_list;
}
}
And I get this error when going to: http://localhost/kohana/
ErrorException [ Notice ]: Undefined property: Model_Album::$db
APPPATH\classes\Model\Album.php [ 39 ]
public function get_list()
{
//error in the next line (39)
$this->db->select('albums.id as id,albums.name as name,albums.author as author, genres.name as genre');
$this->db->from($this->album_table);
$this->db->join($this->genre_table,'genres.id','albums.genre_id');
$query = $this->db->get();
return $query->result_array();
}
bootstrap:
<?php defined('SYSPATH') or die('No direct script access.');
// -- Environment setup --------------------------------------------------------
// Load the core Kohana class
require SYSPATH.'classes/Kohana/Core'.EXT;
if (is_file(APPPATH.'classes/Kohana'.EXT))
{
// Application extends the core
require APPPATH.'classes/Kohana'.EXT;
}
else
{
// Load empty core extension
require SYSPATH.'classes/Kohana'.EXT;
}
date_default_timezone_set('America/Lima');
setlocale(LC_ALL, 'en_US.utf-8');
spl_autoload_register(array('Kohana', 'auto_load'));
//spl_autoload_register(array('Kohana', 'auto_load_lowercase'));
/**
* Enable the Kohana auto-loader for unserialization.
*
* @link http://www.php.net/manual/function.spl-autoload-call
* @link http://www.php.net/manual/var.configuration#unserialize-callback-func
*/
ini_set('unserialize_callback_func', 'spl_autoload_call');
mb_substitute_character('none');
// -- Configuration and initialization -----------------------------------------
I18n::lang('en-us');
if (isset($_SERVER['SERVER_PROTOCOL']))
{
// Replace the default protocol.
HTTP::$protocol = $_SERVER['SERVER_PROTOCOL'];
}
if (isset($_SERVER['KOHANA_ENV']))
{
Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}
Kohana::init(array(
'base_url' => '/kohana/',
));
Kohana::$log->attach(new Log_File(APPPATH.'logs'));
Kohana::$config->attach(new Config_File);
Kohana::modules(array(
// 'auth' => MODPATH.'auth', // Basic authentication
// 'cache' => MODPATH.'cache', // Caching with multiple backends
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
// 'minion' => MODPATH.'minion', // CLI Tasks
'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'unittest' => MODPATH.'unittest', // Unit testing
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
));
// Cookie::$salt = NULL;
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'Album',
'action' => 'index',
));
Model:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Model_Album extends Model_Database
{
private $album_table;
private $genre_table;
public function __construct()
{
//parent::__construct();
$this->album_table = 'albums';
$this->genre_table = 'genres';
}
public function read($id)
{
$this->db->where('id', $id);
$query = $this->db->get($this->album_table);
return $query->result_array();
}
public function delete($id)
{
$this->db->delete($this->album_table, array('id' => $id));
}
public function update($id,$data)
{
$this->db->update($this->album_table, $data, array('id' => $id));
}
public function create($data)
{
$this->db->insert($this->album_table, $data);
}
public function get_list()
{
$this->_db->select('albums.id as id,albums.name as name,albums.author as author, genres.name as genre');
$this->db->from($this->album_table);
$this->db->join($this->genre_table,'genres.id','albums.genre_id');
$query = $this->db->get();
return $query->result_array();
}
}
EDIT---
database.php
<?php defined('SYSPATH') OR die('No direct access allowed.');
return array
(
'default2' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'cd_collection',
'username' => 'root',
'password' => 'root',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
'alternate' => array(
'type' => 'PDO',
'connection' => array(
'dsn' => 'mysql:host=localhost;dbname=kohana',
'username' => 'root',
'password' => 'r00tdb',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
'default' => array
(
'type' => 'MySQLi',
'connection' => array(
'hostname' => 'localhost',
'database' => 'cd_collection',
'username' => 'root',
'password' => 'root',
'persistent' => FALSE,
'ssl' => NULL,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
);
答案 0 :(得分:0)
尝试使用
$this->_db
而不是
$this->db
Model_Database 类没有属性 $ db 。您可以找到更多信息here
编辑: 在您的模型类中更改:
class Model_Album extends Model_Database
到
class Model_Album extends ORM
将 database.php 文件放在 application / config / 目录中:
return array
(
'default' => array
(
'type' => 'MySQLi',
'connection' => array(
/**
* The following options are available for MySQL:
*
* string hostname server hostname, or socket
* string database database name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => 'localhost',
'database' => 'db_name',
'username' => 'db_user_name',
'password' => 'db_user_password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
);