【问题标题】:Table in database not consistent with the entity schema数据库中的表与实体模式不一致
【发布时间】:2016-05-04 14:38:57
【问题描述】:

我在基于 symfony2.8 的网站中使用 Doctrine2 与数据库进行交互。我使用命令行工具(doctrine:schema:create)成功创建了表。

但是在运行命令doctrine:schema:update --dump-sqldoctrine:schema:update --force 之后,我发现在我的MySQL 数据库中只创建了“id”和“name”列。

我检查了我的实体上的映射数据(注释)并再次运行后两个命令,但该表仍然与命令行工具相同,返回消息“无需更新 - 您的数据库已经在与当前实体元数据同步。”

有什么建议或意见吗?

请在下面找到我的实体的样子:

<?php
namespace MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Contact
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity(repositoryClass="MainBundle\Repository\ContactRepository")
 */
class Contact
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

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

    /**
     * @var string
     * @ORM\Column(length=100)
     * @Assert\Length(max="100")
     * @Assert\NotBlank(message="Please enter your email address")
     * @Assert\Email(checkMX=true, message="Please check the email you've provided")
     * @ORM\Column(name="email", type="string")
     */
    private $email;

    /**
     * @Assert\Regex(pattern="/^\+?[\d]+$/", message="Please check the phone info you've provided")
     * @Assert\Length(max="40")
     * @Assert\NotBlank(message="Please enter a phone number")
     * @ORM\Column(name="phone", length=40)
     */
    private $phone;

    /**
     * @Assert\NotBlank(message="Please enter a message")
     *
     * @ORM\Column(name="message", type="text")
     */
    private $message;

    public function __construct()
    {
        $this->date = new \DateTime();
    }


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

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return Contact
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Contact
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Contact
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return Contact
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

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

    /**
     * Set message
     *
     * @param string $message
     *
     * @return Contact
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

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

【问题讨论】:

  • 您是否在使用任何一种学说元数据缓存?还可以尝试在电子邮件字段中合并两个单独的 Column 注释。
  • 完全没有,我没有对 Symphony 框架中嵌入的 Doctrine lib 进行任何更改。我会尝试合并它,但为什么没有在数据库中创建“电话”和“消息”列?
  • 尝试通过删除 cache/* 文件夹手动清除缓存。
  • 我刚刚又试了一次,但仍然收到相同的消息:“无需更新 - 您的数据库已与当前实体元数据同步。”
  • 确保检查正确的数据库。

标签: php mysql symfony doctrine-orm


【解决方案1】:

最后,我能够通过手动创建表来解决此问题,然后在从现有数据库生成实体后,如下所述:http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 1970-01-01
    • 2021-07-24
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多