doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

generateDocumentStubMethods for ReferenceMany different to ORM #487

Closed Narretz closed 11 years ago

Narretz commented 11 years ago

In Doctrine\ODM\MongoDB\Tools\DocumentGenerator->generateDocumentStubMethods generates adders, getters and setters based on the type of field reference. For (@ MongoDB\ReferenceMany) , it generates an adder and a getter,

            } else if ($fieldMapping['type'] === ClassMetadataInfo::MANY) {
                if ($code = $this->generateDocumentStubMethod($metadata, 'add', $fieldMapping['fieldName'], isset($fieldMapping['targetDocument']) ? $fieldMapping['targetDocument'] : null)) {
                    $methods[] = $code;
                }
                if ($code = $this->generateDocumentStubMethod($metadata, 'get', $fieldMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) {
                    $methods[] = $code;
                }
            }

while Doctrine\ORM\Tools\EntityGenerator generates an adder, remover and getter:

            } else if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
                if ($code = $this->generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
                    $methods[] = $code;
                }
                if ($code = $this->generateEntityStubMethod($metadata, 'remove', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
                    $methods[] = $code;
                }
                if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) {
                    $methods[] = $code;
                }
            }

It also singularizes the field name, which the ODM does not.

This is okay. When I use it together with Symfony 2.1.8 and I have the referenced documents in a collection FormType that I try to update. I get the following exception:

Property "tags" is not public in class "AcmeBundle\Document\Product", 
nor could adders and removers be found based on the guessed singulars: Tag. 
Maybe you should create the method "setTags()"? 

You can then either create a setter, or make the adder singular and add a remover, then it works.

Is there a reason this is different from the ORM or is this a bug?

jmikola commented 11 years ago

Regarding the singular/plural form of words, I left a comment in https://github.com/doctrine/doctrine2/commit/5cdb0ae8beaf29d924f0362341d4965cca0f1dbe#commitcomment-2602042.

I'll tend to the rest of this PR now, though.

jmikola commented 11 years ago

I copied over the ORM implementation. You may find issues with improper plural => singular conversions, which I suppose can be manually corrected in the generated code.

@jwage: I don't think there's any reason to generate a setX() method here, is there? addX() and removeX() certainly play nicer with change-tracking.