|
| 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. |
0 commit comments