这可能是
一个新手问题,在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;
}
}
所以它也生成了注释,但我仍然无法生成我的数据库架构:(
下面的原始问题已过时我必须手动输入注释的原因是什么?
好的,我想你错过了理解如何使用 Doctriine 创建数据库。
实体类(在src/AppBundle/Entity/
中)很好地定义了映射表的实体。
此类具有表示列(或关联)的属性(也称为变量)。必须注释此属性,以便教义知道要创建哪种列。
现在,这些类可以完全手动构造,也可以使用以下命令生成:
php app/console generate:doctrine:entity
但你必须明白这只是一个捷径 - 你仍然负责维护和开发架构。
所以回答你的问题:
因为教义我必须手动输入注释的原因是什么?
无法知道你想要保存什么样的数据——注释是你向教义(和symfony)解释你需要什么的一种方式。
不要忘记运行doctrine:schema:create
以实际创建实体中描述的表。
Oke,我想出了问题所在。
使用交互式手册,我忽略了"选项"注释。这是我犯的第一个错误。
我犯的第二个错误是我在 AppBundle 的构建中创建实体。当我创建自己的捆绑包时,一切都完美无缺:)