[DDC-1268] generate:entities creates bad method names with OneToMany Created: 11/Jul/11  Updated: 04/Dec/12  Resolved: 12/Jul/11

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

Type: Bug Priority: Minor
Reporter: Alessandro Desantis Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
Labels: None
Environment:

Ubuntu 11.04, PHP 5.3.5, Symfony 2.0 RC4



 Description   

Hi. I noticed a minor bug in the generate:entities console task. I've got a Region entity that had the following property:

/**
 * @ORM\OneToMany(targetEntity="District", mappedBy="region")
 */
private $districts;

public function __construct()
{
    $this->districts = new ArrayCollection();
}

When I ran the generate:entities task it created the following methods:

/**
 * Add districts
 *
 * @param Fenix\StudyBundle\Entity\District $districts
 */
public function addDistricts(\Fenix\StudyBundle\Entity\District $districts)
{
    $this->districts[] = $districts;
}

/**
 * Get districts
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getDistricts()
{
    return $this->districts;
}

The second method's name is correct, but what about addDistricts? Since it allows me to add a single District entity it should be as follows:

/**
 * Add district
 *
 * @param Fenix\StudyBundle\Entity\District $district
 */
public function addDistrict(\Fenix\StudyBundle\Entity\District $district)
{
    $this->districts[] = $district;
}

In other words, the term after "add" (as well as the parameter's name) should be the entity's name, not the property's one.



 Comments   
Comment by Benjamin Eberlei [ 11/Jul/11 ]

The problem is that making singular out of a word is not an easy task and i will not try to make this work somehow. The Entity Generator result is a helper, not necessarily generating the final result of a class. This is one case where the Code Generation will not nmake you happy, so just change it.

Comment by Alessandro Desantis [ 11/Jul/11 ]

Couldn't you just use the target Entity's name (and not the property's one) in the "add" method?

Comment by Benjamin Eberlei [ 11/Jul/11 ]

The shortname you mean? Yeah that would be an option you are right, i reopen the ticket

Comment by Benjamin Eberlei [ 12/Jul/11 ]

Fixed.

This is sort of a BC break for 2.1, however since this is just a tool and generating code i think its acceptable

Comment by Alessandro Desantis [ 12/Jul/11 ]

Thanks a lot

Comment by Matthew Larson [ 23/Jul/11 ]

Another option for a semantic method name would be to use 'addTo' as the prefix, as in 'addToDistricts'.

The problem with using the target Entity's name (instead of the property's) is that sometimes you need to have multiple properties that map to the same target Entity.

Generated at Fri Aug 01 22:38:32 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.