2013-01-21 93 views
3

我似乎在創建我的博客和我的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

該協會的Acme \ BlogBu​​ndle \實體\博客#標籤是指擁有方現場的Acme \ BlogBu​​ndle \ Entity \ BlogLinkTag#blog_id不存在意味着BlogLinkTag是所有者/父母,當我認爲Blog是所有者並且blog_link_tag.blog_ID是FK to Blog.ID – xQbert

+0

當然在這個實例中Blo如果我沒有弄錯,gLinkTag(ManyToOne)應該是擁有方? –

回答

6

看看關於關聯映射here官方文檔。

試試這個:

在BlogLinkTag

/** 
    * @var integer 
    * @ORM\ManyToOne(targetEntity="Blog", inversedBy="tags") //put the name of the variable in the other entity here 
    * @ORM\JoinColumn(name="blog_id", referencedColumnName="id") //reference of the column targetted here 
    */ 
    private $blog; 

在博客:

/** 
    * @ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog") //put the name of the variable in the other entity here 
    */ 
    protected $tags; 
+0

完成這項工作!我一直在閱讀關聯映射文檔,但我可以將其付諸實踐。現在我有一個工作模式,我應該能夠理解它。謝謝。 –