Generating Entity Getters and Setters in Symfony /

2019-02-04 05:58发布

问题:

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?

回答1:

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;


回答2:

try :

app/console doctrine:generate:entities EvrHomeBundle:Article

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

bin/console doctrine:generate:entities EvrHomeBundle:Article

If you are using symfony 4+ then :

bin/console make:entity --regenerate 


回答3:

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.



回答4:

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

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

 */


回答5:

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/



回答6:

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!



回答7:

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