【问题标题】:In Doctrine schema cannot be updated while I've added new field在我添加新字段时无法更新 Doctrine 架构
【发布时间】:2014-04-30 07:59:34
【问题描述】:

我有两张表,一张是产品,另一张是类别。我首先创建了产品并通过终端生成了数据库模式。当我将以下字段添加到产品实体时:

     /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;

然后我将产品字段添加到类别实体中,如下所示:

/**
 * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
 */
protected $products;

public function __construct()
{
    $this->products = new ArrayCollection();
}

当我在终端输入以下命令时:

php console doctrine:schema:update --force

上面写着Nothing to update - your database is already in sync with the current entity metadata.

对于最后一步,我什至删除了产品和类别表并通过以下命令生成它们:

php console doctrine:schema:update --force

两个表都已创建,但无法添加 category_id。

我遵循了 symfony 的官方书籍。这里有什么我错过的吗?

我在通过 shell 创建实体后手动添加了 category_id 及其元数据

编辑::

产品实体:

<?php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 */
class Product
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $price;

    /**
     * @var string
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;

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

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

        return $this;
    }

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

    /**
     * Set price
     *
     * @param string $price
     * @return Product
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     * @return Product
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

类别实体:

<?php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Category
 */
class Category
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;


    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

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

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

        return $this;
    }

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

【问题讨论】:

  • 你试过删除缓存吗?
  • 你是否用@ORM\Entity标记了两个实体类?
  • 我在两个课程中都使用了namespace Acme\StoreBundle\Entity;
  • @phpGeek 请用实体类的完整源代码更新问题。
  • @JakubPolák,我也试过了,但没有成功

标签: mysql symfony doctrine-orm metadata


【解决方案1】:

您的 yml 文件中缺少正确的映射,您的 yml 文件应包含如下内容:

Product:
  type: entity
  manyToOne:
    category:
      targetEntity: Category
      inversedBy: products
      joinColumn:
        name: category_id
        referencedColumnName: id

related documentation 中阅读有关 yaml 映射的更多信息。

【讨论】:

  • 这些类是由 symfony 自己创建的。顺便说一句,我尝试了您的元数据,但没有任何反应!我收到了同样的信息。
  • @phpGeek 你是在使用注解、yml 还是 xml 来配置实体?
  • @phpGeek 您可以在通过控制台创建实体时选择它。
  • 我选择了 yml。怎么会?这有关系吗?
  • @phpGeek 是的,如果你选择 yml 那么你不能通过像 @ORM\ManyToOne(targetEntity="Category", inversedBy="products") 这样的注解来配置类,但是你需要在 yml 文件中做。就个人而言,我更喜欢注释而不是 xml 或 yml。
猜你喜欢
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
相关资源
最近更新 更多