John Davidson

php - symfony 4 entity manager flush doesn't work sometimes


I'm not an expert at symfony and some help would be much appreciated.

I'm using an API to receive live match data(sports). I get the data and update it every minute, but somehow it gets updated every 8 to 15 minutes on live. (I was using predis for cache, removed it, still the same). It's also not updated on the db, I check to see if they're updated with dump, they look fine.

Code I'm using is;

        foreach ($response['result'] as $result) {
$findFixture = $this->em->getRepository('App:Fixtures')->findOneBy(['fixtureId' => $result['fixture_id']]);
$cannotFind[] = $result['fixture_id'];
$updateFixture[$findFixture->getFixtureId()]['elapsedFirst'] = $findFixture->getElapsed();

$eventDate = new DateTime();
$eventDate->setTimezone(new \DateTimeZone('Europe/Istanbul'));

$firstHalfStart = new DateTime();
$eventDate->setTimezone(new \DateTimeZone('Europe/Istanbul'));

$secondHalfStart = new DateTime();
$eventDate->setTimezone(new \DateTimeZone('Europe/Istanbul'));

if($findFixture->getElapsed() !== $result['elapsed']){
$persist = true;
$persist = false;


if($persist) $this->em->persist($findFixture);
$updateFixture[$findFixture->getFixtureId()]['persist'] = $persist;

if(isset($result['events']) && is_array($result['events'])){

foreach ($result['events'] AS $k => $v){
$result['events'][$k]['fixture_id'] = $findFixture->getFixtureId();

$this->renderFixtureEvents([$findFixture->getFixtureId() => $result['events'] ],false);

$updateFixture[$findFixture->getFixtureId()]['elapsedLast'] = $result['elapsed'];

Thanks a lot in advance.

My Answer:
In Symfony 4, you can use the entity manager to persist changes to your entities and flush them to the database. To do this, you can call the `flush()` method on the entity manager object.

Here's an example of how you can use the entity manager to persist changes and flush them:

// Get the entity manager
$entityManager = $this->getDoctrine()->getManager();

// Create a new entity
$entity = new YourEntity();

// Persist the entity

// Flush the changes to the database

In this example, we first get the entity manager using `$this->getDoctrine()->getManager()`. Then, we create a new entity object and set its properties. We use the `persist()` method to tell the entity manager to keep track of this entity. Finally, we call the `flush()` method to save the changes to the database.

Make sure to replace `YourEntity` with the name of your entity class and `setProperty('value')` with the actual property and value you want to set.

Rate this post

3 of 5 based on 8467 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us