0

i want to use zend_db standalone cos zend framework is too much for my project but i'm new with it, is it correct to do this:

$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');

    $params = array(
        'host' => 'localhost',
        'username' => 'ss_fraat',
        'password' => 'jos10',
        'dbname' => '_a2hDB',
        'driver_options' => $pdoParams
    );

    try {
        $db = Zend_Db::factory('PDO_MYSQL', $params);
        //set default adapter
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
    } catch (Exception $e) {
        exit($e->getMessage());
    }

    //save Db in registry for later use
    Zend_Registry::set('dbAdapter', $db);

then in any class do this:

$db = Zend_Registry::get('db');
/** quote to avoid sql injection */
$date = $db->quote('1980-01-01');
$sql = 'SELECT * FROM product WHERE name = ' . $date;
$result = $db->query($sql);
$db->query(); //run a query

i really need to do this

Zend_Db_Table_Abstract::setDefaultAdapter($db);

i get this code from a website, is it necessary to use Zend_Db_Table_Abstract if i'm not using the full zend framework, or it is better for example to use this:

$db = Zend_Db::factory( ...options... ); $select = new Zend_Db_Select($db);

what i want is to setup a pdo/mysql connexion in my bootstrap php page and be able to get that db instance in any class without starting a new connexion to execute queries but i'm not sure how to do that use Zend_Db_Table_Abstract or Zend_Db_Select use the registry Zend_Registry::set('dbAdapter', $db) or not thanks a lot

thor weiller
  • 71
  • 1
  • 7

1 Answers1

0

The purpose of Zend_Db_Table_Abstract is so you can create your own model classes based around the Table Data Gateway design pattern. The idea of that pattern is that you have a class that encapsulates all the sql you would need for interfacing with a table. So the assumption is that you will be creating model classes that extend Zend_Db_Table_Abstract for each table. If you are going to do that, then you will want to call Zend_Db_Table_Abstract::setDefaultAdapter($db) in your setup/bootstrap. Recent versions of ZF provide as an alternative a quick way of getting basic functionality without having to create a custom class definition by just instantiating Zend_Db_Table:

$userTable = new Zend_Db_Table('users');

In summary, none of this particularly has to do with the MVC part of the framework, although some people choose to use Zend_db as the basis for db connections and models, instead of using a more fully featured ORM like Doctrine or Propel.

The other code you provided simply illustrates that you do not need to use Zend_Db_Table_Abstract either -- you can simply setup an instance of a Zend_Db_Adapter and use that instance to call query() or its other methods.

gview
  • 14,876
  • 3
  • 46
  • 51