Unrecognized field gallery in Symfony prod mode - symfony

I'm developing web site with Symfony 4.4, using SonataAdmin (^3.56), Doctrine ORM Admin (^3.12), Sonata Media (^3.22) and SonataClassification (^3.9) bundles. My web site works perfectly in dev mode. Problem is when I change it to prod mode.
.env
APP_ENV=prod
APP_DEBUG=0
When I access routes with SonataMedia gallery then I get error 500. Log file (prod.log) contains the following content:
request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMException: "Unrecognized field: gallery" at /home/repincln/dev/site/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 101 {"exception":"[object] (Doctrine\\ORM\\ORMException(code: 0): Unrecognized field: gallery at /home/repincln/dev/site/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:101)"} []
In Controller I have:
...
$medias = $this->getDoctrine()->getRepository("ApplicationSonataMediaBundle:GalleryHasMedia")->findBy(array('gallery' => $article->getGallery()));
...
If i dump $medias in dev mode, it looks OK, with all images in gallery, but in production mode it doesn't work.
A similar thing happens in SonataAdmin Admin->Classification
request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line 0, col 84 near 'parent IS NU': Error: Class App\Application\Sonata\ClassificationBundle\Entity\Category has no field or association named parent" at /home/repincln/dev/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 65 {"exception":"[object] (Doctrine\\ORM\\Query\\QueryException(code: 0): [Semantical Error] line 0, col 84 near 'parent IS NU': Error: Class App\\Application\\Sonata\\ClassificationBundle\\Entity\\Category has no field or association named parent at /home/repincln/dev/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:65, Doctrine\\ORM\\Query\\QueryException(code: 0): SELECT c FROM App\\Application\\Sonata\\ClassificationBundle\\Entity\\Category c WHERE c.parent IS NULL at /home/repincln/dev/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:43)"} []
and Tags, Collections, Media. Only Contexts is working normal. Gallery for example looks like:
config/packages/prod/doctrine.yaml
doctrine:
orm:
auto_generate_proxy_classes: false
metadata_cache_driver:
type: service
id: doctrine.system_cache_provider
query_cache_driver:
type: service
id: doctrine.system_cache_provider
result_cache_driver:
type: service
id: doctrine.result_cache_provider
services:
doctrine.result_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.result_cache_pool'
doctrine.system_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.system_cache_pool'
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
config/packages/doctrine.yaml
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
types:
json: Sonata\Doctrine\Types\JsonType
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
Article.php
...
/**
* #ORM\ManyToOne(targetEntity="App\Application\Sonata\MediaBundle\Entity\Gallery");
*/
private $gallery;
...
As i wrote in development mode everything is working fine.
The problem persists even though I did (many times):
APP_ENV=prod APP_DEBUG=0 php bin/console cache:clear
php bin/console cache:clear
remove var/cache and var/log directories, and create them again
php bin/console doctrine:cache:clear-metadata
restart apache
change permissions (777) to uploads folder
configure apcu
-How to configure apcu for doctrine in Symfony5
use Symfony cache pool -
https://github.com/symfony/symfony/issues/24545#issuecomment-336419270
try this -
Doctrine 2.5: Unrecognized field (but only in Symfony's prod mode)
Any ideas what else can I do? I think there is a problem with cache ... somewhere.

I finally found a solution.
I had in the config/bundles.php
Sonata\EasyExtendsBundle\SonataEasyExtendsBundle::class => ['dev' => true, 'test' => true],
It must be:
Sonata\EasyExtendsBundle\SonataEasyExtendsBundle::class => ['all' => true],

Related

Multiple entity manager: The target entity specified on is unknown or not an entity

I have 2 entity managers, each for one of my databases DATABASE_URL and DATABASE_URL_SAGE:
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: site
connections:
site:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_sqlsrv'
# ...
sage:
url: '%env(resolve:DATABASE_URL_SAGE)%'
driver: 'pdo_sqlsrv'
# ...
orm:
auto_generate_proxy_classes: true
default_entity_manager: site
entity_managers:
site:
connection: site
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Site:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Entity/Site'
prefix: 'App\Entity\Site'
alias: Site
sage:
connection: sage
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Sage:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Entity/Sage'
prefix: 'App\Entity\Sage'
alias: Sage
I would like to create an entity which:
has a ManyToOne relation with another entity managed by the same entity manager (site)
has another ManyToOne relation with another entity managed by the sage entity manager
Something like this:
// src/Entity/Site/CategoryFArticle.php
namespace App\Entity\Site;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class CategoryFArticle
{
#[ORM\ManyToOne(targetEntity: 'App\Entity\Site\Category')]
private $category;
#[ORM\ManyToOne(targetEntity: 'App\Entity\Sage\FArticle')]
private $fArticle;
}
The problem is when I do that I got the error:
Mapping
-------
[FAIL] The entity-class App\Entity\Site\CategoryFArticle mapping is invalid:
* The target entity 'App\Entity\Sage\FArticle' specified on App\Entity\Site\CategoryFArticle#fArticle is unknown or not an entity.
Database
--------
In MappingException.php line 23:
The class 'App\Entity\Sage\FArticle' was not found in the chain configured namespaces App\Entity\Site
Questions
Is doctrine able to manage this kind of mapping (with entities managed by differents entity managers) ?
If so, how can I configure my current code to work ?
Thank you

Symfony #Paramconverter works over autowiring

I am working on upgrade Symfony 3.3 to 4.4 and almost done with it, but I have one last issue.
I enabled autowiring with default config, but whole project uses lots of #ParamConverter conversions without annotation.
Problem: ParamConverter with auto_convert tries to convert services and classes that are mentioned to controller by typehinting for autowiring (not entities).
Controller:
/**
* #Route("/report", name="report_page")
*/
public function report(
Request $request,
FileManager $fileManager
): Response {
// code
}
Error:
The class 'App\Service\FileManager' was not found in the chain configured namespaces App\Entity.
Service 'App\Service\FileManager' exists and works correctly.
ParamConverter config (by default 'request: {converters: true, auto_convert: true}'):
sensio_framework_extra:
router:
annotations: false
Doctrine config:
doctrine:
dbal:
default_connection: default
connections:
default:
# config
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
Base:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: App\Entity
Controller setting (services.yaml):
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DataFixtures,Entity,Objects,Repository,Utils}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
I understand that I can set 'sensio_framework_extra.request.auto_convert' to false and use #ParamConverter annotation everywhere in controllers, but there are too many places with this conversion and I try to get default behavior, so additional #ParamConverter annotations would be extra.
Just in case: I tried to disable auto_convert and everything worked correctly (services were autowired and entities were passed to controllers).
Also I checked that I installed last available versions of packages and bundles.
Need some hints on it. Probably I missed something. I tried to find exact way how ParamConverter works by default and how it checks that class is entity or not, but it is too complicated and I decided that I dug too deep.
EDIT:
Probably I have some issue with autowiring's config and not paramconverter. But I checked php bin/console debug:autowiring and got all needed services as available via autowiring.
It looks like symfony is trying to convert the fileManager like an entity.
Try moving the injected service(s) into the __construct method of the controller (and add the property to the controller) like:
private $fileManager;
...
public function __construct(FileManager $fileManager) {
$this->fileManager = $fileManager;
}
and then access it in the report method:
$this->fileManager

Unknown entity namespace in security.yml with custom entities folder in Symfony 2

I have my model entities saved in src/AppBundle/Model/Entity.
You can understand, that i have own custom folder for entities (in model folder).
This is my orm settings in config.yml:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: false
mappings:
user:
type: annotation
dir: %kernel.root_dir%/../src/AppBundle/Model/Entity
alias: 'Entity'
prefix: AppBundle\Model\Entity
is_bundle: false
I have a problem with these lines in my security.yml:
providers:
our_db_provider:
entity:
class: AppBundle:User
property: username
# if you're using multiple entity managers
# manager_name: customer
There is some error:
Unknown Entity namespace alias 'AppBundle'.
I don't what ref I have to use (AppBundle:User probably not).
Thank you for your answers.
By default, all Symfony bundles will have a nice alias: XxxBundle (aliasing the NamespaceOfXxxBundle\Entity namespace). As you're bundle doesn't follow this convention and stores it in Model\Entity instead, you have 2 options:
Don't use the alias feature and pass the FQCN: AppBundle\Model\Entity\User
Create a new alias, give it a nice name and use it:
doctrine:
orm:
# ...
mappings:
user:
type: annotation
dir: %kernel.root_dir%/../src/AppBundle/Model/Entity
alias: App # <-- the alias name
prefix: AppBundle\Model\Entity
is_bundle: false
App:User

FosElasticaBundle: how to dump the actual JSON passed to ElasticSearch?

I am using FosElasticaBundle in a Symfony project. I configured my mappings but I get exception "expected a simple value for field [_id] but found [START_OBJECT]]".
I'd like to see the actual JSON created by FosElasticaBundle so I can directly test it against my ElasticSearch server, and understand more about the exception.
According to FosElastica documentation, everything should be logged when debug mode is enabled (i.e. in DEV environment) but I can't see this happening; I only see Doctrine queries, but no JSON.
How can I dump the JSON created by FosElasticaBundle?
Update: mappings
# FOSElasticaBundle
fos_elastica:
clients:
default: { host: %elasticsearch_host%, port: %elasticsearch_port%, logger: false }
indexes:
app:
types:
user:
mappings:
name: ~
surname: ~
persistence:
driver: orm
model: AppBundle\Entity\User
provider: ~
listener: ~
finder: ~
I think you should only set your logger to true instead of false
fos_elastica:
clients:
default:
host: %elasticsearch_host%
port: %elasticsearch_port%
logger: true <---- set true here
...

Connect Doctrine to memcached pool

Perhaps anybody know, how to connect Doctrine to memcached pool, to use it as a cache driver?
I've check official bundle documentation, and lot of another sources, but didn't find any examples of such connection.
Also due to source code, I could not find any options to use pool, but perhaps I miss something.
Didn't test, but the following should work:
in app/config/parameters.yml, set/add
parameters:
memcached.servers:
- { host: 127.0.0.1, port: 11211 }
- { host: 127.0.0.2, port: 11211 }
in app/config/config.yml set/add
services:
memcache:
# class 'Memcache' or 'Memcached', depending on which PHP module you use
class: Memcache
calls:
- [ addServers, [ %memcached.servers% ]]
doctrine.cache.memcached:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [setMemcached, [#memcached]]
in app/config/config_prod.yml, set
doctrine:
orm:
metadata_cache_driver:
type: service
id: doctrine.cache.memcached
query_cache_driver:
type: service
id: doctrine.cache.memcached
result_cache_driver:
type: service
id: doctrine.cache.memcached
As I said, I can't test it, but this is the combination of several known-to-work techniques.
UPDATE: solution updated based on CrazySquirrel's findings.
Thanks lxg for your ideas. I've build right configuration using your ideas. Please find correct service definition below:
application config:
result_cache_driver:
type: service
id: doctrine.cache.memcached
service.yml
memcached:
class: Memcached
calls:
- [ addServers, [ %memcached_servers% ]]
doctrine.cache.memcached:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [setMemcached, [#memcached]]

Resources