【问题标题】:Doctrine2 @ORM in entity mapping annotation实体映射注释中的 Doctrine2 @ORM
【发布时间】:2015-03-15 15:56:19
【问题描述】:

我是 Doctrine2 的新手,并且使用以下命令从我现有的数据库中创建了实体类:

.\vendor\bin\doctrine-module orm:convert-mapping --force --from-database annotation ./EXPORT/

现在我有了我的课程,它们看起来像这样:

<?php

namespace App\Model\Entity; // this line was not generated automatically

use Doctrine\ORM\Mapping as ORM;

/**
 *
 * Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity
 */
 class Category
 {

     /**
     * @ORM\var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
     private $id;
 ....

看起来不错,但我不明白为什么在文档块中的每个符号之前都使用“@ORM\”。 当我创建查询时,在我删除这个“@ORM\”之前,它们不会工作,直到每个符号都被删除。 我错过了什么? 我没有使用任何框架(Zend/Symfony...)。我只是在作曲家使用 "doctrine/orm": "~2.4"。

最好的问候 迈克尔

【问题讨论】:

    标签: php orm doctrine-orm


    【解决方案1】:

    ORM 指的是顶部的命名空间快捷方式

    use Doctrine\ORM\Mapping as ORM;
    

    例如你也可以像这样使用带有完整命名空间的 Id 注释:

    /**
    * @Doctrine\ORM\Mapping\Id 
    */
    

    除此之外,我建议查看注释文档以使学说正确运行

    (http://doctrine-common.readthedocs.org/en/latest/reference/annotations.htmlhttp://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html)

    【讨论】:

    • 谢谢,但我仍然不明白为什么我需要 Doctrine\ORM\Mapping 命名空间。我的查询: $qb ->select('c.name, c.description') ->from('App\Model\Entity\Category', 'c');当我从注释中删除 ORM 命名空间并在此命名空间存在时停止工作时返回它应该的所有内容。你能解释一下为什么会这样吗?在那之前,我会再次查看您发送的文档,也许我错过了什么......
    • 我可能应该更仔细地阅读错误消息。我在这里找到了解决方案:stackoverflow.com/questions/15099060/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多