在Symfony/Doctrine ORM中生成实体的获取器和设置器

30

我有以下只包含属性的Symfony实体ORM:

<?php

namespace Evr\HomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * 
     * @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles")
     * @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id")
     */
    private $subcategory;


    /**
     * 
     * @ORM\Column(type="string",length=512)
     */
    private $title;

    /**
     * 
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * 
     * @ORM\Column(type="text")
     */
    private $exclusive_content;

    /**
     * 
     * @ORM\Column(type="date")
     */
    private $creation_date;


     /**
     * 
     * @ORM\Column(type="integer")
     */
    private $views;

    /**
     * 
     * @ORM\Column(type="integer")
     */
    private $votes;


}

我希望能自动生成访问器和修改器,所以我运行了以下命令:

app/console doctrine:generate:entities Evr/HomeBundle/Entity/Article
每次我这样做,它都会显示以下错误信息:
  [Doctrine\ORM\Mapping\MappingException]
  Class "Evr\HomeBundle\Entity\Article" is not a valid entity or mapped super
   class.



doctrine:generate:entities [--path="..."] [--no-backup] name
我不知道为什么它没有生成实体,实体/注释中有什么问题吗?
7个回答

75

尝试:

php app/console doctrine:generate:entities EvrHomeBundle:Article
如果您使用的是Symfony 3.0或更高版本,则将app替换为bin:
php bin/console doctrine:generate:entities EvrHomeBundle:Article
如果您正在使用Symfony 4+,则:
php bin/console make:entity --regenerate 

我尝试过了,无论使用什么命令,问题仍然存在。 - SmootQ
嗯,我复制/粘贴了您的实体代码,并更改了命名空间以适应我的一个项目,它正常工作。 - zizoujab
这真的很奇怪!我有许多其他与此实体有关系的类。例如,Subcategory又与Category类有关系。 - SmootQ
5
清除缓存后重试。另外,要检测到映射错误,请执行 php app/console doctrine:mapping:info 命令。 - zizoujab
我清除了缓存并重试,但问题仍然存在...当我运行命令doctrine:mapping:info时,它显示1条消息(找到1个映射实体),映射的实体是User实体...(实际上我在bundle中有超过5个实体,即使它只显示了1个映射实体)。 - SmootQ
对于Symfony 4+,使用以下命令:bin/console make:entity --regenerate --overwrite App\Entity --overwrite:用于覆盖现有方法(如果不需要则忽略)。 - ybenhssaien

11
php bin/console doctrine:generate:entities AppBundle

这将自动生成所有必要的getter和setter方法到您的实体文件中。

如果您想对表格进行具体指定,请使用以下代码:

php bin/console doctrine:generate:entities AppBundle:"TABLE_NAME"

"TABLE_NAME"替换为您表格的名称。


非常感谢您抽出时间添加另一个答案。这个答案也是另一个可行的解决方案,+1..我3年前问过这个问题 :) 最好的! - SmootQ
1
你的解决方案在Symfony 3.4上对我有效,你+1。你能告诉我为什么Symfony没有在任何地方记录它吗? - Dung
@Dung 我不知道,Dung。他们应该诚实地说。 - jcoder

10
尝试使用下一个命令删除此实体并重新生成它们:
php app/console doctrine:generate:entity --entity="EvrHomeBundle:Article" --fields="name:string(255) content:text exclusive_content:text creation_date:date views:integer votes:integer"

然后手动添加:

/**
 * 
 * @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles")
 * @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id")
 */
private $subcategory;

我删除了 subcategory 注解。 - Victor Bocharsky
好的,我更新了答案,请尝试生成此实体,可以吗? - Victor Bocharsky
不,它不会生成getter-setter... 你说你移除了子类别注释?但在答案里还是有它。 - SmootQ
抱歉,我没有注意到,您实际上从所有注释中删除了一个“@”...这将使它们失效。 - SmootQ
尝试更改 ORM\Column(type="string",length=255)。255 是最大值。 - Victor Bocharsky
显示剩余11条评论

5

映射导入(从数据库)

php bin/console doctrine:mapping:import 'AppBundle\Entity' yml --path=src/AppBundle/Resources/config/doctrine

从映射中生成实体,但不包含 getters 和 setters。

php bin/console doctrine:mapping:convert annotation ./src 

从映射中生成具有getter和setter的实体

php bin/console doctrine:generate:entities AppBundle/Entity

谢谢,这个问题很老了,但是你的回答会非常有用 +1 - SmootQ

3

同时要注意ORM,要计算生成getter/setter:

/**
 * @var date
 *
 * @ORM\Column(name="creation_date", type="date")

 */

非常感谢您抽出时间添加另一个答案,它也有效 +1 :) 我3年前提出了这个问题。干杯! - SmootQ

2

虽然缺少*是解决方案之一

但在我的情况下,当我从命令提示符创建实体时,我更喜欢使用YML配置格式,而不是注释。

现在我正在使用注释给出映射命令,所以它无法工作。

请尝试将Resources/config/Category.orm.yml配置为:

AppBundle\Entity\Category:
    type: entity
    table: null
    repositoryClass: AppBundle\Repository\CategoryRepository
    oneToMany:
        products:
            targetEntity: Product
            mappedBy: Category

将 Resources/config/Product.orm.yml 更改为:

AppBundle\Entity\Product:
    type: entity
    table: null
    repositoryClass: AppBundle\Repository\ProductRepository
    manyToOne:
        category:
            targetEntity: Category
            inversedBy: products
            joinColumn:
                name: category_id
                referenceColumnName: id

我认为这不是一个错误,而是更好的理解!


非常感谢您抽出时间提供这个可行的解决方案。我3年前提出了这个问题。祝您一切顺利!+1 - SmootQ

1

感谢您添加这个可行的解决方案,我3年前提出了这个问题,再次感谢+1。 - SmootQ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接