findAll() don't return all elements #6568

Open
opened 2026-01-22 15:35:04 +01:00 by admin · 7 comments
Owner

Originally created by @secit-pl on GitHub (Nov 12, 2020).

composer.json

"doctrine/common": "^2", -> 2.13.3
"doctrine/orm": "^2", -> 2.7.4
"doctrine/doctrine-bundle": "^2", -> 2.2.0

While querying for all elements from the database (SQL Server) not all elements are returned.

The problem was noticed when I was using findAll() to get all elements from the database table:

$doctrine->getRepository('entity name...')->findAll()

After some debugging I noticed that the problem is not in findAll implementation but in processing the DQL query. See the following example (executed from the Symfony command):

  1. lets build the DQL to select specified element (the one which is missed in findAll)
#~> php bin/console doctrine:query:dql "SELECT jd FROM App:Duration jd WHERE jd.id = 27616" --env=prod

 array(1) {
  [0]=>
  object(stdClass)#266 (8) {
    ["__CLASS__"]=>
    string(31) "App\Entity\Duration"
    ["id:App\Entity\Duration:private"]=>
    int(27616)
...
    ["standardDuration:App\Entity\Duration:private"]=>
    string(7) "4800000"
  }
}

all is ok

  1. now query for all and grep for the specified id (27616) in output
#~> php bin/console doctrine:query:dql "SELECT jd FROM App:Duration jd" --env=prod | grep -c 27616
0

Zero matching rows found, so the element with ID 27616 was not returned

  1. now repeat the same procedure from point 2 but using the SQL instead of DQL
#~> php bin/console doctrine:query:sql "SELECT * FROM Journey_Duration" --env=prod | grep -c 27616
1

One record found - the one missing while using the DQL.

ps. Some time ago I noticed the problem that while using findAll() some elements are missing, and in place of them was returned the previous element (the element which is in previous index in array) but after moving to DQL query the problem disappeared. I thing this might be related to the above problem with missing data.

Originally created by @secit-pl on GitHub (Nov 12, 2020). composer.json ``` "doctrine/common": "^2", -> 2.13.3 "doctrine/orm": "^2", -> 2.7.4 "doctrine/doctrine-bundle": "^2", -> 2.2.0 ``` While querying for all elements from the database (SQL Server) not all elements are returned. The problem was noticed when I was using findAll() to get all elements from the database table: ```php $doctrine->getRepository('entity name...')->findAll() ``` After some debugging I noticed that the problem is not in findAll implementation but in processing the DQL query. See the following example (executed from the Symfony command): 1. lets build the DQL to select specified element (the one which is missed in findAll) ``` #~> php bin/console doctrine:query:dql "SELECT jd FROM App:Duration jd WHERE jd.id = 27616" --env=prod array(1) { [0]=> object(stdClass)#266 (8) { ["__CLASS__"]=> string(31) "App\Entity\Duration" ["id:App\Entity\Duration:private"]=> int(27616) ... ["standardDuration:App\Entity\Duration:private"]=> string(7) "4800000" } } ``` all is ok 2. now query for all and grep for the specified id (27616) in output ``` #~> php bin/console doctrine:query:dql "SELECT jd FROM App:Duration jd" --env=prod | grep -c 27616 0 ``` Zero matching rows found, so the element with ID 27616 was not returned 3. now repeat the same procedure from point 2 but using the SQL instead of DQL ``` #~> php bin/console doctrine:query:sql "SELECT * FROM Journey_Duration" --env=prod | grep -c 27616 1 ``` One record found - the one missing while using the DQL. ps. Some time ago I noticed the problem that while using findAll() some elements are missing, and in place of them was returned the previous element (the element which is in previous index in array) but after moving to DQL query the problem disappeared. I thing this might be related to the above problem with missing data.
Author
Owner

@Seb33300 commented on GitHub (Nov 13, 2020):

Any cache enabled in your doctrine config?

@Seb33300 commented on GitHub (Nov 13, 2020): Any cache enabled in your doctrine config?
Author
Owner

@secit-pl commented on GitHub (Nov 13, 2020):

Symfony Doctrine configuration:

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
#        driver: 'pdo_sqlsrv'
#        server_version: '5.7'
        charset: 'UTF-8'
#        default_table_options:
#            charset: utf8mb4
#            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'

        types:
            datetime: App\Doctrine\Type\DateTime

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
parameters:
    memcached_host: localhost
    memcached_port: 11211

doctrine:
    orm:
        metadata_cache_driver:
            type: pool
            pool: doctrine.metadata_cache_driver

        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
            doctrine.metadata_cache_driver:
                adapter: cache.adapter.memcached
                provider: "memcached://%memcached_host%:%memcached_port%"

Entity definition

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
 * Class Duration.
 *
 * @ORM\Entity(repositoryClass="App\Repository\DurationRepository")
 * @ORM\Table(name="Journey_Duration")
 */
class Duration
{
    /**
     * @var int|null
     *
     * @ORM\Column(name="Id", type="integer")
     *
     * @Serializer\Groups({"journey_duration"})
     */
    private $id;

...

}
@secit-pl commented on GitHub (Nov 13, 2020): Symfony Doctrine configuration: ```yaml parameters: # Adds a fallback DATABASE_URL if the env var is not set. # This allows you to run cache:warmup even if your # environment variables are not available yet. # You should not need to change this value. env(DATABASE_URL): '' doctrine: dbal: # configure these for your database server # driver: 'pdo_sqlsrv' # server_version: '5.7' charset: 'UTF-8' # default_table_options: # charset: utf8mb4 # collate: utf8mb4_unicode_ci url: '%env(resolve:DATABASE_URL)%' types: datetime: App\Doctrine\Type\DateTime orm: auto_generate_proxy_classes: '%kernel.debug%' naming_strategy: doctrine.orm.naming_strategy.underscore auto_mapping: true mappings: App: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' alias: App ``` ```yaml parameters: memcached_host: localhost memcached_port: 11211 doctrine: orm: metadata_cache_driver: type: pool pool: doctrine.metadata_cache_driver 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 doctrine.metadata_cache_driver: adapter: cache.adapter.memcached provider: "memcached://%memcached_host%:%memcached_port%" ``` Entity definition ```php <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation as Serializer; /** * Class Duration. * * @ORM\Entity(repositoryClass="App\Repository\DurationRepository") * @ORM\Table(name="Journey_Duration") */ class Duration { /** * @var int|null * * @ORM\Column(name="Id", type="integer") * * @Serializer\Groups({"journey_duration"}) */ private $id; ... } ```
Author
Owner

@Seb33300 commented on GitHub (Nov 13, 2020):

Yes, it looks like you have configured doctrine to use cache.

You should try to clear your cache and test again:
https://symfony.com/doc/current/cache.html#clearing-the-cache

@Seb33300 commented on GitHub (Nov 13, 2020): Yes, it looks like you have configured doctrine to use cache. You should try to clear your cache and test again: https://symfony.com/doc/current/cache.html#clearing-the-cache
Author
Owner

@secit-pl commented on GitHub (Nov 13, 2020):

After each deploy I'm clearing the cache with a following commands:

php bin/console cache:clear --env=prod --no-warmup
php bin/console doctrine:cache:clear-metadata --env=prod
php bin/console doctrine:cache:clear-query --env=prod
php bin/console doctrine:cache:clear-result --env=prod

The output:

 // Clearing the cache for the prod environment with debug                      
 // false                                                                       

 [OK] Cache for the "prod" environment (debug=false) was successfully cleared.  


 // Clearing all Metadata cache entries                                         

 [OK] Successfully deleted cache entries.                                       


 // Clearing all Query cache entries                                            

 [OK] Successfully deleted cache entries.                                       


 // Clearing all Result cache entries                                           

 [OK] Successfully deleted cache entries.  
@secit-pl commented on GitHub (Nov 13, 2020): After each deploy I'm clearing the cache with a following commands: ```bash php bin/console cache:clear --env=prod --no-warmup php bin/console doctrine:cache:clear-metadata --env=prod php bin/console doctrine:cache:clear-query --env=prod php bin/console doctrine:cache:clear-result --env=prod ``` The output: ``` // Clearing the cache for the prod environment with debug // false [OK] Cache for the "prod" environment (debug=false) was successfully cleared. // Clearing all Metadata cache entries [OK] Successfully deleted cache entries. // Clearing all Query cache entries [OK] Successfully deleted cache entries. // Clearing all Result cache entries [OK] Successfully deleted cache entries. ```
Author
Owner

@Seb33300 commented on GitHub (Nov 13, 2020):

Something weird may have happened after the deployment.
In my case, every time I am encountering an issue like this one, clearing the doctrine cache solve it.

@Seb33300 commented on GitHub (Nov 13, 2020): Something weird may have happened after the deployment. In my case, every time I am encountering an issue like this one, clearing the doctrine cache solve it.
Author
Owner

@beberlei commented on GitHub (Nov 13, 2020):

Why are you using the result cache? Is that needed to scale your project?

@beberlei commented on GitHub (Nov 13, 2020): Why are you using the result cache? Is that needed to scale your project?
Author
Owner

@secit-pl commented on GitHub (Nov 13, 2020):

@Seb33300 php bin/console cache:pool:clear cache.global_clearer solves the problem.
I don't understand why executing the

php bin/console cache:clear --env=prod --no-warmup
php bin/console doctrine:cache:clear-metadata --env=prod
php bin/console doctrine:cache:clear-query --env=prod
php bin/console doctrine:cache:clear-result --env=prod

was not enough?

@beberlei 1000+ very active users synchronizing application after each action (offline mode support, so the data shot can be very big). Each synchronization is about 40+ serwer requests and makes a lot of db queries. Server is not a high end machine so I need to cache some things to handle requests in reasonable time. Moreover the database is not an ACID compatible (legacy software integration) so I had to make some hacks to handle database inconsistencies.

@secit-pl commented on GitHub (Nov 13, 2020): @Seb33300 `php bin/console cache:pool:clear cache.global_clearer` solves the problem. I don't understand why executing the ``` php bin/console cache:clear --env=prod --no-warmup php bin/console doctrine:cache:clear-metadata --env=prod php bin/console doctrine:cache:clear-query --env=prod php bin/console doctrine:cache:clear-result --env=prod ``` was not enough? @beberlei 1000+ very active users synchronizing application after each action (offline mode support, so the data shot can be very big). Each synchronization is about 40+ serwer requests and makes a lot of db queries. Server is not a high end machine so I need to cache some things to handle requests in reasonable time. Moreover the database is not an ACID compatible (legacy software integration) so I had to make some hacks to handle database inconsistencies.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6568