一对多关系不起作用

时间:2013-01-21 22:04:05

标签: symfony doctrine-orm

在我的博客和我的blog_link_tag联接表之间创建一个简单的一对多关系时,我似乎遇到了问题。我几乎在那里但是我继续收到Doctrine的以下映射错误,我想知道是否有人可以指出我哪里出错?

The association Acme\BlogBundle\Entity\Blog#tags refers to the owning side field Acme\BlogBundle\Entity\BlogLinkTag#blog_id which does not exist.

下面是我正在使用的表格结构,删除了不必要的字段。

CREATE TABLE `blog` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `blog_link_tag` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `blog_id` int(3) NOT NULL,
  `tag_id` int(3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

blog.php的

<?php

namespace Acme\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Blog
 *
 * @ORM\Table(name="blog")
 * @ORM\Entity(repositoryClass="Acme\BlogBundle\Entity\BlogRepository")
 * @ORM\HasLifecycleCallbacks
 */

class Blog {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


    /**
     * @ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog_id")
     */
    protected $tags;


    public function __construct() {
        $this->tags = new ArrayCollection();
    }

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


    /**
     * Add tags
     *
     * @param \Acme\BlogBundle\Entity\BlogLinkTag $tags
     * @return Blog
     */
    public function addTag(\Acme\BlogBundle\Entity\BlogLinkTag $tags) {
        $this->tags[] = $tags;

        return $this;
    }

    /**
     * Remove tags
     *
     * @param \Acme\BlogBundle\Entity\BlogLinkTag $tags
     */
    public function removeTag(\Acme\BlogBundle\Entity\BlogLinkTag $tags) {
        $this->tags->removeElement($tags);
    }

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

    /**
     * Set tags
     *
     * @param integer $tags
     * @return Blog
     */
    public function setTags($tags) {
        $this->tags = $tags;
        return $this;
    }
}

BlogLinkTag.php

<?php

namespace Acme\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * BlogLinkTag
 *
 * @ORM\Table(name="blog_link_tag")
 * @ORM\Entity
*/
class BlogLinkTag
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     * @ORM\Column(name="blog_id", type="integer", nullable=false)
     * @ORM\ManyToOne(targetEntity="Blog", inversedBy="blog")
     * @ORM\JoinColumn(name="blog_id", referencedColumnName="blog_id")
     */
    private $blogId;

    /**
     * @var integer
     *
     * @ORM\Column(name="tag_id", type="integer", nullable=false)
     */
    private $tagId;

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


    /**
     * Set blogId
     *
     * @param integer $blogId
     * @return BlogLinkTag
     */
    public function setBlogId($blogId) {
        $this->blogId = $blogId;

        return $this;
    }

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

    /**
     * Set tagId
     *
     * @param integer $tagId
     * @return BlogLinkTag
     */
    public function setTagId($tagId) {
        $this->tagId = $tagId;
        return $this;
    }

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

0 个答案:

没有答案