dev_symfo
dev_symfo

Reputation: 61

How to add a field in entity in Symfony2

I try to add a cahmps in entity. I have this code:

/**
* @var integer $colorevent
* @ORM\Column(name="colorevent", type="integer")
*/
private $colorevent;

/**
* Get colorevent
* @return integer 
*/ 
public function getColorevent()
{
    return $this->colorevent;
}

/**
* Set colorevent
* @return integer 
*/
public function setColorevent($colorevent)
{
    return $this->colorevent = $colorevent ;
}

I run these commands:

php app/console doctrine:schema:update
php app/console doctrine:schema:update --dump-sql
php app/console doctrine:schema:update --force

which renders this message:

Nothing to update yopur database is already in sync with the current entity metadata

How do I add a field in entity?

Upvotes: 3

Views: 7212

Answers (3)

Patrick
Patrick

Reputation: 881

If you're using yaml or annotations as described in the other two answers you can use the doctrine command. However the command must contain the entity name:

php app/console doctrine:generate:entities AcmeMyBundle:Customer

Upvotes: 0

SimSimY
SimSimY

Reputation: 3686

How did you generated the entity class in the first time?

It the entity file was generated via doctrine:generate:entities then what you need to do is:

  1. update the Resources/config/doctrine/XXXXXXXXX where xxxx is the entity name.
  2. run app/console doctrine:generate:entity. This will regenerate the php entity file, and the private property, getter and setter.
  3. run php app/console doctrine:schema:update and update the DB

There may be some changes depending of the format you are using (I'm using yml) but the bottom line is that the doctrine:schema:update needs to find changes between the current and the cached (metadata) entity objects.

Upvotes: 2

kgilden
kgilden

Reputation: 10346

Each entity must have the Entity annotation

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class MyEntity
{
    // some code
}

I would also suggest using the console tools provided to create the entities.

app/console doctrine:generate:entity

For more information please read the official book (Doctrine, Propel).

Upvotes: 1

Related Questions