【问题标题】:No annotations created for created entities没有为创建的实体创建注释
【发布时间】:2016-03-19 16:31:15
【问题描述】:

这可能是一个新手问题,在 Symfony 的文档中我找不到它。我已经根据文档安装了 Symfony,并使用“数据库和学说”手册创建了一个数据库。之后,我使用该学说创建了一个实体。当我这样做时,我注意到没有创建注释。作为捆绑包,我使用了 AppBundle,它也在我的 AppKernel 中,如下所示:

$bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        new AppBundle\AppBundle(),
    ];

更新: 使用命令 php bin/console doctrine:generate:entity 我使用注释(而不是 yml、php 或 xml)创建了实体 Foo,结果如下:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Foo
 *
 * @ORM\Table(name="foo")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\FooRepository")
 */
class Foo
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="bar", type="string", length=35)
     */
    private $bar;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set bar
     *
     * @param string $bar
     *
     * @return Foo
     */
    public function setBar($bar)
    {
        $this->bar = $bar;

        return $this;
    }

    /**
     * Get bar
     *
     * @return string
     */
    public function getBar()
    {
        return $this->bar;
    }
}

所以它也生成了注释,但我仍然无法生成我的数据库模式:(

下面的原始问题已过时 我必须手动输入注释的原因是什么?

【问题讨论】:

    标签: php symfony doctrine-orm annotations


    【解决方案1】:

    好的,我认为您对如何使用 Doctriine 创建数据库有所了解。

    实体类(在src/AppBundle/Entity/ 中)定义了映射的实体。

    此类具有表示列(或关联)的属性(又名变量)。必须对这些属性进行注释,以便学说知道要创建什么样的列。

    现在这些类可以完全手动构建使用以下命令生成:

    php app/console generate:doctrine:entity
    

    但是你必须明白这只是一个捷径——你仍然有责任维护和开发架构。

    所以回答你的问题:

    我必须手动输入注释的原因是什么?

    因为教义不知道你想保存什么样的数据——注释是教义(和symfony)解释你需要什么的一种方式。

    不要忘记运行 doctrine:schema:create 来实际创建实体中描述的表。

    【讨论】:

    • 好吧,我很清楚实体是对象,它们引用我数据库中的表。是的,这些属性确实是它们各自表中的列。到目前为止,我同意你的看法。我认为如果学说也生成类(实体),为什么它不生成它的注释也是如此。感觉自己变懒了哈哈
    • Mhh 出于某种原因它也不会创建我的数据库模式。当我执行该命令时,它只返回“没有要处理的元数据类”......我真的不知道我的设置有什么问题。我已经按照手册symfony.com/doc/current/book/doctrine.html
    • 当使用命令生成实体时...您是使用“使用注释”还是选择了其他选项之一(如 yml)。另外,您是否添加了任何字段?您可以将整个 Entity 类添加到您的任务中吗?
    • 我已经按照 foo 实体的要求更新了问题
    • 好的,你运行doctrine:schema:createdoctrine:schema:update --force了吗?
    【解决方案2】:

    好的,我发现了问题。

    使用交互式手册,我忽略了“选项”注释。这是我犯的第一个错误。

    我犯的第二个错误是我在 AppBundle 的构建中创建实体。当我创建自己的 Bundle 时,一切都完美无缺:)

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 2011-12-05
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2015-11-14
      • 2019-03-09
      相关资源
      最近更新 更多