简体   繁体   中英

OneToOne relationship in Doctrine2 ORM and Symfony

I have two entity called PictureTag and Tag, here's the relationship:

/**
 * @ORM\Entity
 * @ORM\Table(name="picture_tag")
 * @ORM\HasLifecycleCallbacks()
 */
class PictureTag
{
     /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     *
     * @ORM\OneToOne(targetEntity="App\MainBundle\Entity\Tag", inversedBy="id")
     * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", nullable=false)
     */
    private $tag;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="tag")
 * @ORM\HasLifecycleCallbacks()
 */
class Tag
{
     /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\ManyToOne(targetEntity="App\MainBundle\Entity\PictureTag", inversedBy="tag")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @ORM\Column(name="tag", type="string", nullable=true)
     */
    private $tag;

}

Basically I wanted the tag table to contain all the unique tags, so there are no duplicate in the tag. And i wanted picturetag to have a joincolumn that points to the id of the tag. So here's my code in the controller:

foreach ($image->tags as $tag) {
    $existingTag = $em->getRepository('AppMainBundle:InstagramTag')->findOneByTag($tag);
    $instaPictureTag = new PictureTag();
    if ($existingTag) {
        $instaPictureTag->setTag($existingTag);
    } else {
         $instagramTag = new Tag();
         $instagramTag->setTag($tag);
         $em->persist($instagramTag);
         $instaPictureTag->setTag($instagramTag);
    }

    $instaPictureTag->setPicture($instaShopPicture);
    $em->persist($instaPictureTag);

}

So the relationship is basically:

One picture is going to have many tags. And one tag will belong to many pictures. 

Picture tag here is the intermediary table.

Basically I am doing a check if the tag already exists in the tag table, if it is then I set the picturetag to be associated with this tag, if not then create one. However doing so generates the following error:

Fatal error: Call to a member function setValue() on a non-object in /Users/MyName/Sites/App/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 2625

Any idea why this is?

As I understand, you want implement OneToMany bidirectional relation between PictureTag and Tag.

Please try:

/**
 * @ORM\Entity
 * @ORM\Table(name="picture_tag")
 * @ORM\HasLifecycleCallbacks()
 */
class PictureTag
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="DynamicSolutions\Bundle\ProsAndConsBundle\Entity\Tag", inversedBy="pictureTags")
     * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", nullable=false)
     */
    private $tag;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="tag")
 * @ORM\HasLifecycleCallbacks()
 */
class Tag
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="PictureTag", mappedBy="tag")
     */
    private $pictureTags;

    /**
     * @var string
     * @ORM\Column(name="tag", type="string", nullable=true)
     */
    private $tag;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM