【问题标题】:Value must be type of string, null returned值必须是字符串类型,返回null
【发布时间】:2018-07-24 02:11:07
【问题描述】:

拥有以下实体:

ID; } /** * @return 字符串 */ 公共函数 getLat(): 字符串 { 返回 $this->lat; } /** * @param 字符串 $lat */ 公共函数 setLat(string $lat): 无效 { $this->lat = $lat; } /** * @return 字符串 */ 公共函数getLng():字符串 { 返回 $this->lng; } /** * @param 字符串 $lng */ 公共函数 setLng(字符串 $lng):无效 { $this->lng = $lng; } /** * @return 字符串 */ 公共函数 getStreet(): 字符串 { 返回 $this->street; } /** * @param 字符串 $street */ 公共函数 setStreet(string $street): 无效 { $this->street = $street; } /** * @return 字符串 */ 公共函数 getZipcode(): 字符串 { 返回 $this-> 邮政编码; } /** * @param 字符串 $zipcode */ 公共函数 setZipcode(string $zipcode): 无效 { $this->zipcode = $zipcode; } /** * @return 字符串 */ 公共函数getCity():字符串 { 返回 $this->city; } /** * @param 字符串 $city */ 公共函数 setCity(string $city): 无效 { $this->city = $city; } /** * @return 字符串 */ 公共函数getDescription():字符串 { 返回 $this-> 描述; } /** * @param 字符串 $description */ 公共函数 setDescription(string $description): 无效 { $this->description = $description; } /** * @var 字符串 * * @ORM\Column(name="lat", type="decimal", precision=10, scale=8, nullable=false) */ 私人 $lat; /** * @var 字符串 * * @ORM\Column(name="lng", type="decimal", precision=10, scale=8, nullable=false) */ 私人 $lng; /** * @var 字符串 * * @ORM\Column(name="street", type="string", length=255, nullable=false) */ 私人$街; /** * @var 字符串 * * @ORM\Column(name="zipcode", type="string", length=5, nullable=false) */ 私人 $zipcode; /** * @var 字符串 * * @ORM\Column(name="city", type="string", length=255, nullable=false) */ 私人$城市; /** * @var 字符串 * * @ORM\Column(name="description", type="text", nullable=false) */ 私人$描述; /** * @var 整数 * * @ORM\Column(name="id", type="integer") * @ORM\ID * @ORM\GeneratedValue(strategy="IDENTITY") */ 私人 $id; } 有一行数据的表: [![在此处输入图像描述][1]][1] [![在此处输入图像描述][2]][2] 我收到了这个错误: >> "类型": "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

【问题讨论】:

    标签: symfony api-platform.com


    【解决方案1】:

    getter getLat 具有返回类型提示 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"
    

    关于教义的注意事项:

    如果数据库中的值设置正确,则在 Doctrine 填充实体后(例如通过$address = $addressRepo->find(123);),您将不会遇到此问题。只有当您自己创建一个新实体然后尝试使用 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 */); 创建对象时提供值。

    【讨论】:

      【解决方案2】:

      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;
              }
      

      【讨论】:

        猜你喜欢
        • 2021-07-29
        • 2017-04-22
        • 2022-12-03
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-15
        相关资源
        最近更新 更多