Skip to content

Commit

Permalink
Merge branch 'bugfix-1.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnmeurt committed Mar 10, 2022
2 parents 7201a85 + cc297e9 commit 966ca9e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Expand Up @@ -30,6 +30,12 @@ BC Breaks
* Add close method in `Bdf\Prime\Connection\ConnectionInterface` interface.


v1.2.2
------

* Fix: Fix lazy loading of the connection on repository.


v1.2.1
------

Expand Down
31 changes: 23 additions & 8 deletions src/Repository/EntityRepository.php
Expand Up @@ -101,6 +101,11 @@ class EntityRepository implements RepositoryInterface, EventSubscriber, Connecti
*/
protected $writer;

/**
* @var ConnectionInterface|null
*/
protected $connection;


/**
* Constructor
Expand All @@ -120,7 +125,6 @@ public function __construct(Mapper $mapper, ServiceLocator $serviceLocator, ?Cac
$this->writer = new Writer($this, $serviceLocator);

$this->mapper->events($this);
$this->connection()->getEventManager()->addEventSubscriber($this);
}

/**
Expand Down Expand Up @@ -239,7 +243,13 @@ public function extractOne($entity, string $property)
*/
public function connection(): ConnectionInterface
{
return $this->serviceLocator->connection($this->mapper->metadata()->connection);
if ($this->connection === null) {
//Repository query factory load the connection on its constructor. Use lazy to let the connection being loaded as late as possible.
$this->connection = $this->serviceLocator->connection($this->mapper->metadata()->connection);
$this->connection->getEventManager()->addEventSubscriber($this);
}

return $this->connection;
}

/**
Expand Down Expand Up @@ -996,7 +1006,10 @@ public function free($entity)
*/
public function destroy()
{
$this->connection()->getEventManager()->removeEventSubscriber($this);
if ($this->connection !== null) {
$this->connection->getEventManager()->removeEventSubscriber($this);
$this->connection = null;
}

$this->serviceLocator = null;
$this->queries = null;
Expand Down Expand Up @@ -1025,24 +1038,26 @@ public function destroy()
*/
private function changeActiveConnection($connectionName)
{
$this->connection()->getEventManager()->removeEventSubscriber($this);
$this->reset();

/** @var string $original */
$original = $this->mapper->metadata()->connection;
$this->mapper->metadata()->connection = $connectionName;

$this->connection()->getEventManager()->addEventSubscriber($this);
$this->reset();

return $original;
}

/**
* Reset the inner queries
* Reset the inner queries and the connection.
* Use for invalidate prepared queries, or when connection changed
*/
private function reset()
{
if ($this->connection !== null) {
$this->connection->getEventManager()->removeEventSubscriber($this);
$this->connection = null;
}

// Reset queries
$this->queries = new RepositoryQueryFactory($this, $this->resultCache);
$this->writer = new Writer($this, $this->serviceLocator);
Expand Down
10 changes: 2 additions & 8 deletions src/Repository/RepositoryQueryFactory.php
Expand Up @@ -30,11 +30,6 @@ class RepositoryQueryFactory
*/
private $repository;

/**
* @var ConnectionInterface
*/
private $connection;

/**
* @var Metadata
*/
Expand Down Expand Up @@ -102,7 +97,6 @@ public function __construct(RepositoryInterface $repository, ?CacheInterface $re

$this->supportsKeyValue = empty($repository->constraints());

$this->connection = $repository->connection();
$this->queries = $repository->mapper()->queries();
$this->metadata = $repository->metadata();
}
Expand All @@ -128,7 +122,7 @@ public function builder()
*/
public function fromAlias(?string $alias = null)
{
return $this->configure($this->connection->builder(new OrmPreprocessor($this->repository)), $alias);
return $this->configure($this->repository->connection()->builder(new OrmPreprocessor($this->repository)), $alias);
}

/**
Expand All @@ -147,7 +141,7 @@ public function fromAlias(?string $alias = null)
public function make(string $query)
{
/** @psalm-suppress InvalidReturnStatement */
return $this->configure($this->connection->make($query, new OrmPreprocessor($this->repository)));
return $this->configure($this->repository->connection()->make($query, new OrmPreprocessor($this->repository)));
}

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/Repository/EntityRepositoryTest.php
Expand Up @@ -2,7 +2,9 @@

namespace Bdf\Prime\Repository;

use Bdf\Prime\Entity\Model;
use Bdf\Prime\Events;
use Bdf\Prime\Mapper\Mapper;
use Bdf\Prime\Prime;
use Bdf\Prime\PrimeTestCase;
use Bdf\Prime\Right;
Expand Down Expand Up @@ -691,4 +693,61 @@ public function test_close_connection_should_reset_queries()
// Data is removed
$this->assertNull($respository->findById(1));
}

/**
*
*/
public function test_lazy_loading_connection()
{
$this->prime()->connections()->declareConnection('lazytest', [
'adapter' => 'sqlite',
'memory' => true
]);
$this->assertNotContains('lazytest', $this->prime()->connections()->getCurrentConnectionNames());

$respository = TestLazyLoadingConnection::repository();
$this->assertNotContains('lazytest', $this->prime()->connections()->getCurrentConnectionNames());

$entity = new TestLazyLoadingConnection();
$respository->free($entity);

$this->assertNotContains('lazytest', $this->prime()->connections()->getCurrentConnectionNames());

$respository->builder();
$this->assertContains('lazytest', $this->prime()->connections()->getCurrentConnectionNames());
}
}



class TestLazyLoadingConnection extends Model
{
public $id;
}

class TestLazyLoadingConnectionMapper extends Mapper
{
/**
* {@inheritdoc}
*/
public function schema()
{
return [
'connection' => 'lazytest',
'table' => 'test_',
];
}

/**
* {@inheritdoc}
*/
public function buildFields($builder)
{
$builder
->integer('id')
->autoincrement()
;
}


}

0 comments on commit 966ca9e

Please sign in to comment.