【问题标题】:ErrorException [ Notice ]: Undefined property: Model_Album::$dbErrorException [注意]:未定义属性:Model_Album::$db
【发布时间】:2016-04-04 17:33:41
【问题描述】:

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

我已将引导程序修改为:

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

相册控制器:

<?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 [注意]:未定义属性: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,
    ),
);

【问题讨论】:

  • 您需要为模型调用父构造函数 public function __construct() { $this->album_table = 'albums'; $this->genre_table = '流派';父::__construct(); }
  • 取消注释父级::__construct();并在函数结束时移动它。

标签: php mysql kohana


【解决方案1】:

尝试使用

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

【讨论】:

  • 现在它说:ErrorException [ 致命错误 ]: Call to a member function select() on null
  • 你能分享你的型号代码吗?还记得在 bootstrap.php 文件中配置数据库连接并启用 ORM 和数据库模块吗?
  • 我已经编辑了 OP 我还编辑了 database.php 'type' => MySQL 'hostname' => 'localhost' 'database' => 'cd_collection' 'username' => 'root' '密码' => 'root ...
  • 您需要在模型类中的任何位置将 $this->db 更改为 $this->_db。
  • 现在我明白了: ErrorException [ Strict ]: Declaration of Model_Album::create() should be compatible with Kohana_ORM::create(Validation $validation = NULL) PD: 我之前激活过 MySQL(我知道它自 php 5.5 起已弃用)
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2016-11-12
  • 2011-03-05
  • 2015-02-27
  • 1970-01-01
相关资源
最近更新 更多