Repository: pimcore/pimcore (language: php)

Issue:
[Bug]: Exception on execute ClassesDefinitionsBuildCommand
### Pimcore version

11.1

### Steps to reproduce

- update from v11.0.12 to v11.1 

- call the command

`pimcore/bin/console pimcore:build:classes`

### Actual Behavior

![exception](https://github.com/pimcore/pimcore/assets/95299884/4c7cf9f7-e0a3-4acc-b29f-65235b986012)


### Expected Behavior

update to v11.1 and call of the command possible without error


Discussion / hints:
I think we need some sort of regression/smoke test for this command, as it's not the first time it broke.
But is it really broken or is something with your configuration broken? It looks like the database server cannot be resolved or not reached?
But this command shouldn't need the database! It should just regenerate the source code of the classes.
@jdreesen right. I should know, I created that :D
Hi @dpfaffenbauer, do you have any news about this issue? Thanks for your support and your work
@dvesh3, etc. could you please consider this for the next release? It breaks deployments with version `11.1`.
@dvesh3 This is the reason:

```
line 49:
                                                                                                                                                   
  [Doctrine\DBAL\Exception\TableNotFoundException (1146)]                                                                                          
  An exception occurred while executing a query: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pimcore.settings_store' doesn't exist  
                                                                                                                                                   

Exception trace:
  at /var/www/html/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php:49
 Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter->convert() at /var/www/html/vendor/doctrine/dbal/src/Connection.php:1938
 Doctrine\DBAL\Connection->handleDriverException() at /var/www/html/vendor/doctrine/dbal/src/Connection.php:1880
 Doctrine\DBAL\Connection->convertExceptionDuringQuery() at /var/www/html/vendor/doctrine/dbal/src/Connection.php:1105
 Doctrine\DBAL\Connection->executeQuery() at /var/www/html/vendor/doctrine/dbal/src/Connection.php:952
 Doctrine\DBAL\Connection->fetchFirstColumn() at /var/www/html/vendor/pimcore/pimcore/models/Tool/SettingsStore/Dao.php:76
 Pimcore\Model\Tool\SettingsStore\Dao->getIdsByScope() at /var/www/html/vendor/pimcore/pimcore/models/Tool/SettingsStore.php:131
 Pimcore\Model\Tool\SettingsStore::getIdsByScope() at /var/www/html/vendor/pimcore/pimcore/lib/Config/LocationAwareConfigRepository.php:262
 Pimcore\Config\LocationAwareConfigRepository->fetchAllKeys() at /var/www/html/vendor/pimcore/pimcore/lib/Model/Dao/PimcoreLocationAwareConfigDao.php:76
 Pimcore\Model\Dao\PimcoreLocationAwareConfigDao->loadIdList() at /var/www/html/vendor/pimcore/pimcore/models/DataObject/SelectOptions/Config/Listing/Dao.php:31
 Pimcore\Model\DataObject\SelectOptions\Config\Listing\Dao->loadList() at /var/www/html/vendor/pimcore/pimcore/models/DataObject/SelectOptions/Config/Listing.php:39
 Pimcore\Model\DataObject\SelectOptions\Config\Listing->getSelectOptions() at /var/www/html/vendor/pimcore/pimcore/models/DataObject/SelectOptions/Config/Listing.php:72
 Pimcore\Model\DataObject\SelectOptions\Config\Listing->getIterator() at /var/www/html/vendor/pimcore/pimcore/bundles/CoreBundle/src/Command/ClassesDefinitionsBuildCommand.php:82
 Pimcore\Bundle\CoreBundle\Command\ClassesDefinitionsBuildCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:326
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1081
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:91
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:320
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:80
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:174
 Symfony\Component\Console\Application->run() at /var/www/html/bin/console:51

In Exception.php line 28:
                                                                                                    
  [Doctrine\DBAL\Driver\PDO\Exception (1146)]                                                       
  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pimcore.settings_store' doesn't exist  
```
@dvesh3 The new Select Options feature in combination with the SettingsStore is the problem here (https://github.com/pimcore/pimcore/pull/14454). It tries to create the PHP Enums with configurations from the database. That can never work in reality. How can we solve that as fast as possible?
Thanks @dpfaffenbauer for your findings. This also concerns other data types like `language` with which tries to load valid languages from db (when system settings are stored on settingsStore) so we need a generic fix (please see https://github.com/pimcore/pimcore/pull/16281) and also adding notices to the docs that using settings store requires db service in deployment setup.

The quick workaround is to use `system-config` on source instances and consider the config files for deployment scenarios:
```yaml
pimcore:
    config_location:
        select_options:
            write_target:
                type: 'symfony-config'
                options:
                    directory: '/var/www/html/var/config/select_options'
```
@dvesh3 Yes. That sounds about right. is it possible then to not trying to connect to the DB at all when symfony-config is configured?
@dpfaffenbauer yes, this shouldn't happen now with this fix https://github.com/pimcore/pimcore/pull/16281/files#diff-aea6f63bf38b8863f731103a59f9a169a7ebd704dd223cb40a3813b1f94b737b

Could you please give it try? thanks!
The Try/Catch doesn't feel right. It is sort of an expectation thing. With the try/catch you expect in some situations to work and in some not. so it behaves differently. We don't want differently, we want the same behaviour. Hope you know what I mean
@dpfaffenbauer got your point :+1:  We'll come up with a proper fix then to handle it on using `symfony-config` so it doesn't break.
Ok, found a possible non-breaking fix. 
What we need to do is enabling the `read_target` option for the `select_options` node in `config_location`, here: 
https://github.com/pimcore/pimcore/blob/bcedd91f80c321b748e0024e8c2c65190bba793b/bundles/CoreBundle/src/DependencyInjection/Configuration.php#L142

Then we can make a default config like this: 
```yaml
pimcore: 
    config_location:
        select_options:
            read_target:
                type: 'symfony-config'
```

If you'd like to use `settings-store` or the fallback mechanism, you'd need to change the default config. 
Created a PR with the approach shared above: https://github.com/pimcore/ee-pimcore/pull/19 
@brusch how should someone (besides pimcore) now review that?
@brusch any eta? this is quite blocking
Fixed by https://github.com/pimcore/pimcore/commit/7a5aaf31e4f48a08b1656a6133a23b90cef32656 in https://github.com/pimcore/pimcore/pull/16380 (`v11.1.4`)
@kingjia90 nope, still not working

```

In ExceptionConverter.php line 101:
                                                                                                                                                                    
  [Doctrine\DBAL\Exception\ConnectionException (2002)]                                                                                                              
  An exception occurred in the driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for db failed: nodename nor servname provided, or not known  
                                                                                                                                                                    

Exception trace:
  at vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php:101
 Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter->convert() at vendor/doctrine/dbal/src/Connection.php:1938
 Doctrine\DBAL\Connection->handleDriverException() at vendor/doctrine/dbal/src/Connection.php:1886
 Doctrine\DBAL\Connection->convertException() at vendor/doctrine/dbal/src/Connection.php:379
 Doctrine\DBAL\Connection->connect() at vendor/doctrine/dbal/src/Connection.php:1654
 Doctrine\DBAL\Connection->getWrappedConnection() at vendor/doctrine/dbal/src/Connection.php:1081
 Doctrine\DBAL\Connection->executeQuery() at vendor/doctrine/dbal/src/Connection.php:952
 Doctrine\DBAL\Connection->fetchFirstColumn() at vendor/pimcore/pimcore/models/Tool/SettingsStore/Dao.php:76
 Pimcore\Model\Tool\SettingsStore\Dao->getIdsByScope() at vendor/pimcore/pimcore/models/Tool/SettingsStore.php:131
 Pimcore\Model\Tool\SettingsStore::getIdsByScope() at vendor/pimcore/pimcore/lib/Config/LocationAwareConfigRepository.php:262
 Pimcore\Config\LocationAwareConfigRepository->fetchAllKeys() at vendor/pimcore/pimcore/lib/Model/Dao/PimcoreLocationAwareConfigDao.php:76
 Pimcore\Model\Dao\PimcoreLocationAwareConfigDao->loadIdList() at vendor/pimcore/pimcore/models/DataObject/SelectOptions/Config/Listing/Dao.php:31
 Pimcore\Model\DataObject\SelectOptions\Config\Listing\Dao->loadList() at vendor/pimcore/pimcore/models/DataObject/SelectOptions/Config/Listing.php:39
 Pimcore\Model\DataObject\SelectOptions\Config\Listing->getSelectOptions() at vendor/pimcore/pimcore/models/DataObject/SelectOptions/Config/Listing.php:72
 Pimcore\Model\DataObject\SelectOptions\Config\Listing->getIterator() at vendor/pimcore/pimcore/bundles/CoreBundle/src/Command/ClassesDefinitionsBuildCommand.php:81
 Pimcore\Bundle\CoreBundle\Command\ClassesDefinitionsBuildCommand->execute() at vendor/symfony/console/Command/Command.php:326
 Symfony\Component\Console\Command\Command->run() at vendor/symfony/console/Application.php:1096
 Symfony\Component\Console\Application->doRunCommand() at vendor/symfony/framework-bundle/Console/Application.php:126
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at vendor/symfony/console/Application.php:324
 Symfony\Component\Console\Application->doRun() at vendor/symfony/framework-bundle/Console/Application.php:80
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at vendor/symfony/console/Application.php:175
 Symfony\Component\Console\Application->run() at bin/console:51

```

Fix patch:
diff --git a/lib/Config/LocationAwareConfigRepository.php b/lib/Config/LocationAwareConfigRepository.php
index f403e8ab8f0..dabeeb76362 100644
--- a/lib/Config/LocationAwareConfigRepository.php
+++ b/lib/Config/LocationAwareConfigRepository.php
@@ -264,6 +264,15 @@ public function fetchAllKeys(): array
         ));
     }
 
+    public function fetchAllKeysByReadTargets(): array
+    {
+        if ($this->storageConfig[self::READ_TARGET][self::TYPE] === self::LOCATION_SYMFONY_CONFIG) {
+            return array_keys($this->containerConfig);
+        }
+
+        return array_unique(SettingsStore::getIdsByScope($this->settingsStoreScope));
+    }
+
     private function invalidateConfigCache(): void
     {
         // invalidate container config cache if debug flag on kernel is set
diff --git a/lib/Model/Dao/PimcoreLocationAwareConfigDao.php b/lib/Model/Dao/PimcoreLocationAwareConfigDao.php
index da49c58a59c..79811869c22 100644
--- a/lib/Model/Dao/PimcoreLocationAwareConfigDao.php
+++ b/lib/Model/Dao/PimcoreLocationAwareConfigDao.php
@@ -76,6 +76,11 @@ protected function loadIdList(): array
         return $this->locationAwareConfigRepository->fetchAllKeys();
     }
 
+    protected function loadIdListByReadTargets(): array
+    {
+        return $this->locationAwareConfigRepository->fetchAllKeysByReadTargets();
+    }
+
     /**
      * Removes config with corresponding id from the cache.
      * A new cache entry will be generated upon requesting the config again.
diff --git a/models/DataObject/SelectOptions/Config/Listing/Dao.php b/models/DataObject/SelectOptions/Config/Listing/Dao.php
index fe9b1d64455..3a9be4a7d01 100644
--- a/models/DataObject/SelectOptions/Config/Listing/Dao.php
+++ b/models/DataObject/SelectOptions/Config/Listing/Dao.php
@@ -28,7 +28,7 @@ class Dao extends Config\Dao
     public function loadList(): array
     {
         $configs = [];
-        foreach ($this->loadIdList() as $id) {
+        foreach ($this->loadIdListByReadTargets() as $id) {
             $configs[] = Config::getById($id);
         }
 


Test patch: