[DDC-1648] Primary Keys as Foreign Keys - still not working in Reverse Engineering in 2.1.6 Created: 12/Feb/12  Updated: 14/Mar/12  Resolved: 14/Mar/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: None
Affects Version/s: 2.1.6
Fix Version/s: 2.1.7, 2.2.2
Security Level: All

Type: Bug Priority: Major
Reporter: ross neacoders Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
Labels: None


Please, read this old thread - looks like this problem still is not solved


>>The primary key as foreign key limitation is from the 2.0 manual. You are using 2.1 with symfony, which supports this. Please use the correct manual.

>> The only problem is that the reverse engineering does not support this. You have to reverse engineer yourself, or write a patch to have Doctrine support this. I would really appreciate it

As I understand it is supported in the "core", but some reverse engineer script is still not using that possibility.
Could you at least point where this can fixed?

Comment by Benjamin Eberlei [ 12/Feb/12 ]

In lib/Doctrine/ORM/Mapping/DatabaseDriver.php

Comment by ross neacoders [ 12/Feb/12 ]

Thank you Benjamin,

Actually started tracking this error from /lib/Doctrine/ORM/Mapping/ClasMetadataInfo.php and step-bystep it goes to

Actually it calls ClasMetadataInfo::mapMayToOne instead of mapOneToOne in such situations.
Will investigate further. Pff... DatabaseDriver.php looks a hard class.

Comment by ross neacoders [ 12/Feb/12 ]

I am almost done with it - need a little more help from you:

So I fixed the DatabaseDriver so it correctly calls mapOneToOne
and passes association_mapping to it.

     * Adds a one-to-one mapping.
     * @param array $mapping The mapping.
    public function mapOneToOne(array $mapping)
        $mapping['type'] = self::ONE_TO_ONE;
        $mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
print_r($assocMapping); <--added this to see what structure we have on output

this gives the following structure

    [fieldName] => someguid                      <--- my primary key field, which is also foreign key
    [targetEntity] => Additionalinfo               <--- target table
    [id] => 1                                                      <--- indicates that this mapping is also id (primary key)
    [joinColumns] => Array
            [0] => Array
                    [name] => Someguid                                                     <--- join columns
                    [referencedColumnName] => UniqueID                    


    [type] => 1  <-- indicates ONE-TO-ONE assoc mapping
    [mappedBy] => 
    [inversedBy] => 
    [isOwningSide] => 1
    [sourceEntity] => Sampledata
    [fetch] => 2
..... (i omit other data as not important)

Now the problem: in my entity I got the following

     * @var Additionalinfo
     * @ORM\OneToOne(targetEntity="Additionalinfo")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="Someguid", referencedColumnName="UniqueID")
     * })
    private $someguid;

So it correctly created a One-To-One mapping, but it did not preserve @Id attribute.

I guess this is because field_associations and mapping_associations are processed in separate way,
and in mapping_association it ignores the $mapping["id"] setting.

Do you know what code is managing creating the <id ....> / @Id staff - it is common for annotation/yaml/xml, e.x. when i generated xml for entity, it also does not contain <id > tag.

Comment by ross neacoders [ 12/Feb/12 ]

Update: fixed the last problem in Doctrine/ORM/Tools/EntityGenerator.php

Still has to be fixed in YAML/XML exporters - not very familiar with the syntax.

Comment by Benjamin Eberlei [ 03/Mar/12 ]

This PR fixes the problem https://github.com/doctrine/doctrine2/pull/280

Comment by Benjamin Eberlei [ 14/Mar/12 ]


Generated at Wed Dec 17 23:15:10 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.