30

I have the following ORM Symfony entity with only properties :

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


}

I want to generate setters and getters automatically, so I run the following command :

app/console doctrine:generate:entities Evr/HomeBundle/Entity/Article

And everytime I do this, it displays the following error message :

  [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

I don't know why it doesn't generate entities, is something wrong in the entity/annotations?

SmootQ
  • 2,096
  • 7
  • 33
  • 58

7 Answers7

75

try :

php app/console doctrine:generate:entities EvrHomeBundle:Article

If you are using symfony 3.0 or higher then substitue app with bin:

php bin/console doctrine:generate:entities EvrHomeBundle:Article

If you are using symfony 4+ then :

php bin/console make:entity --regenerate 
Johnny
  • 55
  • 1
  • 13
zizoujab
  • 7,603
  • 8
  • 41
  • 72
  • I tried it, it doesn't matter which command to use, the problem persists. – SmootQ Jan 23 '14 at 19:07
  • hmmm I copied/pasted your entity code and changed the namespace to fit one of my projects and it worked just fine. – zizoujab Jan 23 '14 at 19:12
  • That's really strange !. I have many other classes that have relations with this entity. Like Subcategory, which in turn has a relation with the class Category – SmootQ Jan 23 '14 at 19:18
  • 5
    clear you cache and try again. also to detect mapping error execute `php app/console doctrine:mapping:info` – zizoujab Jan 23 '14 at 19:44
  • I cleared cache, and retry : but the problem persists...when I run the command doctrine:mapping:info , it shows 1 message (Found 1 mapped Entities) the mapped entity is the entity User... (In fact I have more than 5 entities in the bundle, even if it showed just 1 mapped entity) – SmootQ Jan 23 '14 at 20:22
  • for Symfony 4+ `bin/console make:entity --regenerate --overwrite App\Entity` `--overwrite` : to overwrite existing methods (to ignore if not needed) – ybenhssaien Nov 18 '20 at 08:25
11
php bin/console doctrine:generate:entities AppBundle

This will generate all the necessary getters and setters automatically into your entity files.

If you want to be specific about the tables, then use this:

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

Substitute "TABLE_NAME" with your table's name.

jcoder
  • 227
  • 3
  • 14
  • Thank you so much for taking the time to add another answer. This answer is also another working solution, +1 .. I asked this question 3 years ago :) Best ! – SmootQ Aug 21 '17 at 11:40
  • 1
    Your solution works for me on Symfony 3.4, you +1, can you tell me why Symfony does not document it any where? – Dung Jul 22 '19 at 19:46
  • @Dung I have no idea Dung. They should have to be honest. – jcoder Aug 28 '19 at 17:45
10

Try to delete this entity and regenerate them with next command:

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"

And then add manually:

/**
 * 
 * @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles")
 * @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id")
 */
private $subcategory;
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
5

Mapping import ( from database )

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

Generate Entityes from mapping but without getters and setters

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

OR

Generate Entityes from mapping with getters and setters

php bin/console doctrine:generate:entities AppBundle/Entity
Johnny
  • 55
  • 1
  • 13
Oleksandr P.
  • 370
  • 3
  • 7
3

Be carreful also to the ORM, to be count to generate getters/setters:

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

 */
aurny2420289
  • 481
  • 5
  • 16
  • Thank you so much for taking the time to add another answer, it works too +1 :) I asked this question 3 years ago. Cheers ! – SmootQ Aug 21 '17 at 11:41
2

Thought the missing * is one of the solution

But in my case while creating the entity from command prompt i preferred the Configuration Format to be YML, instead of Annotations.

Now what i am doing is giving mapping commands using annotations, so it is not working.

Try configuring Resources/config/Category.orm.yml as:

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

And Change the Resources/config/Product.orm.yml as:

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

And i feel it is not a bug but a better understanding!

  • Thanks so much for taking the time to this working solution. I asked this question 3 years ago. Wish you the Best ! +1 – SmootQ Aug 21 '17 at 11:42
1

Usage:

orm:generate-entities dest-path

Example in console:

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

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

Muhammed Tanriverdi
  • 3,230
  • 1
  • 23
  • 24
  • Thank you for adding this working solution, I asked this question 3 years ago, Thanks again +1 – SmootQ Aug 21 '17 at 11:43