Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-28-2007, 11:47 AM
Junior Member
 
Join Date: Sep 2007
Posts: 1
Default Call to undefined method jar_database::query()

Hi,

This is probably more related to OOP than to databases but here it goes.

I've created a seperate class as a wrapper for my database connections.

Code:
class jar_database {
 public function __construct() {
    require_once 'Zend/Db.php';
    $params = array ( 'host'      => 'localhost',
                              'username' => 'user',
                              'password' => 'pw',
                              'dbname'    => 'db');
    $db = Zend_Db::factory('PDO_MYSQL', $params);
    return $db;
   }
}
Then in another method of another class I try to do this (the file that contains the code for jar_database is fully included):

Code:
    $db = new jar_database();
    $stmt = $db->query('SELECT * FROM events");
When I try to do this I get an error:
Fatal error: Call to undefined method jar_database::query()

I think the problem lies in the way I return the db but I got no real idea how to resolve it. How should I "return" the Zend_Db::factory('PDO_MYSQL', $params)"?

Thanks in advance!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-05-2007, 01:05 AM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 122
Default

you need to extend the Zend_Db class vs. just including it.

Code:
require_once 'Zend/Db.php';

class jar_database extends Zend_Db {
    public function __construct() {
        parent::__construct(); //run the db's original constructor
       
        $params = array ( 'host'      => 'localhost',
                                   'username' => 'user',
                                   'password' => 'pw', 
                                   'dbname'    => 'db');
       $db = Zend_Db::factory('PDO_MYSQL', $params);
       return $db;
    }
}
however, this is still riddled with bad form and may not work...

Why are you trying to create a class for this at all?

Add this to your bootstrap file or init method:
Code:
$params = array ( 'host'      => 'localhost',
                            'username' => 'user',
                            'password' => 'pw', 
                            'dbname'    => 'db');
$db = Zend_Db::factory('PDO_MYSQL', $params);
Zend_Registry::set('db', $db);
then anytime you need the db you can do this:
Code:
$db = Zend_Registry::get('db');
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:43 AM.