11# AdapterAwareTrait
22
3- The trait ` PhpDb\Adapter\AdapterAwareTrait ` , which provides implementation
4- for ` PhpDb\Adapter\AdapterAwareInterface ` , and allowed removal of
5- duplicated implementations in several components of Laminas or in custom
6- applications.
7-
8- The interface defines only the method ` setDbAdapter() ` with one parameter for an
9- instance of ` PhpDb\Adapter\Adapter ` :
3+ ` PhpDb\Adapter\AdapterAwareTrait ` provides a standard implementation of
4+ ` AdapterAwareInterface ` for injecting database adapters into your classes.
105
116``` php
127public function setDbAdapter(\PhpDb\Adapter\Adapter $adapter) : self;
138```
149
1510## Basic Usage
1611
17- ### Create Class and Add Trait
18-
1912``` php
2013use PhpDb\Adapter\AdapterAwareTrait;
2114use PhpDb\Adapter\AdapterAwareInterface;
@@ -24,28 +17,17 @@ class Example implements AdapterAwareInterface
2417{
2518 use AdapterAwareTrait;
2619}
27- ```
28-
29- ### Create and Set Adapter
30-
31- [ Create a database adapter] ( ../adapter.md#creating-an-adapter-using-configuration ) and set the adapter to the instance of the ` Example `
32- class:
33-
34- ``` php
35- $adapter = new PhpDb\Adapter\Adapter([
36- 'driver' => 'Pdo_Sqlite',
37- 'database' => 'path/to/sqlite.db',
38- ]);
3920
21+ // Set adapter (see adapter.md for creation)
4022$example = new Example();
41- $example->setAdapter ($adapter);
23+ $example->setDbAdapter ($adapter);
4224```
4325
4426## AdapterServiceDelegator
4527
4628The [ delegator] ( https://docs.laminas.dev/laminas-servicemanager/delegators/ )
47- ` PhpDb\Adapter\AdapterServiceDelegator ` can be used to set a database
48- adapter via the [ service manager of laminas-servicemanager] ( https://docs.laminas.dev/laminas-servicemanager/quick-start/ ) .
29+ ` PhpDb\Adapter\AdapterServiceDelegator ` can be used to set a database adapter
30+ via the [ service manager of laminas-servicemanager] ( https://docs.laminas.dev/laminas-servicemanager/quick-start/ ) .
4931
5032The delegator tries to fetch a database adapter via the name
5133` PhpDb\Adapter\AdapterInterface ` from the service container and sets the
@@ -54,8 +36,9 @@ adapter to the requested service. The adapter itself must be an instance of
5436
5537> ### Integration for Mezzio and laminas-mvc based Applications
5638>
57- > In a Mezzio or laminas-mvc based application the database adapter is already
58- > registered during the installation with the laminas-component-installer.
39+ > In a Mezzio or laminas-mvc based application the database adapter is
40+ > already registered during the installation with the
41+ > laminas-component-installer.
5942
6043### Create Class and Use Trait
6144
@@ -80,23 +63,29 @@ class Example implements AdapterAwareInterface
8063
8164### Create and Configure Service Manager
8265
83- Create and [ configured the service manager] ( https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/ ) :
66+ Create and [ configure the service manager] (
67+ https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/ ):
8468
8569``` php
86- use Interop\Container\ContainerInterface;
70+ use Psr\Container\ContainerInterface;
71+ use PhpDb\Adapter\Adapter;
8772use PhpDb\Adapter\AdapterInterface;
8873use PhpDb\Adapter\AdapterServiceDelegator;
8974use PhpDb\Adapter\AdapterAwareTrait;
9075use PhpDb\Adapter\AdapterAwareInterface;
76+ use PhpDb\Sqlite\Driver\Sqlite;
77+ use PhpDb\Sqlite\Platform\Sqlite as SqlitePlatform;
9178
9279$serviceManager = new Laminas\ServiceManager\ServiceManager([
9380 'factories' => [
9481 // Database adapter
95- AdapterInterface::class => static function(ContainerInterface $container) {
96- return new PhpDb\Adapter\Adapter([
97- 'driver' => 'Pdo_Sqlite',
82+ AdapterInterface::class => static function(
83+ ContainerInterface $container
84+ ) {
85+ $driver = new Sqlite([
9886 'database' => 'path/to/sqlite.db',
9987 ]);
88+ return new Adapter($driver, new SqlitePlatform());
10089 }
10190 ],
10291 'invokables' => [
@@ -114,18 +103,19 @@ $serviceManager = new Laminas\ServiceManager\ServiceManager([
114103
115104### Get Instance of Class
116105
117- [ Retrieving an instance] ( https://docs.laminas.dev/laminas-servicemanager/quick-start/#3-retrieving-objects )
106+ [ Retrieving an instance] (
107+ https://docs.laminas.dev/laminas-servicemanager/quick-start/#3-retrieving-objects )
118108of the ` Example ` class with a database adapter:
119109
120110``` php
121111/** @var Example $example */
122112$example = $serviceManager->get(Example::class);
123113
124- var_dump($example->getAdapter() instanceof PhpDb\Adapter\Adapter); // true
114+ var_dump(
115+ $example->getAdapter() instanceof PhpDb\Adapter\AdapterInterface
116+ ); // true
125117```
126118
127- ## Concrete Implementations
128-
129- The validators [ ` Db\RecordExists ` and ` Db\NoRecordExists ` ] ( https://docs.laminas.dev/laminas-validator/validators/db/ )
130- implements the trait and the plugin manager of [ laminas-validator] ( https://docs.laminas.dev/laminas-validator/ )
131- includes the delegator to set the database adapter for both validators.
119+ The [ laminas-validator] (
120+ https://docs.laminas.dev/laminas-validator/validators/db/ )
121+ ` Db\RecordExists ` and ` Db\NoRecordExists ` validators use this pattern.
0 commit comments