A
A
Alexander Afanasiev2021-08-06 10:06:39
symfony
Alexander Afanasiev, 2021-08-06 10:06:39

Querying data from a database in a loop with orm symfony behaves strangely, what am I missing?

The essence of the task is to output data from the database to the console in an endless loop.
To do this, I created a command:

class TestCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $doctrine = $this->container->get('doctrine')->getManager();
        $repository = $doctrine->getRepository(MyClass::class);

        while (true) {
            $my_data = $repository->getFewData();
            echo $my_data[0]->getMyField() . "\n";
            sleep(2);
        }
}


And the turnip itself:
public function getFewData()
    {
        return $this->createQueryBuilder('c')
            ->where('c.id = 1')
            ->getQuery()
            ->getResult();
    }


I run the command, everything is displayed. Without turning off the script, I climb into the database, edit my_field (it's just a string) - in the console, the command output does not change.

And if you execute a raw sql query - the data changes on the fly. If you Fulfill the query through the orm, but convert the output to an array - similarly, the data changes on the fly.

What am I missing, is it cached somewhere?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Afanasiev, 2021-08-06
@XanderEVG

Solved the problem by inserting inside the loop: $doctrine->clear();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question