John Davidson

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

0 comments
Message:


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']]);
if(is_null($findFixture)){
$cannotFind[] = $result['fixture_id'];
continue;
}else{
$updateFixture[$findFixture->getFixtureId()]['elapsedFirst'] = $findFixture->getElapsed();
}

$eventDate = new DateTime();
$eventDate->setTimezone(new \DateTimeZone('Europe/Istanbul'));
$eventDate->setTimestamp($result['event_timestamp']);

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

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

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

$findFixture
->setScore($result['score'])
->setElapsed($result['elapsed'])
->setStatus($result['statusShort'])
->setVenue($result['venue'])
->setReferee($result['referee'])
->setGoalsAwayTeam($result['goalsAwayTeam'])
->setGoalsHomeTeam($result['goalsHomeTeam']);

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);
}

$this->em->flush();
$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:

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

// Create a new entity
$entity = new YourEntity();
$entity->setProperty('value');

// Persist the entity
$entityManager->persist($entity);

// Flush the changes to the database
$entityManager->flush();


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

4 of 5 based on 5622 votes

Comments




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