【问题标题】:Underscore in gets and sets in symfony entitysymfony 实体中的获取和设置中的下划线
【发布时间】:2018-01-04 10:03:00
【问题描述】:

在我的实体中,我在变量名中有一个带下划线的字段,如下所示:

/**
 * @ORM\Column(type="text", nullable=true)
 */
private $value_text;

还有,例如,一个 get 函数:

public function getValueText()
{
    return $this->value_dec;
}

这样,symfony 就会抛出异常:

Neither the property "value_text" nor one of the methods "value_text()", "getvalue_text()"/"isvalue_text()" or "__call()" exist and have public access in class "AppBundle\Entity\MyEntity".

所以我更改了函数名,添加了下划线,但是当我想添加一行时,symfony 会抛出同样的错误,但另一方面 - 它忽略了下划线,并寻找像 getValueText( )。

为什么会这样?我已经让两个函数做同样的事情,我的代码可以工作,但是我当然认为有更好的方法可以做到这一点,而无需加倍函数。

【问题讨论】:

标签: php symfony doctrine-orm doctrine twig


【解决方案1】:

尝试使用这个:

public function getValue_dec() {
    return $this->value_dec;
}

但惯例是使用驼峰式,所以最好这样:

/**
 * @ORM\Column(type="text", nullable=true)
 */
private $valueDec;

public function getValueDec()
{
    return $this->valueDec;
}

【讨论】:

    【解决方案2】:

    Symfony 使用 camelCase 作为属性命名约定。

    这是Naming Conventions的官方文档

    阅读更多相关文章

    [PropertyInfo] Implement Naming Strategy for Properties

    Added support for non-camel-case variable code conventions #29

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 1970-01-01
      • 2011-03-03
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多