我使用类表继承继承了三个级别,如下所示:
班级考试
namespace App\Entities\Test;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Test
* @package App\Entities\Test
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"TestA" = "TestA", "TestB" = "TestB"})
* @ORM\Table(name="test")
*/
abstract class Test {
/**
* @var integer
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $columnTest;
}
类 TestM 扩展了 Test
namespace App\Entities\Test;
use Doctrine\ORM\Mapping as ORM;
/**
* Class TestM
* @package App\Entities\Test
* @ORM\Entity
*/
abstract class TestM extends Test{
/**
* @var string
* @ORM\Column(type="string")
*/
protected $columnTestM;
}
TestA 类扩展了 TestM
namespace App\Entities\Test;
use Doctrine\ORM\Mapping as ORM;
/**
* Class TestA
* @package App\Entities\Test
* @ORM\Entity
*/
class TestA extends TestM{
/**
* @var string
* @ORM\Column(type="string")
*/
private $columnTestA;
public function __construct(string $columnTest, string $columnTestM, string $columnTestA) {
$this->columnTest = $columnTest;
$this->columnTestM = $columnTestM;
$this->columnTestA = $columnTestA;
}
/**
* @return string
*/
public function getColumnTest(): string {
return $this->columnTest;
}
/**
* @param string $columnTest
*/
public function setColumnTest(string $columnTest): void {
$this->columnTest = $columnTest;
}
/**
* @return string
*/
public function getColumnTestM(): string {
return $this->columnTestM;
}
/**
* @param string $columnTestM
*/
public function setColumnTestM(string $columnTestM): void {
$this->columnTestM = $columnTestM;
}
/**
* @return string
*/
public function getColumnTestA(): string {
return $this->columnTestA;
}
/**
* @param string $columnTestA
*/
public function setColumnTestA(string $columnTestA): void {
$this->columnTestA = $columnTestA;
}
}
我遇到了问题,因为当我要从数据库中检索实体时,它带有没有数据的第二层,只有第一层和最后一层带有所有数据。请注意 columnTestM 是空白的。我错过了什么?它在所有三个级别的数据中都存在,问题只是当我必须得到它时。作为示例,我将列的内容与它自己的名称放在一起
>>> print_r(\EntityManager::getRepository('App\Entities\Test\Test')->find(1));
App\Entities\Test\TestA Object
(
[columnTestA:App\Entities\Test\TestA:private] => columnTestA
[columnTestM:protected] =>
[id:protected] => 1
[columnTest:protected] => columnTest
)
编辑:
也许这是一个错误,我在 GitHub 上提交了一个关于错误查询生成的问题。我让 MariaDB 记录所有查询,以检查当我尝试检索数据时生成的查询,结果如下:
SELECT
t0.id AS id_3,
t0.column_test AS column_test_4,
t0.discr,
t1.column_test_a AS column_test_a_5,
t2.column_test_b AS column_test_b_6
FROM
test t0
LEFT JOIN
test_as t1 ON t0.id = t1.id
LEFT JOIN
test_bs t2 ON t0.id = t2.id
WHERE
t0.id = 1
它尝试与 TestB 离开连接,而不是与 TestM 进行连接