Symfony2应用程序/控制台不生成实体关系/关联的属性或模式更新 [英] Symfony2 app/console not generating properties or schema updates for Entity Relationships/Associations

查看:82
本文介绍了Symfony2应用程序/控制台不生成实体关系/关联的属性或模式更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读并遵循Symfony2关于使用数据库和教义的书籍( http://symfony.com/doc/2.0/book/doctrine.html )。我已经到达了实体关系/协会部分,但是框架似乎并没有在做什么。我已经将保护的$ category字段添加到Product实体,并将$ products字段添加到Category实体。我的产品和类别实体如下:

I am reading and following along in code what is written in the Symfony2 book on using Database and Doctrine (http://symfony.com/doc/2.0/book/doctrine.html). I have reached the "Entity Relationships/Associations" section but the framework does not seem to be doing what it is meant to be doing. I have added the protected $category field to the Product entity and added the $products field to the Category entity. My Product and Category entities are as below:

产品:

<?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;
    }
}

类别: p>

Category:

<?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;
    }
}

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



According to the documentation, if i now execute

$ php app/console doctrine:generate:entities mydomain

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

the framework should generate the getters/setters for the new category field in Product and for the new products field in Category.

HOWEVER 当我运行命令,它应该更新实体,但它不添加属性。我已经与备份(〜)文件进行了比较,没有任何区别。如果我添加另一个字段(例如description2),并向其添加用于持久性的原则注释,那么它将生成属性。我首先忽略了这一点,并手动添加了映射字段的属性,然后执行:

HOWEVER when i run the command it supposedly updates the entities but it does not add the properties. I have compared with the backup(~) files and there are no differences. If i add another field (e.g. description2) and add doctrine annotations for persistence to it then it generates the properties. I ignored this at first and manually added the properties for the mapping fields and then executed:

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

为添加新的关联列。

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

HOWEVER once again it told me that the metadata and schema were upto date.

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

I have deleted the app/cache/dev folder and allowed the system to recreate it but it has made no difference.

任何人都可以看到为什么框架不像文档中描述的那样行为?

Can anyone see why the framework is not behaving as described in the documentation??

谢谢

推荐答案

你在这里忘记了一个明星:

You have forgotten one star here:

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

这篇关于Symfony2应用程序/控制台不生成实体关系/关联的属性或模式更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆