【问题标题】:Doctrine 2 @ManyToOne attribute always return nullDoctrine 2 @ManyToOne 属性总是返回 null
【发布时间】:2015-09-18 06:55:54
【问题描述】:

我有这些实体

/** @ORM\Entity */
class Produto {

    /* Other attributes */

    /** 
     * @ORM\ManyToOne(targetEntity="Produto\Entity\Categoria")
     * @ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
     */
    private $categoria;

    public function getCategoria() {
        return $this->categoria;
    }

    public function setCategoria(Categoria $categoria) {
        $this->categoria = $categoria;
    }
}

/** @ORM\Entity */
class Categoria {

/* Other attributes */

    /** @ORM\Column(type="string") */
    private $nome;

}

但是当我调用findAll方法时,$categoria属性类别总是null,我做错了什么?

我尝试在实体类别上添加 OneToMany 注释,但效果不佳,有什么地方可以解释为什么会发生这种情况?我在文档中什么也没找到

【问题讨论】:

  • id列是否包含在其他属性中?

标签: php doctrine-orm zend-framework2 many-to-one


【解决方案1】:

类类别需要有一个 $id 类变量。此外,Producto 实体的数据库中的数据需要将 categoria_id 字段映射到 Categoria id 才能正常工作。

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多