Symfony 一对多关系不起作用

Symfony 一对多关系不起作用,symfony,doctrine-orm,Symfony,Doctrine Orm,我似乎在创建我的博客和我的博客链接标签联接表之间的简单一对多关系时遇到问题。我就快到了,但是我一直收到来自条令的以下映射错误,我想知道是否有人能指出我哪里出了问题 关联Acme\BlogBundle\Entity\Blog#标记指的是不存在的拥有方字段Acme\BlogBundle\Entity\BlogLinkTag#Blog#id。 下面是我使用的表结构,删除了不必要的字段 CREATE TABLE `blog` ( `id` int(11) NOT NULL AUTO_INCREMEN

我似乎在创建我的博客和我的博客链接标签联接表之间的简单一对多关系时遇到问题。我就快到了,但是我一直收到来自条令的以下映射错误,我想知道是否有人能指出我哪里出了问题


关联Acme\BlogBundle\Entity\Blog#标记指的是不存在的拥有方字段Acme\BlogBundle\Entity\BlogLinkTag#Blog#id。

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

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中

    /**
     * @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;

关联Acme\BlogBundle\Entity\Blog#tags指的是拥有方字段Acme\BlogBundle\Entity\BlogLinkTag#Blog#id,它不存在,这意味着BlogLinkTag是所有者/父级,而Blog(链接)tag.Blog#id是Blog.id的FK,在本例中通常是BlogLinkTag(manytone)如果我没弄错的话,应该是拥有方吗?那工作做得很好!我一直在阅读关联映射文档,但我只能将其付诸实践。现在我有了一个工作模型,我应该能够理解它。非常感谢。
    /**
     * @ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog") //put the name of the variable in the other entity here
     */
    protected $tags;