dou6495 2018-01-30 07:41
浏览 67
已采纳

获取Doctrine以对注释更改进行操作

I have been using annotations in a Symfony application to define ORM mappings with Doctrine.

I want to change an existing, string-holding property called "author" so that it now instead acts as a reference to a User entity. 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."

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."

===

Update: After dropping the database and re-running an installation, I now get the following error message when running doctrine:migrations:diff:

[Doctrine\ORM\ORMException] Column name id referenced for relation from AppBundle\Entity\Post towards FOS\UserBundle\Model\User does not exist.

This comes even as DESC users returns the following:

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?

===

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. 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.

  • 写回答

1条回答 默认 最新

  • doubi1931 2018-01-30 08:34
    关注

    You forgot to create your own entity as describe in the FOS user doc :

    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"})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化