在我添加新字段时,无法更新Doctrine架构

时间:2014-04-30 07:59:34

标签: mysql symfony doctrine-orm metadata

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

     /**
     * @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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

详细了解related documentation中的yaml映射。