Symfony: Clear doctrine cache
Asked Answered
M

8

51

I need to clear my doctrine's cache in Symfony.

There must be some way in command line for clear the cache.

Or where should I find and delete the files belonging to cache?

Mccaleb answered 6/8, 2012 at 10:24 Comment(4)
rm -rf app/cache/* didn't help?Riff
does the answer helps? if no, try clearing complete cache using app/console cache:clearWalford
@thecatontheflat - note that the doctrine cache is often stored in apc rather than in the file system so removing the cache files would not help.Disenable
@Walford - note that the general app/console cache:clear is only for the symfony (app) cache. I don't think it clears the doctrine cache(s).Disenable
W
144

For Symfony 3+:

 php bin/console

will list all commands, the following are relevant for cache:

 php bin/console doctrine:cache:clear-metadata 
 php bin/console doctrine:cache:clear-query  
 php bin/console doctrine:cache:clear-result

Before Symfony 3:

app/console

will list how you can do it

 app/console doctrine:cache:clear-metadata 
 app/console doctrine:cache:clear-query  
 app/console doctrine:cache:clear-result 
Walford answered 6/8, 2012 at 10:27 Comment(3)
By the way, if you are using mongodb for example you can do 'app/console doctrine:mongodb:cache:clear-metadata' or just do 'app/console doctrine' and you will see all available optionsMeditate
Note that you can always use app/console list to show all commands or app/console list doctrine to just show commands in the 'doctrine' namespaceDisenable
I tried doctrine:cache:clear-result in prod environment. It is not working. I also tried bin/doctrine orm:clear-cache:result. It doesn't work either. I'm using memcache.Lvov
G
12

If you want to do it within your code (from Doctrine's documentation) :

If you simply want to delete all cache entries you can do so with the deleteAll() method.

$cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
$deleted = $cacheDriver->deleteAll();
Gardel answered 5/7, 2013 at 9:16 Comment(2)
What's the difference to cache flushing?Wyon
This code creates a new cache driver. It should utilize the current QueryCacheProfile or result cache implementation from the configuration. It will not magically clear cache in your application if use use e.g. Redis or Memcached.Alexia
S
2

In case you use APC, you could also just call the code

<?php
$deleted = apc_clear_cache() && apc_clear_cache('user');

in a php page on the same server. This is what deleteAll() method in Antho's answer does, but you do not depend on the Doctrine Classes. Btw: the complete cache will be flushed - just in case you use it for non-Doctrine stuff.

Simply answered 29/6, 2016 at 19:8 Comment(0)
W
2

I thought I was going crazy with doctrine results caching - in the end I had to restart memcached.

Willtrude answered 2/6, 2017 at 11:39 Comment(1)
how to restart memcached?Topheavy
S
1

I know the title of this post says Symfony 2, but for those of you coming from google, if you have Symfony 3+ its gonna be:

bin/console

As opposed to:

app/console
Skyscape answered 7/9, 2018 at 13:59 Comment(0)
G
0

Maybe is a little late for this, but in my case, doctrine didn't generate the proxy classes in production, for that I change the auto_generate_proxy_classes to true:

#symfony2&3 app/config/config.yml
#symfony4 config/packages/doctrine.yaml (by default true since 4.2)

doctrine:
    orm:
        auto_generate_proxy_classes: true #"%kernel.debug%"
Gyratory answered 2/10, 2019 at 19:9 Comment(1)
What do these proxy classes do?Talanta
R
0

Maybe someone searching
Symfony 6.2+

public function __construct(..., private KernelInterface $kernel

And

$this->kernel->shutdown(); //RESET ALL

You running Multiple entities and You managed connection with Sessions

/** @var SiteService $siteService */
$siteService = $this->kernel->getContainer()->get(SiteService::class); //Important kernel->getContainer()
/** @var Site[] $sites */
$sites = $siteService->repository->getByInstalledSites();

foreach ($sites as $site) {
    dump('Domain:'. $site->getDomain());
    $session = new Session();
    $session->set('db_user', $site->getUser());
    $session->set('db_password', $this->getDecryptPassword($site->getPassword()));
    $session->set('db_name', $site->getDatabaseName())
    
    $user = $this->userService->getById(1);

    dump($user);
    //what you want to do ...

    $this->kernel->shutdown(); //Running All bundles shutdown and container set null        
    $this->kernel->boot(); // services_resetter->reset and preboot -> loading bundles&Containers and all bundles set container & run boot
}
Reichert answered 1/8, 2023 at 14:28 Comment(0)
P
0

In the hopes that this might help someone coming here with Doctrine caching issues.

I had an issue with the OrderBy attribute and the PostLoad event not seeming to fire. The problem was that the entity was already loaded into the EntityManager (from when I created it). Because it was already loaded, subsequent calls to get it went to memory/cache and the expected events wouldn't get fired.

To fix, you can clear the entity out of the EntityManager. Then when you get the entity, the PostLoad callback, OrderBy, etc will fire. You can clear the entity out of the EntityManager like so:

$this->em->clear(MyEntity::class);
Pokorny answered 2/11, 2023 at 1:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.