[英]Get Doctrine to act on an annotation change
I have been using annotations in a Symfony application to define ORM mappings with Doctrine. 我一直在Symfony应用程序中使用注释来使用Doctrine定义ORM映射。
I want to change an existing, string-holding property called "author" so that it now instead acts as a reference to a User entity. 我想更改一个名为“ author”的现有字符串保留属性,以使其现在代替对User实体的引用。 I change the annotations above my property's definition from this: 我从此更改属性定义上方的注释:
/**
* @var string
*
* @ORM\Column(name="author", type="string", length=255)
*/
... to this: ...对此:
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="FOS\UserBundle\Model\User", cascade={"all"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
Now the problem: When I run doctrine:migrations:diff
in order to generate a migration, I get a message saying "No changes detected in your mapping information." 现在的问题是:当我运行doctrine:migrations:diff
来生成迁移时,我收到一条消息,提示“在映射信息中未检测到更改”。
Similarly, when I run doctrine:schema:update
, I get a message saying that there is "Nothing to update - your database is already in sync with the current entity metadata." 同样,当我运行doctrine:schema:update
,我收到一条消息,说“没有什么要更新的-您的数据库已经与当前实体元数据同步”。
=== ===
Update: After dropping the database and re-running an installation, I now get the following error message when running doctrine:migrations:diff
: 更新:删除数据库并重新运行安装后,运行doctrine:migrations:diff
时,我现在得到以下错误消息:
[Doctrine\\ORM\\ORMException] Column name id
referenced for relation from AppBundle\\Entity\\Post towards FOS\\UserBundle\\Model\\User does not exist. [Doctrine \\ ORM \\ ORMException]从AppBundle \\ Entity \\ Post到FOS \\ UserBundle \\ Model \\ User的关系所引用的列名id
不存在。
This comes even as DESC users
returns the following: 即使DESC users
返回以下内容,也会出现这种情况:
mysql> DESC users;
+-----------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(180) | NO | | NULL | |
...
... so I'm a bit farther than where I originally was, but my basic question still applies: What information or action is still needed in order to get Doctrine to happily build a ManyToOne mapping on this column? ...所以我比原来的地方要远一些,但是我的基本问题仍然适用: 为了使Doctrine在本专栏上愉快地构建ManyToOne映射,仍需要什么信息或操作?
=== ===
Additional edit: Thanks to the answer from goto, I peeked around and saw that our application already had an entity extending the FOS UserBundle entity. 附加编辑:感谢goto的回答,我四处张望,看到我们的应用程序已经具有扩展FOS UserBundle实体的实体。 So I changed my annotations to be as follows: 因此,我将注释更改如下:
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", cascade={"all"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
... and everything worked. ...一切正常。
You forgot to create your own entity as describe in the FOS user doc : 您忘记了按照FOS用户文档中的描述创建自己的实体:
https://symfony.com/doc/current/bundles/FOSUserBundle/index.html#step-3-create-your-user-class https://symfony.com/doc/current/bundles/FOSUserBundle/index.html#step-3-create-your-user-class
Then you'll have to map it to your entity 然后,您必须将其映射到您的实体
@ORM\ManyToOne(targetEntity="YourBundle\Entity\User", cascade={"all"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.