kakashi
kakashi

Reputation: 23

Associations with Doctrine 2.1

I'm busy with Doctrine these days. but i'm confused about associations. while i designate an entity, unfortunately i can't do associations between in two tables. one2many, many2one or whatever.

i read document about Associations Mapping in website. and i'm so confused.

I wanna do simple example. Please help me !

here we go !

I have two tables. One of them is users and the other one is comments. I know that is very simple but everything is for my benefaction.

users table has three columns that are ID, name, comment_id.

comments table has id and comment_text columns. (i think, problem is here)

and now that my users.php file

/** @Entity @Table(name="users") */
class User{

/** @Id @GeneratedValue @Column(type="integer") */
private $id;

/** @Column(name="name", length=55, type="string") */
private $name;

/**
 * @OneToMany(targetEntity="Comment", mappedBy="author")
 */
private $commentID;

public function addComment($comment){

    $this->commentID[] = $comment;

}

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

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

public function __construct()
   {
       $this->commentID = new Doctrine\Common\Collections\ArrayCollection();
   }
}

and comments.php file

/** @Entity @Table(name="comments") */
class Comment{
   /** @Id @GeneratedValue @Column(name="author", type="integer")
    * @ManyToOne(targetEntity="User", inversedBy="commentID")
   */
private $author;

   /** @Column(name="comment_text", type="text") */
private $commentText;

public function setAuthor($author)
  {
      $author->addComment($this);
      $this->author = $author;
  }

public function setComment($new){
     $this->commentText = $new;
  }

public function getComment(){
      return $this->commentText;
  }
}

that's it. I wanna insert, select, update and delete items in two tables.

$person = new User();
$person->setName('Jack Sparrow');

$comment = new Comment();
$comment->setComment('Hello pirates !');
$comment->setAuthor($person);

$em->persist($person);
$em->persist($comment);
$em->flush();

that code works well but value of comment_id column always is zero = 0 in users table. I think i have a big wrong in somewhere.

Help where i am wrong !

regards....

Upvotes: 2

Views: 477

Answers (1)

J0HN
J0HN

Reputation: 26921

You are misusing the @ManyToOne on your comment entity. You are trying to link it with comment_id column, but that's not necessary, and in your example is wrong. It should be something like the following (just a sample, correct code below)

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

    /**
     * @ManyToOne(targetEntity="User", inversedBy="commentID")
     */
    private $author;
    //other declarations//
}

Also, I doubt that your database is designed correctly. In your current schema, you have User->Comment association, so actually User is on Many side of relation. So, for User to have multiple Comments your db structure should be the following (pseudocode):

users (id, name)
comments(id, comment_text, author_id)

In this case, your Entitites declarations will be

/** @Entity @Table(name="users") */
class User{

    /** @Id @GeneratedValue @Column(type="integer") */
    private $id;

    /** @Column(name="name", length=55, type="string") */
    private $name;

    /**
     * @OneToMany(targetEntity="Comment", mappedBy="author")
     */
    private $comments;

    //Other declarations//
}

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

    /** @Column(name="comment_text", type="text") */
    private $text;

    /**
     * @ManyToOne(targetEntity="User", inversedBy="comments")
     * @JoinColumn(name="author_id", referencesdColumn="id")
     */
    private $author;

    //Other declarations//
}

Have a look at Associations Mapping chapter of Doctrine 2.0 reference, it contains samples of common associations use-cases.

Upvotes: 2

Related Questions