php - 数据库中的表与实体模式不一致

标签 php mysql symfony doctrine-orm

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

但是在运行命令 doctrine:schema:update --dump-sql 然后 doctrine: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;
    }
}

最佳答案

最后,我能够通过手动创建表来解决这个问题,然后按照此处的说明从现有数据库生成实体:http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

关于php - 数据库中的表与实体模式不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030978/

相关文章:

javascript - 如何将文件上传更改为仅图像上传,Laravel

mysql - symfony2主义2 : in which cases do we need to lock a database table/row?

php - 如何使用 Doctrine 的 ArrayCollection::exists 方法

symfony - Twig 批处理和循环索引

php - 提交表单时, Bootstrap 模式打开,但单击确定按钮时,它不起作用

php - 获取不带扩展名的文件名

php - 加载评论时加入查询

mysql - 连接两个表。使用 SUM 的一列

MySQL:在 SELECT 查询期间为每个唯一出现标记 1

mysql - SQL 查询时间复杂度 - 连接与子查询