Question

I've been struggling with Doctrine, I've got an entity for blog posts, which references comments and it's throwing the following error: Notice: Undefined index: post_id

It also gets all of the comments regardless of post_id. Here's my mapping:

   /**
    * @OneToMany(targetEntity="CommentsBundle\Entities\Comments", mappedBy="post_id")
    */
    protected $comments;

EDIT

Here's the comments entity:

<?php

namespace CommentsBundle\Entities;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @Entity @Table(name="comments")
 **/
class Comments
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;

/** @Column(type="string") */
protected $name;

/** @Column(type="string") */
protected $email;

/** @Column(type="string") */
protected $content;

/** @Column(type="string") */
protected $date;

/** @Column(type="integer") */
protected $user_id;

/** @Column(type="integer") */
protected $post_id;

/**
 * @ORM\ManyToOne(targetEntity="ContentBundle\Entities\Posts", inversedBy="comments")
 * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
 */
protected $post;

public function setId( $id ) 
{
    $this->id = $id;

    return $this;
}

public function getId()
{
    return $this->id;
}

public function setName( $name ) 
{
    $this->name = $name;

    return $this;
}

public function getName() 
{
    return $this->name;
}

public function setEmail( $email ) 
{
    $this->email = $email;

    return $this;
}

public function getEmail()
{
    return $this->email;
}

public function setContent( $content ) 
{
    $this->content = $content;

    return $this;
}

public function getContent() 
{
    return $this->content;
}

public function setDate( $date ) 
{
    $this->date = $date;

    return $this;
}

public function getDate() 
{
    return $this->date;
}

public function setUser_id( $user_id ) 
{
    $this->user_id = $user_id;
}

public function getUser_id() 
{
    return $this->user_id;
}

public function setPost_id( $post_id ) 
{
    $this->post_id = $post_id;

    return $this;
}

public function getPost_id() 
{
    return $this->post_id;
}
}

Thanks in advance!

Was it helpful?

Solution

You've got to write your mappedBy on the Entity's property, not the column.

Basically, the entities' properties must "talk to each other".

$comments is mapped by $post, $post is inversed by $comments :

class Posts
{
    /**
    * @OneToMany(targetEntity="CommentsBundle\Entities\Comments", mappedBy="post")
    */
    protected $comments;
}

class Comments
{
    /**
     * @ORM\ManyToOne(targetEntity="ContentBundle\Entities\Posts", inversedBy="comments")
     * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
     */
    protected $post;
}

Also, I wouldn't have defined $post_id in your Comments entity. Just $post, and when you need to retrieve the post's id then:

public function getPost_id()
{
    return $this->post->getId();
}

Things are cleaner this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top