gpt4 book ai didi

php - ErrorException [注意]:未定义的属性:Model_Album::$db

转载 作者:行者123 更新时间:2023-11-30 22:22:03 26 4
gpt4 key购买 nike

我正在尝试使用 Kohana 框架 3.3 做一个简单的 MVC 界面。

我已将 Bootstrap 修改为:

'controller' => 'Album',
'action' => 'index',

专辑 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;
}
}

我在去往: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();
}

自举:

<?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',
));

型号:

<?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();
}
}

编辑---

数据库.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,
),
);

最佳答案

尝试使用

$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,
),
);

关于php - ErrorException [注意]:未定义的属性:Model_Album::$db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36409197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com