在Symfony / Doctrine ORM中生成实体Getter和Setter

时间:2014-01-23 18:57:12

标签: symfony annotations doctrine getter-setter

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

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


}

我想自动生成setter和getter,所以我运行以下命令:

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 个答案:

答案 0 :(得分:40)

尝试:

app/console doctrine:generate:entities EvrHomeBundle:Article

如果您使用的是symfony 3.0或更高版本,请使用bin:

替换app
bin/console doctrine:generate:entities EvrHomeBundle:Article

如果您使用的是symfony 4+,那么:

bin/console make:entity --regenerate 

答案 1 :(得分:11)

尝试删除此实体并使用下一个命令重新生成它们:

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;

答案 2 :(得分:8)

php bin/console doctrine:generate:entities AppBundle

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

如果您想具体了解表格,请使用:

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

用您的表格替换&#34; TABLE_NAME&#34;

答案 3 :(得分:3)

对ORM也要好心,要计算生成getter / setter:

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

 */

答案 4 :(得分:3)

映射导入(从数据库)

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

通过映射生成实体,但没有getter和setters

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

OR

通过使用getter和setter进行映射来生成实体

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

答案 5 :(得分: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

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

答案 6 :(得分:1)

用法:

  

orm:generate-entities dest-path

控制台中的示例:

doctrine orm:generate-entities --generate-annotations="true" destination_path

来源:http://wildlyinaccurate.com/useful-doctrine-2-console-commands/

相关问题