Setting up a database adapter

Source: Internet
Author: User
Tags dsn zend

Setting up a database adapter

ZEND-DB provides a general purpose database abstraction layer. Adapterat it Heart is the, which abstracts common database operations across the variety of drivers we support.

In this guide, we'll document how to configure both a, default adapter as well as multiple adapters (which Useful in architectures that has a cluster of read-only replicated servers and a single writable server of record).

Installing ZEND-DB

First, install zend-db using Composer:

$ composer require zendframework/zend-db

If you is using Zend-component-installer (installed by default with the skeleton application, and optionally for Expressi ve applications), you'll be prompted to install the package configuration.

    • For ZEND-MVC applications, choose either application.config.php or modules.config.php .
    • For expressive applications, choose config/config.php .

If you aren't using the installer, you'll need to manually configure add the component to your application.

    • For ZEND-MVC applications, update your list of modules in either or to add a entry for at the top of the config/application.config.php config/modules.config.php ‘Zend\Db‘ List.
    • For expressive applications, create a new file, config/autoload/zend-db.global.php with the following contents:
<?phpuse Zend\Db\ConfigProvider;return (new ConfigProvider())();
Configuring the default Adapter

Within your service factories, you could retrieve the default adapter from your application container using the class name c1/>:

UseZend\db\adapter\adapterinterface; function  ( $container {return new someserviceobject ( $container -> get (adapterinterface< Span class= "token punctuation" >::class) " ;}       

When installed and configured, the factory associated with  Adapterinterface would look for a top-level   db  key in the configuration, and use it to create a adapter. As an example, the following would connect to a MySQL database using PDO, and the supplied PDO DSN:

return [ ' db ' = > [ ' driver ' =>  ' DSN ' =>  ' mysql:dbname=zf2tutorial;host=localhost '  ],]              

More information on adapter configuration can is found in the Docs forzend\db\adapter.

Configuring named Adapters

Sometimes need multiple adapters. As an example, if your work with a cluster of databases, one could allow to write operations, while another could be read-only.

ZEND-DB provides an abstract factory, Zend\Db\Adapter\AdapterAbstractServiceFactory as this purpose. To use it, you'll need to the create named configuration keys under db.adapters , with the configuration for a adapter:

Return[' DB '=>[' Adapters '=>[' Application\db\writeadapter '=>[' Driver '=>' Pdo ',' DSN '=>' Mysql:dbname=application;host=canonical.example.com ',] ' application\db\readonlyadapter ' => = >  mysql:dbname=application;host=replica.example.com ' ]],]    

You retrieve the database adapters using the keys you define, so ensure they is unique to your application, and Descripti ve of their purpose!

Retrieving named Adapters

Retrieve named adapters in your service factories just as you would another service:

function ($container) { return new SomeServiceObject($container->get(‘Application\Db\ReadOnlyAdapter));}
Using the adapterabstractservicefactory as a factory

Depending on what application container your use, abstract factories may is not available. Alternately, want to reduce lookup time retrieving an adapter from the container (abstract factories is cons Ulted last!). Zend-servicemanager abstract factories work as factories in their own right, and is passed the service name as an argumen T, allowing them to vary their return value based on requested service name. As such, you can add the following service configuration as well:

UseZend\db\adapter\adapterabstractservicefactory;If using ZEND-MVC:' Service_manager '=>[' Factories '=>[' Application\db\writeadapter '=>Adapterabstractservicefactory::Class,] ,],//If using Expressive ' dependencies ' => [ ' factories ' => [=> adapterabstractservicefactory:: Class],]       

Setting up a database adapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.