Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-56

ManyToMany mapping does not take difference between field name and column name into account

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-ALPHA2
    • Fix Version/s: 2.0-ALPHA3
    • Component/s: Mapping Drivers, ORM
    • Security Level: All
    • Labels:
      None

      Description

      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.

        Activity

        Hide
        Roman S. Borschel added a comment -

        I dont understand. Can you give an example?

        Show
        Roman S. Borschel added a comment - I dont understand. Can you give an example?
        Hide
        Marijn Huizendveld added a comment -

        For the example the following model definition is used.

        'EntityAbstract.php'
        <?php
        
          namespace Entities;
        
          /**
           * @MappedSuperclass
           */
          abstract class EntityAbstract
          {
        
            /**
             * @Id @Column(name="id", type="smallint")
             * @GeneratedValue(strategy="AUTO")
             */
            private $_id;
        
            /* ... */
        
          }
        
        'Document.php'
        <?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;
        
            /* ... */
        
          }
        
        'Keyword.php'
        <?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: ErrorException: Undefined index: id in /path/to/Doctrine/ORM/Mapping/ClassMetadata.php on line 257

        DQL
        <?php
        
          $query = $em->createQuery('SELECT d FROM Entities\Document d LEFT JOIN d._keywords k');
        
           printf("<pre>\n%s\n</pre>", $query->getArrayResult());
        
        Show
        Marijn Huizendveld added a comment - For the example the following model definition is used. 'EntityAbstract.php' <?php namespace Entities; /** * @MappedSuperclass */ abstract class EntityAbstract { /** * @Id @Column(name= "id" , type= "smallint" ) * @GeneratedValue(strategy= "AUTO" ) */ private $_id; /* ... */ } 'Document.php' <?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; /* ... */ } 'Keyword.php' <?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: ErrorException: Undefined index: id in /path/to/Doctrine/ORM/Mapping/ClassMetadata.php on line 257 DQL <?php $query = $em->createQuery('SELECT d FROM Entities\Document d LEFT JOIN d._keywords k'); printf( "<pre>\n%s\n</pre>" , $query->getArrayResult());
        Hide
        Roman S. Borschel added a comment -

        Should be fixed now.

        Show
        Roman S. Borschel added a comment - Should be fixed now.

          People

          • Assignee:
            Roman S. Borschel
            Reporter:
            Marijn Huizendveld
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: