1

我在教义 2 中遇到了一些烦人的问题。

我有一个文档实体和一个会议实体。

/**
 *
 * @author klauss
 *
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Document
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $path;

    /**
     * @ORM\OneToOne(targetEntity = "Conference", mappedBy = "image")
     */
    protected $conference;

    /**
     * @Assert\File(
     *      maxSize="4M",
     *      maxSizeMessage="Allowed maximum size is {{ limit }}"
     * )
     */
    public $file;

    // .....

和会议实体

// .........

    /**
     * Uploaded image.
     *
     * @ORM\OneToOne(targetEntity = "Document", inversedBy = "conference")
     * @ORM\JoinColumn(name = "image", nullable = true, referencedColumnName = "id")
     */
    protected $image;

// ............

因此,在 Twig 模板中,我想执行以下操作:

{{ conference.image.path }}

但它只是不加载图像,我总是需要调用

$conference->getImage()->getPath();

在 PHP 中获取 Twig 中的正确路径。但不应该是一样的吗?如果我不在 PHP 中调用它,Twig 调用只会返回一个空字符串

我怎样才能实现 Doctrine 自动知道 Document 关系?

4

1 回答 1

2

将您的实体类变量设为私有或受保护。Doctrine 2 依赖于它的延迟加载魔法。

http://docs.doctrine-project.org/en/latest/reference/architecture.html

于 2012-04-12T13:08:55.080 回答