DDC-56: ManyToMany mapping does not take difference between field name and column name into account #68

Closed
opened 2026-01-22 12:25:51 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 18, 2009).

Jira issue originally created by user marijn:

The fields of my entities are prefixed with an underscore since they are private variables. Since I don't like my database schema that way I've named all the columns without the underscore, thus far no problems. Now when you create a ManyToMany mapping a problem occurs. The column name in the database is "fieldname" but the fieldname = "_fieldname". We need an annotation in the form of @referencedFieldName to specify the relevant field name.

Originally created by @doctrinebot on GitHub (Oct 18, 2009). Jira issue originally created by user marijn: The fields of my entities are prefixed with an underscore since they are private variables. Since I don't like my database schema that way I've named all the columns without the underscore, thus far no problems. Now when you create a ManyToMany mapping a problem occurs. The column name in the database is "fieldname" but the fieldname = "_fieldname". We need an annotation in the form of @referencedFieldName to specify the relevant field name.
admin added the Bug label 2026-01-22 12:25:52 +01:00
admin closed this issue 2026-01-22 12:25:53 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 18, 2009):

Comment created by romanb:

I dont understand. Can you give an example?

@doctrinebot commented on GitHub (Oct 18, 2009): Comment created by romanb: I dont understand. Can you give an example?
Author
Owner

@doctrinebot commented on GitHub (Oct 18, 2009):

Comment created by marijn:

For the example the following model definition is used.

<?php

  namespace Entities;

  /****
   * @MappedSuperclass
   */
  abstract class EntityAbstract
  {

    /****
     * @Id @Column(name="id", type="smallint")
     * @GeneratedValue(strategy="AUTO")
     */
    private $_id;

    /** ... **/

  }
<?php

  namespace Entities;

  /****
   * @Entity
   * @Table(name="documents")
   */
  class Document extends EntityAbstract
  {

    /****
     * @Column(name="uri", type="string")
     */
    private $_uri;

    /****
     * @Column(name="culture", type="string")
     */
    private $_culture;

    /****
     * @Column(name="description", type="string")
     */
    private $_description;

    /****
     * @Column(name="title", type="string")
     */
    private $_title;

    /****
     * @ManyToMany(targetEntity="Keyword")
     * @JoinTable(name="document*keywords", joinColumns={@JoinColumn(name="document_id", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="keyword*id", referencedColumnName="id")})
     */
    private $_keywords;

    /** ... **/

  }
<?php

  /****
   * @Entity
   * @Table(name="keywords")
   */
  class Keyword extends EntityAbstract
  {

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

    /****
     * @ManyToMany(targetEntity="Document", mappedBy="_keywords")
     */
    private $_documents;

    /** ... **/

  }

The following query issues this error message: {color:red}ErrorException: Undefined index: id in /path/to/Doctrine/ORM/Mapping/ClassMetadata.php on line 257{color}

<?php

  $query = $em->createQuery('SELECT d FROM Entities\Document d LEFT JOIN d._keywords k');

   printf("<pre>\n%s\n</pre>", $query->getArrayResult());
@doctrinebot commented on GitHub (Oct 18, 2009): Comment created by marijn: For the example the following model definition is used. ``` <?php namespace Entities; /**** * @MappedSuperclass */ abstract class EntityAbstract { /**** * @Id @Column(name="id", type="smallint") * @GeneratedValue(strategy="AUTO") */ private $_id; /** ... **/ } ``` ``` <?php namespace Entities; /**** * @Entity * @Table(name="documents") */ class Document extends EntityAbstract { /**** * @Column(name="uri", type="string") */ private $_uri; /**** * @Column(name="culture", type="string") */ private $_culture; /**** * @Column(name="description", type="string") */ private $_description; /**** * @Column(name="title", type="string") */ private $_title; /**** * @ManyToMany(targetEntity="Keyword") * @JoinTable(name="document*keywords", joinColumns={@JoinColumn(name="document_id", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="keyword*id", referencedColumnName="id")}) */ private $_keywords; /** ... **/ } ``` ``` <?php /**** * @Entity * @Table(name="keywords") */ class Keyword extends EntityAbstract { /**** * @Column(name="name", type="string") */ private $_name; /**** * @ManyToMany(targetEntity="Document", mappedBy="_keywords") */ private $_documents; /** ... **/ } ``` The following query issues this error message: {color:red}ErrorException: Undefined index: id in /path/to/Doctrine/ORM/Mapping/ClassMetadata.php on line 257{color} ``` <?php $query = $em->createQuery('SELECT d FROM Entities\Document d LEFT JOIN d._keywords k'); printf("<pre>\n%s\n</pre>", $query->getArrayResult()); ```
Author
Owner

@doctrinebot commented on GitHub (Oct 18, 2009):

Comment created by romanb:

Should be fixed now.

@doctrinebot commented on GitHub (Oct 18, 2009): Comment created by romanb: Should be fixed now.
Author
Owner

@doctrinebot commented on GitHub (Oct 18, 2009):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 18, 2009): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#68