Unexpected behavior of refresh() method
Opened this issue · 0 comments
cebe commented
When a record is loaded using the EntityManager, and afterwards some changes are made to the database without using the EntityManager, I see no way to reload the record to retrieve fresh information from the DB. I was expecting the refresh()
method to do this, but it does not work. Also it seems there is no documentation about this method.
I demonstrate the problem in form of a test case using the code from OneToManyRelationshipEntityTest
:
// create initial data
$owner = new Owner('M');
$house1 = new House();
$house1->setAddress('A Street 1');
$this->persist($owner, $house1);
$this->em->flush();
$this->assertGraphExist('(o:Owner {name:"M"})');
$this->assertGraphExist('(h:House {address: "A Street 1"})');
$this->em->flush();
$this->em->clear();
// load owner instance
/** @var Owner $me */
$me = $this->em->getRepository(Owner::class)->findOneBy(['name' => 'M']);
$this->assertSame(0, $me->getAcquisitions()->count());
// make some changes to the db
$this->em->getDatabaseDriver()->run('MATCH (o:Owner {name:"M"}), (h:House {address: "A Street 1"}) CREATE (o)-[r:ACQUIRED {year: 1980}]->(h)');
$this->assertGraphExist('(o:Owner {name:"M"})-[r:ACQUIRED {year: 1980}]->(h:House {address: "A Street 1"})');
// this line should fetch a new record, but somehow it uses a chaced one, as the assertation below fails
$me = $this->em->getRepository(Owner::class)->findOneBy(['name' => 'M']);
// uncommenting this line complains that the entity is not managed by the entity manager, however if it is cached, I expect it to be refreshed by this.
//$this->em->refresh($me);
$this->assertSame(1, $me->getAcquisitions()->count());
Any idea what I am doing wrong? Not sure if its a bug or I am missing something but at least some documentation is needed on how to properly refresh entities. Thanks!
I'll submit a PR with the failing test.