原则 2 OneToOne 实体映射返回空实体

发布于 2024-11-28 01:06:26 字数 1160 浏览 1 评论 0原文

设置:

我有以下代码两个实体模型:

帐户,代表用户帐户并具有指向教室的外键。

<?php
namespace models;

/**
 * @Entity
 */
class Account
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @OneToOne(targetEntity="Classroom")
     */
    public $classroom;
}

代表学生注册的教室的教室。

<?php
namespace models;

/**
 * @Entity
 */
class Classroom
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Column(type="string")
     */
    public $name;
}

问题:

当我执行$account = $em->find('Account',$id);时,我正确地从数据库取回帐户,但 $account->classroom; 是一个空(非空)对象。经过一番尝试后,我尝试在执行之前添加以下命令:

$em->getRepository('Classroom')->findAll().

然后我执行了 $em->find('Account', $id ); 并且帐户对象内的教室对象正确返回。

推测

我认为从数据库加载和缓存实体的方式有问题,因为如果我在执行操作之前加载所有教室对象(或与帐户 I 关联的对象), find(),那么一切都很好。

由于我是 PHP 和 Doctrine 的初学者,我寻求进一步的意见/帮助来解决这个问题。

The setup:

I have the following code two entity models:

Account, representing the user account and has a foreign key to classroom.

<?php
namespace models;

/**
 * @Entity
 */
class Account
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @OneToOne(targetEntity="Classroom")
     */
    public $classroom;
}

Classroom that represents the classroom a Student is enroled in.

<?php
namespace models;

/**
 * @Entity
 */
class Classroom
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Column(type="string")
     */
    public $name;
}

The problem:

When I do $account = $em->find('Account',$id); I correctly get the account back from the database, but the $account->classroom; is an empty (not null) object. After experimenting a bit I tried adding the following command before the execution:

$em->getRepository('Classroom')->findAll().

Then I executed the $em->find('Account', $id ); and the classroom object inside the account object returned correctly.

Speculation:

I assume that there is something wrong with the way entities are loaded and cached from the database, because if I load all the classroom objects (or the one associated with the account I) before I do my find(), then everything is fine.

As I'm beginner of PHP and Doctrine, I seek further opinions/help to solve that issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-12-05 01:06:26

来自 Doctrine 架构文档

任何实体类的所有持久属性/字段都应始终是私有受保护,否则延迟加载可能无法按预期工作

您的 $classroom 属性是 public,这可能就是与 Classroom 实体的关联未被延迟加载的原因。您应该将其更改为 privateprotected (我建议将所有实体属性设置为 protected,对于 各种原因)。

如果这不起作用,您可以尝试将 OneToOne 关联的 fetch 属性设置为 EAGER,即

/**
 * @OneToOne(targetEntity="Classroom", fetch="EAGER")
 */
protected $classroom;

From the Doctrine Architecture Documentation:

All persistent properties/field of any entity class should always be private or protected, otherwise lazy-loading might not work as expected.

Your $classroom property is public, which is probably why the association with the Classroom entity is not being lazy-loaded. You should change it to be private or protected (I recommend making all your Entity properties protected, for various reasons).

If that doesn't work, you could try setting the fetch attribute of your OneToOne association to EAGER, i.e.

/**
 * @OneToOne(targetEntity="Classroom", fetch="EAGER")
 */
protected $classroom;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文