Symfony2 应用程序/控制台不生成实体关系/关联的属性或模式更新

2024-03-15

我正在阅读并遵循代码中有关使用数据库和原则的 Symfony2 书中所写的内容(http://symfony.com/doc/2.0/book/doctrine.html http://symfony.com/doc/2.0/book/doctrine.html)。我已经到达“实体关系/关联”部分,但该框架似乎没有做它应该做的事情。我已将受保护的 $category 字段添加到 Product 实体,并将 $products 字段添加到 Category 实体。我的产品和类别实体如下:

Product:

<?php

namespace mydomain\mywebsiteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

类别:

<?php

namespace mydomain\mywebsiteBundle\Entity;

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

/**
 * Category
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /*
     * @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 description
     *
     * @param string $description
     * @return Category
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

根据文档,如果我现在执行

$ php app/console doctrine:generate:entities mydomain

框架应该为“产品”中的新类别字段和“类别”中的新产品字段生成 getter/setter。

HOWEVER当我运行该命令时,它应该会更新实体,但不会添加属性。我与备份(~)文件进行了比较,没有任何差异。如果我添加另一个字段(例如description2)并添加持久性的学说注释,那么它会生成属性。我一开始忽略了这一点,手动添加了映射字段的属性,然后执行:

$php app/console doctrine:schema:update --force

让它添加新的关联列。

HOWEVER它再次告诉我元数据和模式是最新的。

我已经删除了 app/cache/dev 文件夹并允许系统重新创建它,但没有任何区别。

谁能明白为什么框架的行为不符合文档中的描述?

Thanks


你在这里忘记了一颗星:

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

一定是

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Symfony2 应用程序/控制台不生成实体关系/关联的属性或模式更新 的相关文章

随机推荐