Non-existing setting in Doctrine bridge in Symfony2 - symfony

I've just installed Sylius Settings bundle.
After composer finished its work, and after setting everything as in docs (link above), enabling Sylius Settings bundle causes the following error:
The service definition "doctrine.orm.default_metadata_driver" does not exist.
I searched for this keywords (whole service name) for the whole vendors folder, but it found it only in some Doctrine test files, nowhere else. Anyone knows what is it about? Perhaps it's just a "legacy bug" in SF2?
Or perhaps not. I found this class for the moment:
Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass
in particular this part (constructor):
public function __construct($driver, array $namespaces, array $managerParameters, $enabledParameter = false, array $aliasMap = array())
{
$managerParameters[] = 'doctrine.default_entity_manager';
parent::__construct(
$driver,
$namespaces,
$managerParameters,
'doctrine.orm.%s_metadata_driver',
$enabledParameter,
'doctrine.orm.%s_configuration',
'addEntityNamespace',
$aliasMap
);
}
(Note: "'doctrine.orm.%s_metadata_driver'," as a partly hard-coded param)
I wonder if it has anything to do with the fact that I'm not using ORM, but PHPCR-ODM as a default connection type (ORM is non-default additional connection layer).
config.yml part referring to the bundle:
sylius_settings:
driver: doctrine/orm
doctrine_cache:
providers:
my_apc_metadata_cache:
type: apc
namespace: metadata_cache_ns
my_apc_query_cache:
namespace: query_cache_ns
apc: ~
sylius_settings:
type: file_system
Nothing extra ordinary.

Related

Issue with Doctrine namespace aliases during upgrade from Symfony 3 to 4

I am trying to upgrade a Symfony 3 application to Symfony 4, but I am having trouble with Doctrine's namespace aliases. My entities are located under AppBundle\Entity (as was the default in older Symfony versions) and there was an alias for that, so my entities are referred to via AppBundle:SomeEntity all over my codebase. I understand that the new way to do this is to use the FQCN instead, but I would like to avoid having to do a massive find&replace operation right now.
I modified config/packages/doctrine.yaml with the following mapping:
mappings:
AppBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/AppBundle/Entity'
prefix: 'AppBundle\Entity'
alias: AppBundle
However, I only get a ReflectionException saying Class AppBundle:SomeEntity does not exist. As far as I can tell from the stack trace, the alias is never resolved, just passed directly down to AbstractManagerRegistry as a class name, which then just passes it to the ReflectionClass constructor. The calling code, e.g., looks like this (inside a controller): $this->getDoctrine()->getRepository("AppBundle:SomeEntity")->...
I have now idea what the problem is. It looks like there is just no logic that would resolve aliases in this code path. It is probably a configuration issue. However, the configuration is mostly the default that comes with Symfony 4. I basically just replaced "App" with "AppBundle". Can anyone help me out here?

Exclude fields from FOSUserBundle using JMS Serializer working on dev but not on prod environment

I would like to exclude fields when exposing my API from my user class that extends FOSUser.
I've setup JMS Serializer on the global config file and created a FOSUB config to only expose the fields I need.
Global Config:
app/config/config.yml
jms_serializer:
metadata:
directories:
FOSUB:
namespace_prefix: "FOS\\UserBundle"
path: "#AppBundle/Resources/config/serializer/fos"
FOS config file:
src/AppBundle/Resources/config/serializer/fos/Model.user.yml
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
email:
expose: true
roles:
expose: true
This config is working perfectly on my local machine however it doesn't work when deployed on prod. Both use same stack, my guess is that on prod somehow the serializer can't find FOS config file.
Any help would be much appreciated.
The issue was somehow related to the naming of the config file.
While in local (macos) the file name Model.user.yml was working, in production (centos) it didn't work. So I had to rename the file to Model.User.yml then it worked fine on both.
I tried to find some documentation related to this issue but couldn't find any.
Take away: Make sure that the config file name represent exactly the entity you want to override.

symfony4 migrate autowire to true - get error message

I am migrating from symofony 2.7 to symfony 4.0. With success I migrated one bundle. Now I am migrating the second bundle and the error message is coming up. I don't get at all what symfony 4.0 wants from me.
If I turn on autowire: true this error message is coming up.
Cannot autowire service "App\Kernel": argument "$environment" of method "Symfony\Component\HttpKernel\Kernel::__construct()" must have a type-hint or be given a value explicitly.
Can somebody help me?
If I turn it off, no message is coming up.
Update
I registered my bundle only in bundles.php
App\Backend\AccountBundle\BackendAccountBundle::class => ['all' => true],
Usually the Kernel is added to the Service Container as a so called synthetic service, meaning it's not generated by the DI-container from configuration. Rather the id is set and then the previously configured service is just added to the container. It seems rather odd that your bundle's container wants to create a new kernel here. So I would check where and how you want to access the kernel in any of the bundle's services and whether you actually want to pass in the kernel and not something else. If you do you might want to check the Service Container-documentation on synthetic services.
As to the error itself. Symfony's autowiring often falls flat when you have services that require parameters like with the Kernel:
public function __construct(string $environment, bool $debug) {...}
In these cases you have to either have a parameter defined in your services.yaml that matches the name of the parameter:
# config/services.yaml
parameters:
environment: prod
debug: false
or you have to tell the configuration which parameters you want to have in those places.
App\Kernel:
$environment: prod
$debug: false
This will tell the autowiring that only the 2 arguments named environment and debug should be overwritten with the values you provide, but the rest is done via autowiring. This way you can skip the arguments: part of the definition and you can also skip all arguments you know are correctly set via autowiring.
For example if you have a service like this:
class MyService {
public function __construct(OtherServce $service, string $someParameter) {}
}
# config/services.yaml
services:
_defaults:
autowiring: true
MyService:
$someParameter: 'someValue'
This is the same as explicitly writing:
services:
MyService:
class: MyService
arguments:
- '#OtherServce'
- 'someValue'

Provide Doctrine with custom cache factory for second level cache

Background: we're using Symfony 3.1 + Doctrine 2.5.5 + symfony doctrine bundle.
While trying to enable second level caching for our entities, we have encountered the following issue. If we use NONSTRICT_READ_WRITE, everything works fine. However, when we tried to use READ_WRITE, we got the following error
0)
Type error: Argument 2 passed to Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister::__construct() must be an instance of Doctrine\ORM\Cache\ConcurrentRegion, instance of Doctrine\ORM\Cache\Region\DefaultRegion given, called in vendor/doctrine/orm/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php on line 133
As far as I understood, we need to actually implement our own version of ConcurrentRegion and CacheFactory to make it work (FileLockRegion does not suit us due to its usage of file system to handle cache locks). So I wrote those implementations, but the main issue now lies in following: I cannot find where to put my custom factory class' name in the configuration. We have tried the following locations in config:
1)
doctrine:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: master
second_level_cache:
default_cache_factory:
class: AppBundle\Cache\MyCacheFactory
This fails due to
Unrecognized option "second_level_cache" under "doctrine.orm"
even though in our other project using Symfony 2.8 option "second_level_cache" does not throw any errors.
So we went to doctrine bundle code and found the following node description (vendor/doctrine/doctrine-bundle/DependencyInjection/Configuration.php:492)
->arrayNode('second_level_cache')
->children()
->append($this->getOrmCacheDriverNode('region_cache_driver'))
->scalarNode('region_lock_lifetime')->defaultValue(60)->end()
->booleanNode('log_enabled')->defaultValue($this->debug)->end()
->scalarNode('region_lifetime')->defaultValue(0)->end()
->booleanNode('enabled')->defaultValue(true)->end()
->scalarNode('factory')->end()
->end()
So we decided we should try this config in our master entity manager section:
2)
second_level_cache:
region_cache_driver:
type: memcache
enabled: true
log_enabled: true
factory: AppBundle\Cache\MyCacheFactory
regions:
hour_region:
lifetime: 3600
However, even though this config is considered valid, when we actually try to access the entity with configured caching, we get the error 0), which makes us think that this option is being ignored by doctrine/symfony.
Is there any way to do it via .yml config at all? Doctrine docs only propose to implement CacheFactory and provide a PHP code example, but it's still quite unclear where should this PHP code go, even if we decide to abandon the idea of putting our class in .yml config and go the PHP way.
Use type - filelock for configurate FilelockRegion
regions:
default:
cache_driver:
type: service
id: 'Doctrine\Common\Cache\RedisCache'
lifetime: 3600
type: filelock

Unable to load an entity that is not related to a bundle

I am building a company Symfony2 library that includes several bundles and a library of classes that can be used by any code. The plan is to eventually turn this into an internal package and install it into projects via composer so that it will live in the vendor directory.
One component of the library has a pair of database entity classes and a single repository class associated with it.
Because the only logic associated with these classes is contained in the repository class, it doesn't seem to make sense to to surround it with a bundle.
I have, as yet, been unable to get Doctrine to load this repository. It initially failed with a
The class 'Dplh\Library\DplhEnum\Entity\EnumGroupRepository' was not found in the chain configured namespaces Dplh\DplhSecurityBundle\Entity
exception. (The reference to my security bundle was initially rather confusing. Turns out that this is Doctrine helping out by listing all of the known entity namespaces (see Symfony error The class XXX was not found in the chain configured namespaces XXX)).
As I understand it, this happens because Doctrine is configured to use auto-mapping and expects all entities to be defined in src/WhateverBundle/Entity directories.
This can be changed with additional Doctrine configuration for specific mappings in config.yml (http://zalas.eu/how-to-store-doctrine-entities-outside-of-a-symfony-bundle/).
So far, I have been unable to get this to work either. I expect it's a minor config thing somewhere. I added the following to config.yml:
doctrine:
dbal:
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
DplhEnum:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/Dplh/Library/DplhEnum/Entity
prefix: Dplh\Library\DplhEnum\Entity
alias: DplhEnum
I get two different exceptions depending on my call to getRepository().
// Using the fully-qualified class name.
$this->getEntityManager()->getRepository('Dplh\Library\DplhEnum\Entity\EnumGroup' )
This throws
Attempted to load class "EnumGroupRepository" from namespace "Dplh\DplhEnumBundle\Entity".
Did you forget a "use" statement for another namespace?
// Using the alias.
$this->getEntityManager()->getRepository('DplhEnum\EnumGroup' )
this throws
Class 'DplhEnum\EnumGroup' does not exist
I have verified that the EnumGroupRepository.php file is in the src/Dplh/Library/DplhEnum/Entity directory and the EnumGroupRepository class is in the Dplh\Library\DplhEnum\Entity namespace and that they are in the correct directory.

Resources