原则 3:一对一单向关系不 return 所有领域
Doctrine 3: one-to-one Unidirectional relationship doesn't return all fields
我有 2 个实体:Thesis 和 DoList。
每个 DoList 都属于 0 或 1 个 Thesis。所以我们有一个从 DoList 到 Thesis 的单向一对一关系。这是论文:
class Thesis
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @var int
*/
protected int $thesisID;
和 DoList:
/**
* @ORM\OneToOne(targetEntity="Thesis")
* @ORM\JoinColumn(name="thesisID", referencedColumnName="thesisID", nullable=true)
*/
protected $thesisID;
当然我减少了 类 以仅显示重要的部分。
据我所知。 @ORM\JoinColumn 应该加入表 DoList 和 Thesis on
DoList.thesisID 和 Thesis.thesisID.
我在这个关系中保留 thesisID 没问题但是我无法在从 DoList 中选择所有值时检索 thesisID 的值它总是空的。
编辑:
列名必须两边都是thesisID。
错误是我在使用查询生成器时没有加入 table。
我认为在使用查询构建器原则进行查询并从 table 中选择所有行时,会读取我在模型上指定的关系并自动执行连接查询。
注意:当您在模型上指定该选项时,DOCTRINE 不会自动连接表。您需要在 QUERYBUILDER 上执行连接功能。
$qb->select(['u', 'c'])
->from('ThesisNET\Models\DoList', 'u')
->join('u.thesisID', 'c')
->where('u.instructor = ?1')
->setParameter('1', $username);
这就是它最终的工作方式。 (我刚刚添加了加入选项。)
我有 2 个实体:Thesis 和 DoList。
每个 DoList 都属于 0 或 1 个 Thesis。所以我们有一个从 DoList 到 Thesis 的单向一对一关系。这是论文:
class Thesis
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @var int
*/
protected int $thesisID;
和 DoList:
/**
* @ORM\OneToOne(targetEntity="Thesis")
* @ORM\JoinColumn(name="thesisID", referencedColumnName="thesisID", nullable=true)
*/
protected $thesisID;
当然我减少了 类 以仅显示重要的部分。
据我所知。 @ORM\JoinColumn 应该加入表 DoList 和 Thesis on DoList.thesisID 和 Thesis.thesisID.
我在这个关系中保留 thesisID 没问题但是我无法在从 DoList 中选择所有值时检索 thesisID 的值它总是空的。
编辑:
列名必须两边都是thesisID。
错误是我在使用查询生成器时没有加入 table。 我认为在使用查询构建器原则进行查询并从 table 中选择所有行时,会读取我在模型上指定的关系并自动执行连接查询。
注意:当您在模型上指定该选项时,DOCTRINE 不会自动连接表。您需要在 QUERYBUILDER 上执行连接功能。
$qb->select(['u', 'c'])
->from('ThesisNET\Models\DoList', 'u')
->join('u.thesisID', 'c')
->where('u.instructor = ?1')
->setParameter('1', $username);
这就是它最终的工作方式。 (我刚刚添加了加入选项。)