1

I am experimenting this error when I execute orm:schema-tool:create. It's my first time with Doctrinem, so I've got several doubts, the one which -I think- causes the error is that I've got a class Player with 3 array of Comment objects. My idea of the db scheme is:

player_comment(id_player,id_comment)
commentsliked(id_player,id_comment)
commentsdisliked(id_player,id_comment)

Notice that comments is a bidirectional relationship although commentsLiked and commentsDisliked are not.

As far as I've understood, Doctrine wants to name the 3 previous tables "player_comment".

Thanks in advance.

My class Player look like this:

/**
 * @Entity @Table(name="players") 
 */
class Player
{
/** 
 * @Id @Column(type="integer") @GeneratedValue 
 * @var int
**/
private $id;

/** 
     * @OneToMany(targetEntity="Comment", mappedBy="writer")
 * @var Comment[]
    **/
private $comments = null;
/**
 * @ManyToMany(targetEntity="Comment")
     * @Table(name="commentsliked")
 * @var Comment[]
**/
private $commentsLiked = null;
/**
 * @ManyToMany(targetEntity="Comment")
     * @Table(name="commentsdisliked")
 * @var Comment[]
**/
private $commentDisliked = null;
}

My class Comment look like this:

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

/** 
     * @ManyToOne(targetEntity="Player", inversedBy="comments") 
 * @var Player
    **/
private $writer;
}
j0k
  • 22,600
  • 28
  • 79
  • 90
Jonás
  • 1,459
  • 4
  • 27
  • 43

3 Answers3

1

I was getting this problem from a conflict with join table defined in an association class annotation and a join table defined in a ManyToMany annotation.

The mapping definitions in two entities with a direct ManytoMany relationship appeared to result in the automatic creation of the join table using the 'joinTable' annotation. However the join table was already defined by an annotation in its underlying entity class and I wanted it to use this association entity class's own field definitions so as to extend the join table with additional custom fields.

The explanation and solution was thanks to this post in the forum 'Doctrine Annotation Question'. This post draws attention to the Doctrine documentation regarding ManyToMany Uni-directional relationships. Look at the note regarding the approach of using an 'association entity class' thus replacing the many-to-many annotation mapping directly between two main entity classes with a one-to-many annotation in the main entity classes and two 'many-to-one' annotations in the Associative Entity class. There is an example provided in this forum post Association models with extra fields:

public class Person {

  /** @OneToMany(targetEntity="AssignedItems", mappedBy="person") */
  private $assignedItems;

}

public class Items {

    /** @OneToMany(targetEntity="AssignedItems", mappedBy="item") */
    private $assignedPeople;
}

public class AssignedItems {

    /** @ManyToOne(targetEntity="Person")
    * @JoinColumn(name="person_id", referencedColumnName="id")
    */
private $person;

    /** @ManyToOne(targetEntity="Item")
    * @JoinColumn(name="item_id", referencedColumnName="id")
    */
private $item;

}

See also Example of a Doctrine 2.x many-to-many association class and Doctrine2: Best way to handle many-to-many with extra columns in reference table.

Community
  • 1
  • 1
Onshop
  • 2,915
  • 1
  • 27
  • 17
  • Thank you, @Ben. Of all answers related to this and similar questions you go in depth, refer to the manual and provide all the necessary information needed to understand the problem. – halfpastfour.am Sep 13 '17 at 08:55
0

I've used @JoinTable as indicated in http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-unidirectional.

I updated the annotations of commentsLiked and commentsDisliked:

/**
 * @ManyToMany(targetEntity="Comment")
 * @JoinTable(name="commentsliked",
 * joinColumns={@JoinColumn(name="player_id", referencedColumnName="id")},
 * inverseJoinColumns={@JoinColumn(name="commment_id",referencedColumnName="id")})
 * @var Coments[]
**/
private $commentsLiked = null;
/**
 * @ManyToMany(targetEntity="Comment")
 * @JoinTable(name="commentsDisliked",
 * joinColumns={@JoinColumn(name="player_id", referencedColumnName="id")},
 * inverseJoinColumns={@JoinColumn(name="comment_id",referencedColumnName="id")})     )
 * @var Comment[]
**/
private $commentsDisliked = null;
Jonás
  • 1,459
  • 4
  • 27
  • 43
0

One cool solution to that is to have a "updatable view" for the ManyToMany association and use a real table for the matrix entity. This way, you'll have benefits from both approaches.

Artur Bodera
  • 1,662
  • 22
  • 20