Skip to content
Closed
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
9 changes: 8 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,17 @@
<InvalidThrow>
<code><![CDATA[throw $state->getLastError()]]></code>
</InvalidThrow>
<PossiblyUnusedReturnValue>
<code><![CDATA[StateInterface]]></code>
</PossiblyUnusedReturnValue>
<PropertyNotSetInConstructor>
<code><![CDATA[$unitOfWork]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/EntityManagerInterface.php">
<PossiblyUnusedReturnValue>
<code><![CDATA[StateInterface]]></code>
<code><![CDATA[self]]></code>
<code><![CDATA[static]]></code>
</PossiblyUnusedReturnValue>
</file>
Expand Down Expand Up @@ -2341,7 +2346,6 @@
<code><![CDATA[$row]]></code>
<code><![CDATA[$self->scope]]></code>
<code><![CDATA[$this->morphKey]]></code>
<code><![CDATA[$where[$this->outerKey[$i]]]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[parseRow]]></code>
Expand All @@ -2350,6 +2354,9 @@
<code><![CDATA[$grouped]]></code>
<code><![CDATA[array<non-empty-string, array<non-empty-string, mixed>>]]></code>
</MixedReturnTypeCoercion>
<PossiblyNullOperand>
<code><![CDATA[$this->fieldAlias($this->outerKey[$i])]]></code>
</PossiblyNullOperand>
<PropertyNotSetInConstructor>
<code><![CDATA[BelongsToMorphedLoader]]></code>
<code><![CDATA[BelongsToMorphedLoader]]></code>
Expand Down
5 changes: 1 addition & 4 deletions src/Select/Loader/Morphed/BelongsToMorphedLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,10 @@ public function isLoaded(): bool
private function applyCriteria(SelectQuery $query, array $criteria): SelectQuery
{
// Map criteria to inner keys
$where = [];
foreach ($this->innerKey as $i => $key) {
$where[$this->outerKey[$i]] = $criteria[$key];
$query->where($this->getAlias() . '.' . $this->fieldAlias($this->outerKey[$i]), $criteria[$key]);
}

$query->where(\key($where), \reset($where));

return $query;
}

Expand Down
24 changes: 13 additions & 11 deletions tests/ORM/Functional/Driver/Common/Integration/Case428/CaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428;

use Cycle\ORM\EntityManager;
use Cycle\ORM\Select;
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait;
Expand Down Expand Up @@ -32,17 +31,14 @@ public function testCreate(): void
$user = new Entity\User('Test User', 'test@example.com');
$post = new Entity\Post('New title', 'New content');
$post->user = $user;
$user->id = 42;

$em = new EntityManager($this->orm);
$em->persist($post);
$em->run();
$this->save($post);

$post->best_comment = new Entity\Comment(42, 'New comment content', $post, $user);

// Store changes and calc write queries
$em = new EntityManager($this->orm);
$em->persist($post);
$em->run();
$this->save($post);

// Check write queries count
$this->orm->getHeap()->clean();
Expand All @@ -64,15 +60,15 @@ public function setUp(): void
private function makeTables(): void
{
$this->makeTable('user', [
'id' => 'int',
'id' => 'int,primary',
'name' => 'string',
'email' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
]);

$this->makeTable('category', [
'id' => 'int',
'id' => 'int,primary',
'name' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
Expand All @@ -91,7 +87,7 @@ private function makeTables(): void
]);

$this->makeTable('comment', [
'id' => 'int',
'id' => 'int,primary',
'content' => 'string',
'post_id' => 'int',
'user_id' => 'int',
Expand Down Expand Up @@ -128,7 +124,7 @@ private function fillData(): void
$this->getDatabase()->table('post')->insertMultiple(
['title', 'content', 'best_comment_id', 'user_id', 'category_id', 'data'],
[
['Title 1', 'Foo-bar-baz content 1', 2, 1, 1, 'metadata'],
['Title 1', 'Foo-bar-baz content 1', null, 1, 1, 'metadata'],
],
);

Expand All @@ -140,5 +136,11 @@ private function fillData(): void
[3, 1, 1, 'Foo-bar-baz comment 3'],
],
);

$this->getDatabase()->table('post')->update([
'best_comment_id' => 2,
], [
'id' => 1,
])->run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testSelectAll(): void
$this->captureReadQueries();
foreach ($logs as $log) {
$this->assertInstanceOf(Entity\LogRecord::class, $log);
\in_array($log->id, [9], true)
\in_array($log->lid, [9], true)
? self::assertNull($log->actor)
: self::assertInstanceOf(Entity\Actor::class, $log->actor);
}
Expand All @@ -80,6 +80,7 @@ public function testUpdateMany(): void
{
// Eager load morphed relation
$this->captureReadQueries();
/** @var list<Entity\LogRecord> $logs */
$logs = (new Select($this->orm, Entity\LogRecord::class))
->fetchAll();
$this->assertNumReads(1);
Expand All @@ -95,7 +96,7 @@ public function testUpdateMany(): void
// Check result
$this->captureReadQueries();
foreach ($logs as $log) {
\in_array($log->id, [9], true)
\in_array($log->lid, [9], true)
? self::assertNull($log->actor)
: self::assertInstanceOf(Entity\Actor::class, $log->actor);
}
Expand All @@ -110,7 +111,7 @@ public function testScoped(): void
$logs = (new Select($this->orm, Entity\LogRecord::class))
->load('actor')
->wherePK(9, 10)
->orderBy('id')
->orderBy('l_id')
->fetchAll();
$this->assertNumReads(3);

Expand All @@ -136,19 +137,19 @@ public function setUp(): void
private function makeTables(): void
{
$this->makeTable(Entity\LogRecord::ROLE, [
'id' => 'primary', // autoincrement
'message' => 'string',
'actor_id' => 'int,nullable',
'actor_type' => 'string,nullable',
'created_at' => 'datetime',
'l_id' => 'primary', // autoincrement
'l_message' => 'string',
'l_actor_id' => 'int,nullable',
'l_actor_type' => 'string,nullable',
'l_created_at' => 'datetime',
]);

$this->makeTable(Entity\User::ROLE, [
// The columns order is matters here for testSelectAll purpose
'active' => 'bool',
'name' => 'string',
'id' => 'primary',
'created_at' => 'datetime',
'u_active' => 'bool',
'u_name' => 'string',
'u_id' => 'primary',
'u_created_at' => 'datetime',
]);

$this->makeTable(Entity\Tenant::ROLE, [
Expand All @@ -161,7 +162,7 @@ private function makeTables(): void
private function fillData(): void
{
$this->getDatabase()->table(Entity\User::ROLE)->insertMultiple(
['name', 'active'],
['u_name', 'u_active'],
[
['user-1', true],
['user-2', true],
Expand All @@ -181,7 +182,7 @@ private function fillData(): void
],
);
$this->getDatabase()->table(Entity\LogRecord::ROLE)->insertMultiple(
['message', 'actor_type', 'actor_id', 'created_at'],
['l_message', 'l_actor_type', 'l_actor_id', 'l_created_at'],
[
['log-1 for user-1', Entity\User::ROLE, 1, new \DateTimeImmutable()],
['log-2 for user-2', Entity\User::ROLE, 2, new \DateTimeImmutable()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class LogRecord
{
public const ROLE = 'log';

public ?int $id = null;
public ?int $lid = null;
public string $message;
public string $actor_type;
public int $actor_id;
Expand Down
24 changes: 13 additions & 11 deletions tests/ORM/Functional/Driver/Common/Integration/Issue356/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
Schema::SOURCE => Source::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'log',
Schema::PRIMARY_KEY => ['id'],
Schema::FIND_BY_KEYS => ['id'],
Schema::PRIMARY_KEY => ['lid'],
Schema::FIND_BY_KEYS => ['lid'],
Schema::COLUMNS => [
'id' => 'id',
'message' => 'message',
'actor_id' => 'actor_id',
'actor_type' => 'actor_type',
'created_at' => 'created_at',
'lid' => 'l_id',
'message' => 'l_message',
'actor_id' => 'l_actor_id',
'actor_type' => 'l_actor_type',
'created_at' => 'l_created_at',
],
Schema::RELATIONS => [
'actor' => [
Expand All @@ -46,7 +46,7 @@
],
Schema::SCOPE => null,
Schema::TYPECAST => [
'id' => 'int',
'lid' => 'int',
'message' => 'string',
'actor_id' => 'int',
'actor_type' => 'string',
Expand All @@ -64,16 +64,18 @@
Schema::PRIMARY_KEY => ['id'],
Schema::FIND_BY_KEYS => ['id'],
Schema::COLUMNS => [
'id' => 'id',
'name' => 'name',
'created_at' => 'created_at',
'id' => 'u_id',
'name' => 'u_name',
'created_at' => 'u_created_at',
'active' => 'u_active',
],
Schema::RELATIONS => [],
Schema::SCOPE => ActiveScope::class,
Schema::TYPECAST => [
'id' => 'int',
'name' => 'string',
'created_at' => 'datetime',
'active' => 'bool',
],
Schema::SCHEMA => [],
],
Expand Down
Loading