Skip to content

Commit

Permalink
Failed getting entity with fetch eager property
Browse files Browse the repository at this point in the history
  • Loading branch information
dbannik committed Nov 28, 2023
1 parent 1a4fe6e commit a34f0d7
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"mobile"="MobileRemoteControl"})
* @ORM\Table(name="test_control")
*/
abstract class AbstractRemoveControl
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* /**
*
* @ORM\Column(type="string")
*
* @var string
*/
public $name;

/**
* @ORM\OneToMany(targetEntity="User", mappedBy="remoteControl", fetch="EAGER")
*
* @var Collection<User>
*/
public $users;

public function __construct(string $name)
{
$this->name = $name;
$this->users = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class MobileRemoteControl extends AbstractRemoveControl
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Table(name="test_user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="AbstractRemoveControl", inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*
* @var AbstractRemoveControl
*/
public $remoteControl;

public function __construct(AbstractRemoveControl $control)
{
$this->remoteControl = $control;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"mobile"="MobileRemoteControl"})
* @ORM\Table(name="test_control")
*/
abstract class AbstractRemoveControl
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* /**
*
* @ORM\Column(type="string")
*
* @var string
*/
public $name;

/**
* @ORM\OneToMany(targetEntity="User", mappedBy="remoteControl")
*
* @var Collection<User>
*/
public $users;

public function __construct(string $name)
{
$this->name = $name;
$this->users = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class MobileRemoteControl extends AbstractRemoveControl
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Table(name="test_user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="AbstractRemoveControl", inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*
* @var AbstractRemoveControl
*/
public $remoteControl;

public function __construct(AbstractRemoveControl $control)
{
$this->remoteControl = $control;
}
}
58 changes: 58 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/AbstractFetchEagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager\AbstractRemoveControl;
use Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager\MobileRemoteControl;
use Doctrine\Tests\Models\AbstractFetchEager\WithFetchEager\User;
use Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager\AbstractRemoveControl as AbstractRemoveControlWithoutFetchEager;
use Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager\MobileRemoteControl as MobileRemoteControlWithoutFetchEager;
use Doctrine\Tests\Models\AbstractFetchEager\WithoutFetchEager\User as UserWithoutFetchEager;
use Doctrine\Tests\OrmFunctionalTestCase;

final class AbstractFetchEagerTest extends OrmFunctionalTestCase
{
public function testWithAbstractFetchEager(): void
{
$this->createSchemaForModels(
AbstractRemoveControl::class,
User::class
);

$control = new MobileRemoteControl('smart');
$user = new User($control);

$this->_em->persist($control);
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find(User::class, $user->id);

self::assertNotNull($user);
self::assertEquals('smart', $user->remoteControl->name);
}

public function testWithoutAbstractFetchEager(): void
{
$this->createSchemaForModels(
AbstractRemoveControlWithoutFetchEager::class,
UserWithoutFetchEager::class
);

$control = new MobileRemoteControlWithoutFetchEager('smart');
$user = new UserWithoutFetchEager($control);

$this->_em->persist($control);
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find(UserWithoutFetchEager::class, $user->id);

self::assertNotNull($user);
self::assertEquals('smart', $user->remoteControl->name);
}
}

0 comments on commit a34f0d7

Please sign in to comment.