Luis Mejía F.
Luis Mejía F.

Reputation: 70

Doctrine get confused when generating plural method of a field that ends with a letter "s"

I have an rntity called Account, which can have many phone numbers (or Dnis, as I have named the related Entity ).

The definition for Account using yml is:

models\Account:
  type: entity
  table: account
    oneToMany:
      dnis:
        targetEntity: models\Dnis
        mappedBy: account

The problem is when I generate the entities classes with the following command:

doctrine orm:generate:entities

Since it is a OneToMany relation, the Account entity has a dnis collection, the problem is that the "add" method gets named as "addDni".

/**
 * Add dni
 *
 * @param \application\models\Dnis $dni
 *
 * @return CreditAccount
 */
public function addDni(\application\models\Dnis $dni)
{
    $this->dnis[] = $dni;

    return $this;
}

/**
 * Remove dni
 *
 * @param \application\models\Dnis $dni
 */
public function removeDni(\application\models\Dnis $dni)
{
    $this->dnis->removeElement($dni);
}

I guess doctrine get confused because it thinks that the property "dnis" is a plural just because ends with a letter "s".

How can I let doctrine know that "dnis" is the actual name of the property? Or am I missing something here in the entity definition?

Thanks in advance.

Upvotes: 2

Views: 925

Answers (1)

chalasr
chalasr

Reputation: 13167

You are defining a OneToMany association.

That means that one Account can have many Dnis.
This is why Dnis is considered as plural, because it represents the Many side of the OneToMany association, also it's normal that doctrine generates a addDni method that add a given Dnis to the collection of Dnis, same for the removeDni that remove a given Dnis and for the getDnis that fetches the whole collection of Dnis.

If you need that an account can have one Dnis, define it as OneToOne and keep it as plural. You'll have a getDnis() and setDnis().

If you doesn't like the name of your variables (i.e. Dnis $dni), just change it (i.e. Dnis $dnis).

EDIT

I found a quick way to avoid this unexpected behavior and I submitted a pull request.
I'll let you know in case of the PR is merged, for now you can use my fix.

Upvotes: 1

Related Questions