Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Entity Getters and Setters in Symfony / Doctrine ORM

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?

like image 817
SmootQ Avatar asked Jan 23 '14 18:01

SmootQ


People also ask

Does Symfony use Doctrine?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

What is ORM Symfony?

Database Model This mapping can be done with an Object Relational Mapping (ORM) tool. Symfony provides a separate bundle, DoctrineBundle, which integrates Symfony with third party PHP database ORM tool, Doctrine.

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.

What is schema Symfony?

In order to create the data object model that symfony will use, you need to translate whatever relational model your database has to an object data model. The ORM needs a description of the relational model to do the mapping, and this is called a schema.


2 Answers

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 
like image 154
zizoujab Avatar answered Oct 14 '22 04:10

zizoujab


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.

like image 11
jcoder Avatar answered Oct 14 '22 04:10

jcoder