Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Db Laravel Package

laminas/laminas-db

Laminas\Db is a flexible database abstraction for PHP, providing adapters for major databases, SQL builders, table gateways, result sets, metadata tools, and a profiler to help build portable, testable data access layers.

View on GitHub
Deep Wiki
Context7

AdapterAwareTrait

The trait Laminas\Db\Adapter\AdapterAwareTrait, which provides implementation for Laminas\Db\Adapter\AdapterAwareInterface, and allowed removal of duplicated implementations in several components of Laminas or in custom applications.

The interface defines only the method setDbAdapter() with one parameter for an instance of Laminas\Db\Adapter\Adapter:

public function setDbAdapter(\Laminas\Db\Adapter\Adapter $adapter) : self;

Basic Usage

Create Class and Add Trait

use Laminas\Db\Adapter\AdapterAwareTrait;
use Laminas\Db\Adapter\AdapterAwareInterface;

class Example implements AdapterAwareInterface
{
    use AdapterAwareTrait;
}

Create and Set Adapter

Create a database adapter and set the adapter to the instance of the Example class:

$adapter = new Laminas\Db\Adapter\Adapter([
    'driver'   => 'Pdo_Sqlite',
    'database' => 'path/to/sqlite.db',
]);

$example = new Example();
$example->setAdapter($adapter);

AdapterServiceDelegator

The delegator Laminas\Db\Adapter\AdapterServiceDelegator can be used to set a database adapter via the service manager of laminas-servicemanager.

The delegator tries to fetch a database adapter via the name Laminas\Db\Adapter\AdapterInterface from the service container and sets the adapter to the requested service. The adapter itself must be an instance of Laminas\Db\Adapter\Adapter.

Integration for Mezzio and laminas-mvc based Applications

In a Mezzio or laminas-mvc based application the database adapter is already registered during the installation with the laminas-component-installer.

Create Class and Use Trait

Create a class and add the trait AdapterAwareTrait.

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\AdapterInterface;

class Example implements AdapterAwareInterface
{
    use AdapterAwareTrait;

    public function getAdapter() : ?Adapter
    {
        return $this->adapter;
    }
}

(A getter method is also added for demonstration.)

Create and Configure Service Manager

Create and configured the service manager:

use Interop\Container\ContainerInterface;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\Adapter\AdapterServiceDelegator;
use Laminas\Db\Adapter\AdapterAwareTrait;
use Laminas\Db\Adapter\AdapterAwareInterface;

$serviceManager = new Laminas\ServiceManager\ServiceManager([
    'factories' => [
        // Database adapter
        AdapterInterface::class => static function(ContainerInterface $container) {
            return new Laminas\Db\Adapter\Adapter([
                'driver'   => 'Pdo_Sqlite',
                'database' => 'path/to/sqlite.db',
            ]);
        }
    ],
    'invokables' => [
        // Example class
        Example::class => Example::class,
    ],
    'delegators' => [
        // Delegator for Example class to set the adapter
        Example::class => [
            AdapterServiceDelegator::class,
        ],
    ],
]);

Get Instance of Class

Retrieving an instance of the Example class with a database adapter:

/** [@var](https://github.com/var) Example $example */
$example = $serviceManager->get(Example::class);

var_dump($example->getAdapter() instanceof Laminas\Db\Adapter\Adapter); // true

Concrete Implementations

The validators Db\RecordExists and Db\NoRecordExists implements the trait and the plugin manager of laminas-validator includes the delegator to set the database adapter for both validators.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4