Symfony2无法使用FPN TagBundle向Entity添加字段

时间:2015-01-23 10:27:19

标签: symfony tags doctrine entity

我正在实施FPN TagBundle。它需要将$ tags字段添加到我们要添加标签的实体。我添加了fiels但是doctrine不会更新数据库,好像它不会将它识别为表字段。如果我手动添加它,当我执行doctrine:schema:update --dump-sql时,字段$ tags将从DB表中删除。有什么建议为什么会这样?

这是我的实体发布:

namespace Blog\BlogBundle\Entity;

use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Usuarios\UsersBundle\Entity\User;
use DoctrineExtensions\Taggable\Taggable;


/**
 * Post
 *
 * @ORM\Table(name = "1_00_posts")
 *
 * @ORM\HasLifecycleCallbacks
 *
 * @ORM\Entity(repositoryClass="Blog\BlogBundle\Repository\PostRepository")
 */
class Post
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
public $id;

/**
 * @var User
 *
 * @ORM\ManyToOne(targetEntity="Usuarios\UsersBundle\Entity\User", inversedBy="blog")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
 *@Assert\NotBlank()
 */
public $author;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 * @Assert\NotBlank
 */
public $title;

/**
 * @var string
 *
 * @ORM\Column(name="body", type="text")
 * @Assert\NotBlank
 */
public $body;

/**
 *@var \Doctrine\Common\Collections\Collection
 */
public $tags; //this is the "un-updatable" field

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="Comment", mappedBy="post", cascade={"remove"})
 */
public $comments;

/**
 * @var string
 *
 * @Gedmo\Slug(fields={"title"}, unique=false)
 * @ORM\Column(length=255)
 */
public $slug;

/**
 * @var datetime $createdAt
 *
 * @Gedmo\Timestampable(on="create")
 * @ORM\Column(name="createdAt", type="datetime")
 */
public $createdAt;

/**
 * @var datetime $updatedAt
 *
 * @Gedmo\Timestampable(on="update")
 * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
 */
public $updatedAt;


/**
* Construct DateTime
*/
public function __construct()
{
    $this->createdAt = new \DateTime();
    $this->comments = new ArrayCollection();
}


/**
 * Tell doctrine that before we persist or update we call the updatedTimestamps() function.
 *
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function updatedTimestamps()
{
    if ($this->getCreatedAt() == null) {
        $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
    } else {
        $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));
    }
}


/**
 * @return mixed
 */
public function __toString()
{
    return $this->comments;
}


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

/**
 * Set title
 *
 * @param string $title
 * @return Post
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

/**
 * Get title
 *
 * @return string 
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set body
 *
 * @param string $body
 * @return Post
 */
public function setBody($body)
{
    $this->body = $body;

    return $this;
}

/**
 * Get body
 *
 * @return string
 */
public function getBody()
{
    return $this->body;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return Post
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime 
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 * @return Post
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

/**
 * Set author
 *
 * @param \Usuarios\UsersBundle\Entity\User $author
 * @return Post
 */
public function setAuthor(User $author)
{
    $this->author = $author;

    return $this;
}

/**
 * Get author
 *
 * @return \Usuarios\UsersBundle\Entity\User
 */
public function getAuthor()
{
    return $this->author;
}

/**
 * Set slug
 *
 * @param string $slug
 * @return Post
 */
 public function setSlug($slug)
{
    $this->slug = $slug;

    return $this;
}

/**
 * Get slug
 *
 * @return string 
 */
public function getSlug()
{
    return $this->slug;
}

/**
 * Add comments
 *
 * @param \Blog\BlogBundle\Entity\Comment $comments
 * @return Post
 */
public function addComment(Comment $comments)
{
    $this->comments[] = $comments;

    return $this;
}

/**
 * Remove comments
 *
 * @param \Blog\BlogBundle\Entity\Comment $comments
 */
public function removeComment(Comment $comments)
{
    $this->comments->removeElement($comments);
}

/**
 * Get comments
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getComments()
{
    return $this->comments;
}


public function setTags($tags)
{
    $this->tags = $tags;

    return $this;
}

public function getTags()
{
    $this->tags = $this->tags ?: new ArrayCollection();

    return $this->tags;
}

public function getTaggableType()
{
    return 'post_tag';
}

public function getTaggableId()
{
    return $this->getId();
}
}

0 个答案:

没有答案