Skip to content

Connection factory

Inject the factory contract

Use ConnectionFactoryInterface when application code needs direct access to a Core connection:

use Small\SwooleEntityManagerBundle\Contract\ConnectionFactoryInterface;

final class ReportGateway
{
    public function __construct(
        private readonly ConnectionFactoryInterface $connectionFactory,
    ) {
    }
}

Calling get() without a name returns the configured default connection:

$connection = $this->connectionFactory->get();

Use a configured name for another connection:

$connection = $this->connectionFactory->get('users');

The wrapper returns the Core AbstractConnection implementation for the configured driver.

Native statements

Core connections execute driver-native statement objects. For example, with MySQL:

use Small\Collection\Collection\Collection;
use Small\SwooleEntityManager\Database\Connection\Mysql\Bean\SqlStatement;

$statement = new SqlStatement(
    'SELECT * FROM users WHERE id = :id',
    new Collection(['id' => 42]),
);

$resultSet = $this->connectionFactory
    ->get()
    ->execute($statement);

Use the statement type matching the selected database driver. For entity, scalar and statistics queries, prefer the query builders exposed by your entity manager.

Next chapter: Database layers