dselp3944 2016-03-21 23:06
浏览 35
已采纳

当生成以字母“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.

  • 写回答

1条回答 默认 最新

  • douju6542 2016-03-21 23:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大