Skip to content

Loading…

DDC-1497: Orm:generate-entities generates incorrect code when it has multiple relations with the same entity #2125

Closed
doctrinebot opened this Issue · 4 comments

2 participants

@doctrinebot

Jira issue originally created by user wishcow:

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
    /****
     * 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.

@doctrinebot

Comment created by venimus:

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);

@doctrinebot

Comment created by schyzophrenic:

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.

@doctrinebot

Comment created by @beberlei:

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

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.3 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.