更改后不会在数据库中更新Symfony实体 [英] Symfony entities are not updated in database after changes

查看:99
本文介绍了更改后不会在数据库中更新Symfony实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的symfony应用程序正在使用Doctrine在mysql中保留实体。

My symfony app is using Doctrine to persist entities in mysql.

今天,我更新了实体广告客户和报表,因此两者之间存在关联-如这篇文章中所建议的:使用EntityType显示选择项时,无法从Symfony表单中保存实体

Today I updated my entities "Advertiser" and "Report" so there are relations between the two - as suggested in this post: When using EntityType to show a select, can't save entity from Symfony form

当我尝试创建迁移时,它说数据库已经在

When I try creating a migration, it says that the database is already in sync.

php bin/console make:migration

返回:
[警告]未检测到数据库更改。
数据库架构和应用程序映射信息已经同步。

Returns: [WARNING] No database changes were detected. The database schema and the application mapping information are already in sync.

但是,如果我查看报表表,仍然看到旧的模式:

However if I look at the table for the report, I see it still has the old schema:

+---------------+------------+------+-----+---------+----------------+
| Field         | Type       | Null | Key | Default | Extra          |
+---------------+------------+------+-----+---------+----------------+
| id            | int(11)    | NO   | PRI | NULL    | auto_increment |
| advertiser_id | int(11)    | NO   | MUL | NULL    |                |
| start_date    | date       | NO   |     | NULL    |                |
| end_date      | date       | NO   |     | NULL    |                |
| deleted       | tinyint(1) | NO   |     | NULL    |                |
+---------------+------------+------+-----+---------+----------------+

即使我的实体现在看起来像这样:

Even though my entity looks like this now:

class Report
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="date")
     */
    private $start_date;

    /**
     * @ORM\Column(type="date")
     */
    private $end_date;

    /**
     * @ORM\Column(type="boolean")
     */
    private $deleted;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Advertiser", inversedBy="reports")
     * @ORM\JoinColumn(nullable=false)
     */
    private $advertiser;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getStartDate(): ?\DateTimeInterface
    {
        return $this->start_date;
    }

    public function setStartDate(\DateTimeInterface $start_date): self
    {
        $this->start_date = $start_date;

        return $this;
    }

    public function getEndDate(): ?\DateTimeInterface
    {
        return $this->end_date;
    }

    public function setEndDate(\DateTimeInterface $end_date): self
    {
        $this->end_date = $end_date;

        return $this;
    }


    public function getDeleted(): ?bool
    {
        return $this->deleted;
    }

    public function setDeleted(bool $deleted): self
    {
        $this->deleted = $deleted;

        return $this;
    }

    public function getAdvertiser(): ?Advertiser
    {
        return $this->advertiser;
    }

    public function setAdvertiser(?Advertiser $advertiser): self
    {
        $this->advertiser = $advertiser;

        return $this;
    }
}

我一直在寻找解决方案并尝试过,但没有运气:

I've been searching for solutions and have tried these, but no luck:

php bin/console doctrine:cache:clear-metadata
php bin/console doctrine:schema:update --force

请告诉我您对我如何更新我的建议

Please let me know if you have any suggestion on how I can update my database with my updated schema.

推荐答案

我相信问题是我以前拥有一个名为 advertiser_id的属性(int)在报表对象上。可能我正在尝试一次更改太多东西以使该学说无法处理。以前,我曾尝试在添加广告商的关联属性时删除adser_id。

I believe that the problem was that I previously had a property called "advertiser_id" (int) on the report object. And possibly I was trying to change too many things at once for doctrine to manage. Previously, I had tried to remove the advertiser_id while adding the relation property for advertiser.

要使该理论再次起作用,我从Report对象中删除了广告商属性-以及吸气剂和吸气剂。我还从Advertiser对象中删除了反向查找的内容。当我尝试运行迁移时,似乎正在尝试运行多个迁移-都在做相同的事情:删除不存在的外键。因此,我注释掉了迁移文件中的所有这些命令,最后终于能够将其迁移。我还删除了advertiser_id属性。该应用程序再次运行。

To get doctrine working again, I removed the advertiser property from the Report object - along with the getters and setters. I also removed the reverse lookup stuff from the Advertiser object. When I tried to run the migration, it seems like there were several migrations that it was trying to run - all doing the same thing: dropping a foreign key that doesn't exist. So I commented out all of those commands in the migration files and finally was able to get it to migrate. I also removed the advertiser_id property. The app is working again.

然后,我尝试将广告商关系属性添加回报表中。这次,它按预期工作,并且能够迁移。因此,我认为问题与我的对象已经具有adser_id属性有关。现在,我已经将广告客户关系添加到了报表对象,我看到该原则在表中添加了一个Advertiser_id列。我怀疑它以前是出现故障的原因。

Then I tried adding the "advertiser" relation property back to the report. This time it worked as expected and I was able to migrate. So I think the issue is related to my object already having an advertiser_id property. Now that I've added the advertiser relation to the Report object, I see that doctrine added an advertiser_id column to the table. I suspect that it being present previously was the reason things broke down.

感谢您的回复!很高兴再次使用它。

Thanks for the replies! Glad to have it working again.

这篇关于更改后不会在数据库中更新Symfony实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆