Added columns to Doctrine Entity (Symfony 2.6 standard) are not recognized by doctrine

时间秒杀一切 提交于 2019-12-12 01:27:05

问题


I have an entity in my symfony2 app with more than a few attributes. It implements JSONserializable, since all the work on the entity is done on the javascript side, and I have a magic setter function defined so I can loop through the JSON I'm getting from the client and set all of my attributes at once.

The Class definition:

   /**
   *@ORM\Entity
   *@ORM\Table(name="creature")
   */
    class Creature implements JsonSerializable {

And the atypical function definitions:

public function __set($name, $value) {
    $this->$name = $value;

    return $this;
}
public function jsonSerialize() {
  $json = array();
  foreach($this as $key => $value) {
    if($key != "attacks") {
      $json[$key] = $value;
    } else {
      $json[$key] = array();
      for($x = 0; $x < count($this->attacks); $x++) {
        $json[$key][$x] = array();
        $json[$key][$x]["attack"] = $this->attacks[$x]->getName();
        $json[$key][$x]["bonus"] = $this->attacks[$x]->getBonus();
        $json[$key][$x]["damage"] = $this->attacks[$x]->getDamage();
      }
    }
  }
  return $json;
}

For the most part, this entity is working great. Except as I was going along I discovered I needed to add 3 more columns. So, naturally, I added this to my entity class:

  /**
   *ORM\Column(type="integer", nullable=true)
   */
  protected $experience;

  /**
   *ORM\Column(type="integer", nullable=true)
   */
  protected $cr;

  /**
   *ORM\Column(type="integer", nullable=true)
   */
  protected $proficiencybonus;

And attempted to run

php app/console generate:doctrine:entities AppBundle
php app/console doctrine:schema:update --force

Except neither command recognized that I had made any change. I tried clearing my cache(dev and prod) and deleting my custom code from the entity, but it still won't add my three new columns. My next thought is to reset my database completely, but I'm not keen to do that if I can help it.

Anyone have any ideas?


回答1:


Looks like you forgot the @ in your annotations :

/**
 *@ORM\Column(type="integer", nullable=true)
 */
protected $experience;

/**
 *@ORM\Column(type="integer", nullable=true)
 */
protected $cr;

/**
 *@ORM\Column(type="integer", nullable=true)
 */
protected $proficiencybonus;



回答2:


I had a similar problem, I forgot to add the: *@ORM\Entity annotation.

This also results in not adding / updating the entity.



来源:https://stackoverflow.com/questions/29614374/added-columns-to-doctrine-entity-symfony-2-6-standard-are-not-recognized-by-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!