Skip to content
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"php": ">=8.1",
"cycle/database": "^2.11",
"cycle/orm": "^2.7",
"psr/container": "^2.0"
"psr/container": "^2.0",
"yiisoft/injector": "^1.2"
},
"require-dev": {
"buggregator/trap": "^1.5",
Expand Down
142 changes: 71 additions & 71 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<MixedArgument>
<code><![CDATA[$primaryKey]]></code>
</MixedArgument>
<PossiblyUnusedReturnValue>
<code><![CDATA[TResult]]></code>
</PossiblyUnusedReturnValue>
<MixedReturnStatement>
<code><![CDATA[static::getOrm()->getSchema()->define(static::class, SchemaInterface::TABLE)]]></code>
</MixedReturnStatement>
</file>
<file src="src/Facade.php">
<MixedReturnStatement>
Expand All @@ -19,8 +19,8 @@
</InvalidThrow>
</file>
<file src="src/Repository/ActiveRepository.php">
<MixedArgument>
<ArgumentTypeCoercion>
<code><![CDATA[$id]]></code>
</MixedArgument>
</ArgumentTypeCoercion>
</file>
</files>
21 changes: 19 additions & 2 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
use Cycle\ORM\Exception\RunnerException;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\RepositoryInterface;
use Cycle\ORM\SchemaInterface;

/**
* A base class for entities that are managed by the ORM.
* Adds a set of ActiveRecord methods to the extending entity class.
*/
abstract class ActiveRecord
{
/**
* Get the table name associated with the entity.
*
* @return non-empty-string
*/
final public static function tableName(): string
{
return static::getOrm()->getSchema()->define(static::class, SchemaInterface::TABLE);
}

/**
* Create a new entity instance with the given data.
* It is preferable to use this method instead of the constructor because
Expand Down Expand Up @@ -142,8 +153,14 @@ final public static function groupActions(
* will be used.
*
* @template TResult
* @param callable(DatabaseInterface, EntityManagerInterface): TResult $callback Note that the provided
* Entity Manager doesn't collect operations and executes them right away in the opened transaction.
* @param callable(): TResult $callback A function that may accept parameters of the following types in any order:
* - {@see DatabaseInterface}
* - {@see EntityManagerInterface}
* - {@see ORMInterface}
* - {@see SchemaInterface}
* @psalm-param callable(...): TResult $callback
* @note that the provided Entity Manager doesn't collect operations and executes them right away in the opened transaction.
*
* @return TResult
*
* @throws TransactionException
Expand Down
9 changes: 6 additions & 3 deletions src/Internal/TransactionFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cycle\ORM\Service\SourceProviderInterface;
use Cycle\ORM\Transaction\Runner;
use Cycle\ORM\Transaction\UnitOfWork;
use Yiisoft\Injector\Injector;

/**
* @internal
Expand Down Expand Up @@ -65,8 +66,9 @@

/**
* @template TResult
* @param callable(DatabaseInterface, EntityManagerInterface): TResult $callback
* @param callable(): TResult $callback
* @param class-string|null $entity If null, the default database will be used.
* @psalm-param callable(...): TResult $callback
* @return TResult
*
* @throws TransactionException
Expand All @@ -83,15 +85,16 @@
->getSource($entity)
->getDatabase();

return $dbal->transaction(static function (DatabaseInterface $db) use ($callback): mixed {

Check failure on line 88 in src/Internal/TransactionFacade.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

MixedReturnStatement

src/Internal/TransactionFacade.php:88:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
$previous = self::$em;
try {
$orm = Facade::getOrm();
self::$em = $em = new EntityManager(
static fn(): UnitOfWork => new UnitOfWork(Facade::getOrm(), Runner::outerTransaction(strict: true)),
static fn(): UnitOfWork => new UnitOfWork($orm, Runner::outerTransaction(strict: true)),
autoExecute: true,
);

return $callback($db, $em);
return (new Injector())->invoke($callback, [$db, $em, $orm, $orm->getHeap(), $orm->getSchema()]);
} finally {
self::$em = $previous;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Repository/ActiveRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function __construct(string $role)
*
* @note Limit of 1 will be added to the query.
*
* @param string|int|non-empty-list<string|int>|non-empty-array<non-empty-string, string|int>|object $id
*
* @return TEntity|null
*/
public function findByPK(mixed $id): ?object
Expand Down
24 changes: 24 additions & 0 deletions tests/src/Functional/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Cycle\Database\DatabaseInterface;
use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\Exception\RunnerException;
use Cycle\ORM\Heap\HeapInterface;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Select\Repository;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -283,9 +286,30 @@ public function it_runs_transaction_with_orm_actions(): void
self::assertSame($savedUserFour->name, $user4->name);
}

#[Test]
public function transact_method_resolves_parameters(): void
{
$ars = User::transact(static fn(
SchemaInterface $schema,
EntityManagerInterface $em,
ORMInterface $orm,
HeapInterface $heap,
DatabaseInterface $dbal,
): array => \func_get_args());

self::assertIsArray($ars);
}

#[Test]
public function query_method_returns_ActiveQuery(): void
{
self::assertInstanceOf(ActiveQuery::class, Identity::query());
}

#[Test]
public function get_table_name(): void
{
self::assertSame('user', User::tableName());
self::assertSame('user_identity', Identity::tableName());
}
}
Loading