Skip to content

Commit ae1dc78

Browse files
froschdesignweierophinney
authored andcommitted
Adds documentation for AdapterAwareTrait
Signed-off-by: Frank Brückner <[email protected]>
1 parent 9b66104 commit ae1dc78

File tree

2 files changed

+135
-10
lines changed

2 files changed

+135
-10
lines changed
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# AdapterAwareTrait
2+
3+
The trait `Laminas\Db\Adapter\AdapterAwareTrait`, which provides implementation
4+
for `Laminas\Db\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 `Laminas\Db\Adapter\Adapter`:
10+
11+
```php
12+
public function setDbAdapter(\Laminas\Db\Adapter\Adapter $adapter) : self;
13+
```
14+
15+
## Basic Usage
16+
17+
### Create Class and Add Trait
18+
19+
```php
20+
use Laminas\Db\Adapter\AdapterAwareTrait;
21+
use Laminas\Db\Adapter\AdapterAwareInterface;
22+
23+
class Example implements AdapterAwareInterface
24+
{
25+
use AdapterAwareTrait;
26+
}
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 Laminas\Db\Adapter\Adapter([
36+
'driver' => 'Pdo_Sqlite',
37+
'database' => 'path/to/sqlite.db',
38+
]);
39+
40+
$example = new Example();
41+
$example->setAdapter($adapter);
42+
```
43+
44+
## AdapterServiceDelegator
45+
46+
The [delegator](https://docs.laminas.dev/laminas-servicemanager/delegators/)
47+
`Laminas\Db\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/).
49+
50+
The delegator tries to fetch a database adapter from the service container and
51+
sets the adapter to the requested service.
52+
53+
### Create Class and Use Trait
54+
55+
Create a class and add the trait `AdapterAwareTrait`.
56+
57+
```php
58+
use Laminas\Db\Adapter\Adapter;
59+
use Laminas\Db\Adapter\AdapterInterface;
60+
61+
class Example implements AdapterAwareInterface
62+
{
63+
use AdapterAwareTrait;
64+
65+
public function getAdapter() : Adapter
66+
{
67+
return $this->adapter;
68+
}
69+
}
70+
```
71+
72+
(A getter method is also added for demonstration.)
73+
74+
### Create and Configure Service Manager
75+
76+
Create and [configured the service manager](https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/):
77+
78+
```php
79+
use Interop\Container\ContainerInterface;
80+
use Laminas\Db\Adapter\AdapterInterface;
81+
use Laminas\Db\Adapter\AdapterServiceDelegator;
82+
use Laminas\Db\Adapter\AdapterAwareTrait;
83+
use Laminas\Db\Adapter\AdapterAwareInterface;
84+
85+
$serviceManager = new Laminas\ServiceManager\ServiceManager([
86+
'factories' => [
87+
// Database adapter
88+
AdapterInterface::class => static function(ContainerInterface $container) {
89+
return new Laminas\Db\Adapter\Adapter([
90+
'driver' => 'Pdo_Sqlite',
91+
'database' => 'path/to/sqlite.db',
92+
]);
93+
}
94+
],
95+
'invokables' => [
96+
// Example class
97+
Example::class => Example::class,
98+
],
99+
'delegators' => [
100+
// Delegator for Example class to set the adapter
101+
Example::class => [
102+
AdapterServiceDelegator::class,
103+
],
104+
],
105+
]);
106+
```
107+
108+
### Get Instance of Class
109+
110+
[Retrieving an instance](https://docs.laminas.dev/laminas-servicemanager/quick-start/#3-retrieving-objects)
111+
of the `Example` with a database adapter:
112+
113+
```php
114+
/** @var Example $example */
115+
$example = $serviceManager->get(Example::class);
116+
117+
var_dump($example->getAdapter() instanceof Laminas\Db\Adapter\Adapter); // true
118+
```
119+
120+
## Concrete Implementations
121+
122+
The validators [`Db\RecordExists` and `Db\NoRecordExists`](https://docs.laminas.dev/laminas-validator/validators/db/)
123+
implements the trait and the plugin manager of [laminas-validator](https://docs.laminas.dev/laminas-validator/)
124+
includes the delegator to set the database adapter for both validators.

mkdocs.yml

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ docs_dir: docs/book
22
site_dir: docs/html
33
nav:
44
- Home: index.md
5-
- Reference:
6-
- Adapters: adapter.md
7-
- "Result Sets": result-set.md
8-
- "SQL Abstraction": sql.md
9-
- "DDL Abstraction": sql-ddl.md
10-
- "Table Gateways": table-gateway.md
11-
- "Row Gateways": row-gateway.md
12-
- "RDBMS Metadata": metadata.md
13-
- "Application Integration":
14-
- "Integrating in a Laminas MVC application": application-integration/usage-in-a-laminas-mvc-application.md
5+
- Adapters:
6+
- Introduction: adapter.md
7+
- AdapterAwareTrait: adapters/adapter-aware-trait.md
8+
- "Result Sets": result-set.md
9+
- "SQL Abstraction": sql.md
10+
- "DDL Abstraction": sql-ddl.md
11+
- "Table Gateways": table-gateway.md
12+
- "Row Gateways": row-gateway.md
13+
- "RDBMS Metadata": metadata.md
14+
- "Application Integration":
15+
- "Integrating in a Laminas MVC application": application-integration/usage-in-a-laminas-mvc-application.md
1516
site_name: laminas-db
1617
site_description: "Database abstraction layer, SQL abstraction, result set abstraction, and RowDataGateway and TableDataGateway implementations"
1718
repo_url: 'https://github.com/laminas/laminas-db'

0 commit comments

Comments
 (0)