symfony - 一对多关系不起作用

标签 symfony doctrine-orm

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

关联 Acme\BlogBu​​ndle\Entity\Blog#tags 指的是不存在的拥有方字段 Acme\BlogBu​​ndle\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;

博客.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;
    }
}

最佳答案

看看关于关联映射的官方文档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;

关于symfony - 一对多关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14448075/

相关文章:

database - 在 Symfony2 中生成数据库

symfony - PHP fatal error : Class 'Application\Sonata\MediaBundle\ApplicationSonataMediaBundle' not found in/var/www/znata. com/app/AppKernel.php 第 47 行

symfony - 为什么 Doctrine 不返回数组中的值?

使用 KnpPaginatorBundle 对连接表进行排序

security - Symfony2 : Injecting @security. 获取当前用户的上下文。如何避免 "ServiceCircularReferenceException"?注入(inject)整个容器?

mysql - 我想通过DQL获取与文本相关的记录

symfony - 在 JMS 中序列化对象时包含方法

mysql - Symfony 2/Doctrine 不在 varchar 字段中保存任何零

mysql - Doctrine 2 DQL CASE WHEN in Count

datetime - 如何将Doctrine2中的日期时间字段与日期进行比较?