Skip to content

[Cache] Using DSN with PDOAdapter #52460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HypeMC opened this issue Nov 5, 2023 · 3 comments · Fixed by #53448
Closed

[Cache] Using DSN with PDOAdapter #52460

HypeMC opened this issue Nov 5, 2023 · 3 comments · Fixed by #53448

Comments

@HypeMC
Copy link
Contributor

HypeMC commented Nov 5, 2023

Symfony version(s) affected

>=5.4

Description

According to the docs it should be possible to use DSN to configure the PDOAdapter, how an exception is thrown instead:

  Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".

This happens because the AbstractAdapter::createConnection() supports only certain adapters:

public static function createConnection(string $dsn, array $options = [])
{
if (str_starts_with($dsn, 'redis:') || str_starts_with($dsn, 'rediss:')) {
return RedisAdapter::createConnection($dsn, $options);
}
if (str_starts_with($dsn, 'memcached:')) {
return MemcachedAdapter::createConnection($dsn, $options);
}
if (0 === strpos($dsn, 'couchbase:')) {
if (CouchbaseBucketAdapter::isSupported()) {
return CouchbaseBucketAdapter::createConnection($dsn, $options);
}
return CouchbaseCollectionAdapter::createConnection($dsn, $options);
}
throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:" nor "couchbase:".');
}

I'm not sure if this is a bug in the documentation (and possible new feature) or a bug in the code.

How to reproduce

framework:
  cache:
    pools:
      my.cache:
        adapter: cache.adapter.pdo
        provider: "pgsql:host=localhost;port=5432;dbname=postgres;user=postgres;password=pass"
#[AsCommand('app:test')]
class TestCommand extends Command
{
    public function __construct(
        #[Autowire(service: 'my.cache')] private CacheInterface $cache,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        return Command::SUCCESS;
    }
}

Possible Solution

Add the necessary logic to AbstractAdapter::createConnection().

Additional Context

No response

@nicolas-grekas
Copy link
Member

Looks like a feature request indeed, there's nothing in the code that could make this work currently.

@stof
Copy link
Member

stof commented Nov 6, 2023

And this should be reported as a bug in https://github.com/symfony/symfony-docs/ as it documents something that does not exist.

@HypeMC
Copy link
Contributor Author

HypeMC commented Nov 6, 2023

Thanks guys, I'll open a PR in the docs repo.

@xabbuh xabbuh added Feature and removed Bug labels Nov 6, 2023
OskarStark added a commit to symfony/symfony-docs that referenced this issue Nov 7, 2023
This PR was merged into the 5.4 branch.

Discussion
----------

[Cache] Fix PdoAdapter examples

As mentioned in symfony/symfony#52460, using a DSN with the PdoAdapter is currently not supported, instead a service name should be provided, eg:

```yaml
app.my_pdo_service:
    class: \PDO
    arguments:
        - 'pgsql:host=localhost'
```

I haven't included the actual service definition since `app.my_psr6_service` isn't included either, but if you think I should, please let me know.

Commits
-------

7ab6c81 [Cache] Fix PdoAdapter examples
@fabpot fabpot closed this as completed Jan 12, 2024
fabpot added a commit that referenced this issue Jan 12, 2024
…ypeMC)

This PR was merged into the 7.1 branch.

Discussion
----------

[Cache] Add support for using DSN with `PDOAdapter`

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | Fix #52460
| License       | MIT

Adds support for using DSN with the `PDOAdapter`:

```yaml
framework:
  cache:
    pools:
      my.cache:
        adapter: cache.adapter.pdo
        provider: "pgsql:host=localhost;port=5432;dbname=postgres;user=postgres;password=pass"
```

Commits
-------

75984c2 [Cache] Add support for using DSN with PDOAdapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants