Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 - Database connection

Trying to wrap my head around the new concepts of Zend Framework 2.0.

I'm trying to connect to a database, and to get that connection in a controller or model. Nothing fancy, just the pure connection to run queries against.

So this is my current code:

//module.config.php
return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=DBNAME;host=HOSTNAME,
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
        'username' => 'USERNAME',
        'password' => 'PASSWORD',
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

What am I doing wrong?

like image 475
grubolsch Avatar asked Apr 26 '13 11:04

grubolsch


People also ask

Does Zend own PHP?

Zend is referred to as “The PHP Company,” and for good reason. Zend was founded in 2003 by Andi Gutmans and Zeev Suraski — two of the driving forces behind the historical improvement and widespread adoption of PHP.

How do I run a Zend project in xampp?

Within your XAMPP installation directory (usually C:\xampp), create a new directory named apps\ (if it doesn't already exist). Then, within this new apps\ directory, create a directory to hold your Zend Framework 2 application and its related XAMPP configuration files. In this example, call the directory myapp\.

What is the use of Zend framework in PHP?

Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.


1 Answers

Create db.local.php in your ./config/autoload folder and add the following content

return array(
'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=zenBlog;host=localhost',
    'username'       =>'root',
    'password'      =>'',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),
'service_manager' => array(
    'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),);

in your controller $this->getServiceLocator()->get('db'); to access to database.

like image 188
user773440 Avatar answered Oct 08 '22 18:10

user773440