[DDC-1497] Orm:generate-entities generates incorrect code when it has multiple relations with the same entity Created: 17/Nov/11  Updated: 27/May/12  Resolved: 27/May/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: Tools
Affects Version/s: 2.1.2
Fix Version/s: 2.3
Security Level: All

Type: Bug Priority: Major
Reporter: Kéri Norbert Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 2
Labels: None
Environment:

ArchLinux, PHP 5.3.8



 Description   

Using the same entities, as in the cookbook, when you generate the entities, the add* methods on User, or generally, on the inverse for many type relations are incorrect, and overwrite each other, so you end up with only one add method, that is incorrectly named.

Rework\Model\User:
    type: entity
    table: user
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
    oneToMany:
        reportedBugs:
            targetEntity: Bug
            mappedBy: reporter
        assignedBugs:
            targetEntity: Bug
            mappedBy: engineer

Rework\Model\Bug:
    type: entity
    table: bug
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
    manyToOne:
        reporter:
            targetEntity: User
            inversedBy: reportedBugs
        engineer:
            targetEntity: User
            inversedBy: assignedBugs
The generated method on User.php
    /**
     * Add reportedBugs
     *
     * @param Rework\Model\Bug $reportedBugs
     */
    public function addBug(\Rework\Model\Bug $reportedBugs)
    {
        $this->reportedBugs[] = $reportedBugs;
    }

What I expected:
The method should be named addReportedBugs, eg. it should be named after the relation, and not the entity's classname.
There should also be two add* methods, instead there is only one.



 Comments   
Comment by venimus [ 17/Jan/12 ]

I also reproduce that with a manyToMany relations

The misbehavior I found to be in the EntityGenerator.php on lines 706-712

 
        if ($type == "add") {
            $addMethod = explode("\\", $typeHint);
            $addMethod = end($addMethod);
            $methodName = $type . $addMethod;
        } else {
            $methodName = $type . Inflector::classify($fieldName);
        }

the whole block should be simply

 $methodName = $type . Inflector::classify($fieldName); 
Comment by Schyzophrenic [ 31/Mar/12 ]

I have also reproduced the very same issue on Doctrine 2.1.4 on a Win7 environment (using Symfony2, but it is not really relevant here).
While waiting for a fix, I will use the provided workaround.

Comment by Benjamin Eberlei [ 27/May/12 ]

Fixed but merged only into master, since this is also a BC break.

Generated at Mon Nov 24 16:28:33 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.