6

拥有以下实体:

ID; } /** * @return string */ public function getLat(): string { return $this->lat; } /** * @param string $lat */ public function setLat(string $lat): void { $this->lat = $lat; } /** * @return string */ public function getLng(): string { return $this->lng; } /** * @param string $lng */ public function setLng(string $lng): void { $this->lng = $lng; } /** * @return string */ public function getStreet(): string { return $this->street; } /** * @param string $street */ public function setStreet(string $street): void { $this->street = $street; } /** * @return string */ public function getZipcode(): string { return $this->zipcode; } /** * @param string $zipcode */ public function setZipcode(string $zipcode): void { $this->zipcode = $zipcode; } /** * @return string */ public function getCity(): string { return $this->city; } /** * @param string $city */ public function setCity(string $city): void { $this->city = $city; } /** * @return string */ public function getDescription(): string { return $this->description; } /** * @param string $description */ public function setDescription(string $description): void { $this->description = $description; } /** * @var string * * @ORM\Column(name="lat", type="decimal", precision=10, scale=8, nullable=false) */ private $lat; /** * @var string * * @ORM\Column(name="lng", type="decimal", precision=10, scale=8, nullable=false) */ private $lng; /** * @var 字符串 * * @ORM\Column(name=" [在此处输入图像描述][2]][2] 我收到此错误:>> "type": "https://www.rfc-editor.org/rfc/rfc2616#section-10", "title": "发生错误", "detail": "类型错误:App\\Entity\\Address::getLat() 的返回值必须是字符串类型,返回null", [![在此处输入图片描述][3 ]][3] 我的错在哪里?使用 Symfony 4.0。[1]:https://i.stack.imgur.com/OQw4b.png [2]:https://i.stack.imgur.com/HrFT3.png [3]:https://i.stack。 imgur.com/PCPww.png [在此处输入图像描述][2]][2] 我收到此错误:>> "type": "https://www.rfc-editor.org/rfc/rfc2616#section-10", "title": "发生错误", "detail": "类型错误:App\\Entity\\Address::getLat() 的返回值必须是字符串类型,返回null", [![在此处输入图片描述][3 ]][3] 我的错在哪里?使用 Symfony 4.0。[1]:https://i.stack.imgur.com/OQw4b.png [2]:https://i.stack.imgur.com/HrFT3.png [3]:https://i.stack。 imgur.com/PCPww.png [![在此处输入图片描述][3]][3] 我的错在哪里?使用 Symfony 4.0。[1]:https://i.stack.imgur.com/OQw4b.png [2]:https://i.stack.imgur.com/HrFT3.png [3]:https://i.stack。 imgur.com/PCPww.png [![在此处输入图片描述][3]][3] 我的错在哪里?使用 Symfony 4.0。[1]:https://i.stack.imgur.com/OQw4b.png [2]:https://i.stack.imgur.com/HrFT3.png [3]:https://i.stack。 imgur.com/PCPww.png
4

2 回答 2

22

gettergetLat具有返回类型提示string,这意味着只接受实际的字符串值(这也意味着:没有空值!)作为返回值被接受。

您没有显示实际使用实体的代码,但它基本上归结为对象中每个属性的默认值(null如果没有以不同方式定义)。

看这个例子:

$address = new Address();

// the following lines will produce an error in your example
// because your return type hint doesn't allow null values

$address->getId();     // returns null
$address->getStreet(); // returns null
$address->getLat();    // returns null

$address->setLat("4.56789");

$address->getLat();   // returns "4.56789"

关于教义的注意事项:

$address = $addressRepo->find(123);如果数据库中的值设置正确,则在 Doctrine 填充实体后(例如 via ),您将不会遇到此问题。只有当您自己创建一个新实体然后尝试使用 getter 方法时,才会发生这种情况。

可能的解决方案:

1.) 允许空值作为返回值。在返回类型提示前加上问号,如下所示:

/**
 * @return string|null
 */
public function getLat(): ?string
{
    return $this->lat;
}

但是如果你这样做,你的代码必须准备好处理来自这些方法的空值!

2.) 在对象中使用正确的数据类型定义默认值:

/**
 * @var string
 *
 * @ORM\Column(name="lat", type="decimal", precision=10, scale=8, nullable=false)
 */
private $lat = "";

您的代码必须准备好将空字符串作为返回值处理!或者,您还可以在构造函数方法中定义默认值。

3.) 通过使这些属性成为构造函数的参数来要求这些属性可用:

public function __constructor(string $lat, string $lng /*, add the other required properties */) {
    $this->lat = $lat;
    $this->lng = $lng;
    // ... additional properties here ...
}

在这种情况下,您必须在使用创建对象时提供值new Address(/* parameters go here */);

于 2018-02-13T18:30:42.753 回答
-1

Symfony 5:

public function getSomething(): ?string
    {
        return $this->something;
    }

    public function setSomething(string $something): self
    {
        $this->something= $something;

        return $this;
    }

只需像这样从 (string $something) 中删除字符串,它应该可以工作,它对我有用

public function getSomething(): ?string
        {
            return $this->something;
        }
    
        public function setSomething($something): self
        {
            $this->something= $something;
    
            return $this;
        }
于 2020-09-08T08:01:08.450 回答