Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Pckg/Database/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function __construct(Repository $repository = null, $alias = null, $boot
$this->repository = $repository;

$this->guessDefaults();

if ($boot) {
$this->initExtensions();
$this->preboot();
Expand Down Expand Up @@ -539,7 +539,9 @@ public function one()
{
$this->applyExtensions();

$one = $this->repository->one($this);
$repository = $this->getRepository()->aliased('read');

$one = $repository->one($this);

$this->resetQuery();

Expand Down Expand Up @@ -577,7 +579,9 @@ public function all()
{
$this->applyExtensions();

$all = $this->repository->all($this);
$repository = $this->getRepository()->aliased('read');

$all = $repository->all($this);

$this->resetQuery();

Expand Down Expand Up @@ -663,6 +667,8 @@ public function delete(Repository $repository = null)
$repository = $this->getRepository();
}

$repository = $repository->aliased('write');

$delete = $this->getQuery()->transformToDelete();

$prepare = $repository->prepareQuery($delete);
Expand All @@ -681,6 +687,8 @@ public function insert(Repository $repository = null)
$repository = $this->getRepository();
}

$repository = $repository->aliased('write');

$insert = $this->getQuery()->transformToInsert();

$prepare = $repository->prepareQuery($insert);
Expand Down Expand Up @@ -711,6 +719,8 @@ public function update(Repository $repository = null)
$repository = $this->getRepository();
}

$repository = $repository->aliased('write');

$update = $this->getQuery()->transformToUpdate();

$update->setSet($this->setData);
Expand Down
2 changes: 2 additions & 0 deletions src/Pckg/Database/Record/Extension/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function deleteTranslation($language, Entity $entity = null, Repository $
$repository = $entity->getRepository();
}

$repository = $repository->aliased('write');

$deleted = $repository->deleteTranslation($this, $entity, $language);

return $deleted;
Expand Down
28 changes: 28 additions & 0 deletions src/Pckg/Database/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ abstract class AbstractRepository implements Repository
*/
protected $cache;

/**
* @var array
*/
protected $aliases = [];

/**
* @return PDO
*/
Expand All @@ -30,6 +35,29 @@ public function getConnection()
return $this->connection;
}

/**
* @param $alias
* @param Repository $repository
*
* @return $this
*/
public function addAlias($alias, Repository $repository)
{
$this->aliases[$alias] = $repository;

return $this;
}

/**
* @param $alias
*
* @return mixed|AbstractRepository
*/
public function aliased($alias)
{
return $this->aliases[$alias] ?? $this;
}

/**
* @param \PDO $connection
*/
Expand Down
10 changes: 0 additions & 10 deletions src/Pckg/Database/Repository/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,6 @@ public function executePrepared($prepare)
{
$execute = $prepare->execute();

/*$caller = db(0, 6, false)[0];
message(
'query in ' .
($caller['class'] ?? null) .
($caller['type'] ?? null) .
($caller['function'] ?? null) . ':' .
($caller['line'] ?? null),
'optimize'
);*/

if (!$execute) {
$errorInfo = $prepare->errorInfo();

Expand Down
2 changes: 1 addition & 1 deletion src/Pckg/Database/Repository/PDO/Command/DeleteRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(Record $record, Entity $entity, Repository $reposito
{
$this->record = $record;
$this->entity = $entity;
$this->repository = $repository;
$this->repository = $repository->aliased('write');
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Pckg/Database/Repository/PDO/Command/GetRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class GetRecords
public function __construct(Entity $entity, Repository $repository = null)
{
$this->entity = $entity;
$this->repository = $repository
?: $this->entity->getRepository();
$this->repository = ($repository
?? $this->entity->getRepository())->aliased('read');
}

/**
Expand Down Expand Up @@ -61,6 +61,7 @@ public function executeAll()
$collection->setEntity($entity)->setSaved()->setOriginalFromData();

stopMeasure('Executing ' . $measure);

return $entity->fillCollectionWithRelations($collection);
}
stopMeasure('Executing ' . $measure);
Expand All @@ -86,6 +87,7 @@ public function executeOne()
$record->setEntity($entity)->setSaved()->setOriginalFromData();

stopMeasure('Executing ' . $measure);

return $entity->fillRecordWithRelations($record);
}
stopMeasure('Executing ' . $measure);
Expand Down
2 changes: 1 addition & 1 deletion src/Pckg/Database/Repository/PDO/Command/InsertRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Record $record, Entity $entity, Repository $reposito
{
$this->record = $record;
$this->entity = $entity;
$this->repository = $repository;
$this->repository = $repository->aliased('write');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Pckg/Database/Repository/PDO/Command/UpdateRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Record $record, Entity $entity, Repository $reposito
{
$this->record = $record;
$this->entity = $entity;
$this->repository = $repository;
$this->repository = $repository->aliased('write');
}

/**
Expand Down
23 changes: 18 additions & 5 deletions src/Pckg/Database/Repository/RepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static function getOrCreateRepository($name)
if (array_key_exists($name, static::$repositories)) {
$repository = static::$repositories[$name];
} else {
if (!context()->exists($name)) {
$fullName = Repository::class . '.' . $name;
if (!context()->exists($fullName)) {
/**
* Lazy load.
*/
Expand All @@ -53,10 +54,10 @@ public static function getOrCreateRepository($name)
throw new Exception("No config found for database connection " . $name);
}
$repository = RepositoryFactory::initPdoDatabase($config, $name);
context()->bind($name, $repository);
context()->bind($fullName, $repository);
}

$repository = context()->get($name);
$repository = context()->get($fullName);
}

if (!$repository) {
Expand Down Expand Up @@ -121,7 +122,7 @@ protected static function checkDebugBar($pdo, $name)
$debugBar->addCollector($pdoCollector = new PDOCollector());
}

$pdoCollector->addConnection($tracablePdo, $name);
$pdoCollector->addConnection($tracablePdo, str_replace(':', '-', $name));
}
}

Expand Down Expand Up @@ -168,7 +169,19 @@ public static function createRepositoryConnection($config, $name)
/**
* Bind repository to context so we can reuse it later.
*/
context()->bindIfNot(Repository::class, $repository);
if ($pos = strpos($name, ':')) {
/**
* We're probably initializing write connection.
*/
$originalAlias = Repository::class . '.' . substr($name, 0, $pos);
$originalRepository = context()->get($originalAlias);
$alias = substr($name, $pos + 1);
$originalRepository->addAlias($alias, $repository);
$repository->addAlias($alias == 'write' ? 'read' : 'write', $originalRepository);
} else {
context()->bindIfNot(Repository::class, $repository);
}

context()->bind(Repository::class . '.' . $name, $repository);

return $repository;
Expand Down