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
11 changes: 10 additions & 1 deletion src/Traits/HasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Model;
use LogicException;
use Michalsn\CodeIgniterNestedModel\Enums\RelationTypes;
Expand Down Expand Up @@ -396,7 +397,15 @@ protected function relationsAfterFind(array $eventData): array
}
} else {
foreach ($this->relations as $relationName => $relationObject) {
$ids = array_column($eventData['data'], $relationObject->primaryKey);
$ids = array_unique(array_column(
array_map(
static fn ($item) => $item instanceof Entity
? $item->toRawArray()
: $item,
$eventData['data'],
),
$relationObject->primaryKey,
));
$relationData = $this->getDataForRelationByIds($ids, $relationObject, $relationName);

foreach ($eventData['data'] as &$data) {
Expand Down
46 changes: 46 additions & 0 deletions tests/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\Database\Seeds\SeedTests;
use Tests\Support\Entities\Comment;
use Tests\Support\Entities\Post;
use Tests\Support\Entities\User;
use Tests\Support\Models\CommentModel;
use Tests\Support\Models\PostModel;
use Tests\Support\Models\UserModel;

Expand Down Expand Up @@ -139,4 +141,48 @@ public function testFindBelongsTo()
$this->assertSame('1', $post->user->id);
$this->assertSame('Test User 1', $post->user->username);
}

public function testFindBelongsToWithEntityDatamap()
{
// Load normal model
$comment = model(CommentModel::class)->find(1);
$this->assertInstanceOf(Comment::class, $comment);

$isset = isset($comment->user);
$this->assertFalse($isset);

// Load model with relation
$comment = model(CommentModel::class)->with('user')->find(1);
$this->assertInstanceOf(Comment::class, $comment);

$isset = isset($comment->user);
$this->assertTrue($isset);

$this->assertInstanceOf(User::class, $comment->user);

$this->assertSame('1', $comment->user->id);
$this->assertSame('Test User 1', $comment->user->username);
}

public function testFindAllBelongsToWithEntityDatamap()
{
// Load normal model
$comments = model(CommentModel::class)->findAll();
$this->assertInstanceOf(Comment::class, $comments[0]);

$isset = isset($comments[0]->user);
$this->assertFalse($isset);

// Load model with relation
$comments = model(CommentModel::class)->with('user')->findAll();
$this->assertInstanceOf(Comment::class, $comments[0]);

$isset = isset($comments[0]->user);
$this->assertTrue($isset);

$this->assertInstanceOf(User::class, $comments[0]->user);

$this->assertSame('1', $comments[0]->user->id);
$this->assertSame('Test User 1', $comments[0]->user->username);
}
}
8 changes: 5 additions & 3 deletions tests/_support/Entities/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

class Comment extends Entity
{
protected $datamap = [];
protected $dates = ['created_at', 'updated_at'];
protected $casts = [];
protected $datamap = [
'userId' => 'user_id',
];
protected $dates = ['created_at', 'updated_at'];
protected $casts = [];
}