Skip to content

Loading…

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

Closed
doctrinebot opened this Issue · 4 comments

1 participant

@doctrinebot

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.

@doctrinebot

Comment created by romanb:

I dont understand. Can you give an example?

@doctrinebot

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

Comment created by romanb:

Should be fixed now.

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added this to the 2.0-ALPHA3 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.