• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

problem understanding relation mapping in doctrine 2

I read official documentation and tons of threads but still do not find solution for my situation. My case is very basic. I have 2 entities: comments and keywords for them. One comment can have many keywords but each keyword is only for one comment. Keywords are not unique in keyword table. So i decided this is one-to-many relation. Tables structure are simply like follows:

keywords

id          int(11)
comment_id  int(11)
text        varchar(30)

comments

id      int(11)
text    text

here is how i mapped them:


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

    /**
     * @OneToMany(targetEntity="keywords", mappedBy="comment_id")
     */
    private $keywords;

    public function getText(){return $this->text;}
    public function getId(){return $this->id;}
    public function getKeywords(){return $this->keywords;}
}
/**
 *  @Entity
 *  @Table(name="keywords")
 */

class Keywords
{
    /** @Id @Column(type="integer") */
    private $id;

    private $text;

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

and how using it is like this:


$comments = $this->em->getRepository('comments' )->findAll();
foreach($comments as $comment){
    foreach($comment->getKeywords() as $keyword){
        $keyword->getText();
    }
}

and got this errors:

Notice: Undefined index: comment_id in C:\web_includes\doctrine\ORM\Persisters\BasicEntityPersister.php on line 1096
Notice: Trying to get property of non-object in C:\web_includes\doctrine\ORM\Persisters\BasicEntityPersister.php on line 1098
Warning: Invalid argument supplied for foreach() in C:\web_includes\doctrine\ORM\Persisters\BasicEntityPersister.php on line 1098
Notice: Undefined index: comment_id in C:\web_includes\doctrine\ORM\PersistentCollection.php on line 168
Fatal error: Call to a member function setValue() on a non-object in C:\web_includes\doctrine\ORM\PersistentCollection.php on line 169
What is wrong? where is should define comment_id? Is my mapping correct? i really stuck and need help so please, any advice are very welcome.

      • 1
    • comment_id is a field in database in keywords table that have id of the comment this keyword belongs to. I suppose doctrine will use it when selecting keywords for comments. relation is like this OneToMany(targetEntity="keywords", mappedBy="comment_id") is it right?

The mappedBy attribute does say nothing about the name of the foreign key, That is what the "@JoinColumn" annotation is for. The correct mapping for this scenario would be:

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

    /**
     * @OneToMany(targetEntity="keywords", mappedBy="comment")
     */
    private $keywords;

    public function getText(){return $this->text;}
    public function getId(){return $this->id;}
    public function getKeywords(){return $this->keywords;}
}

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

    /**
     * @ManyToOne(targetEntity="Comments", inversedBy="keywords")
     */
    private $comment;

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

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

Using Schema Tool it generates the SQL which equals your schema:

CREATE TABLE comments (id INT NOT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE keywords (id INT NOT NULL, comment_id INT DEFAULT NULL, text LONGTEXT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE keywords ADD FOREIGN KEY (comment_id) REFERENCES comments(id);

Two problems in your mapping:

  1. You have to understand the difference between owning and inverse side. Just having a One-To-Many uni-directional relation requires a third join table. However with a bi-directional relation like in my mapping with the owning side property Keyword::$comment it works.
  2. "mappedBy" refers to the property on the other targetEntity that is "the other side of this associations". InversedBy has somewhat the same meaning, just that inversedBy is always specified on the owning side, mappedBy on the inverse side.

This all sounds very complicated, but its a very efficient way from the ORMs technical perspective, because it allows to update associations with the least number of required SQL UPDATE statements. See the docs on how Inverse/Owning works:

http://www.doctrine-project.org/projects/orm/2.0/docs/reference/association-mapping/en#owning-side-and-inverse-side

  • 14
Reply Report
    • THAT IS REALLY WORK! thanx a lot. But i have to ask what is a difference between uni-directional relations and bi-directional. Also, what entity in my case will be inverse side and what will be owning. I suppose comment in owning and keywodrs is inverse, but can't say why.
      • 1
    • Keywords is the owning side, because the foreign key "comment_id" is on the keywords table. Unidirectional means you could only walk from Comment to Keywords, using Comment::getKeywords(). Bi-directional means you can go in both directions with the additional Keyword::getComment() (missing in my example). The association chapter is rather lengthy, but you should read both the "Working with Associations" and "Association Mappings" chapters to get the whole concept.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions