One-to-Many relation not working

2019-05-14 06:15发布

I appear to be having a problem creating a simple One to Many relationship between my blog and my blog_link_tag join table. I'm almost there however I keep on receiving the following mapping error from Doctrine and I'm wondering if anyone can point out where I'm going wrong?

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

Below is the table structure I'm using with unnecessary fields removed.

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;
    }
}

1条回答
冷血范
2楼-- · 2019-05-14 06:35

Take a look at the official documentation about association mapping here.

Try this :

In 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;

In Blog :

    /**
     * @ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog") //put the name of the variable in the other entity here
     */
    protected $tags;
查看更多
登录 后发表回答