Symfony,无法链式调用getter方法

huangapple go评论61阅读模式
英文:

Symfony, can't chain getter methods

问题

Symfony 6
Doctrine 3

Titelmeta Entity Class

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use DateTime;

/**
 * Titelmeta
 *
 * @ORM\Entity
 */
class Titelmeta
{
    

    /**
     * @var \Isbn
     *
     * @ORM\Column(name="ISBN_mobi", type="bigint", nullable=false, options={"unsigned"=true})
     * @ORM\ManyToOne(targetEntity="Isbn")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ISBN_mobi", referencedColumnName="isbn")
     * })
     */
    private $isbnMobi;

    

    public function getIsbnMobi(): ?Isbn
    {
        return $this->isbnMobi;
    }
}

Isbn Entity Class

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
 * Isbn
 *
 * @ORM\Table(name="isbn", uniqueConstraints={@ORM\UniqueConstraint(name="isbn", columns={"isbn"})})
 * @ORM\Entity
 */
class Isbn
{
    /**
     * @var int
     *
     * @ORM\Column(name="isbn", type="bigint", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $isbn;

    

    public function __toString()
    {
        return (string) $this->getIsbn();
    }

    public function getIsbn(): ?string
    {
        return $this->isbn;
    }

    
}

I have a result object named $record

dump($record);

> App\Entity\Titelmeta {#901 ▼
   -isbnMobi: "97812345678"
}
dump($record->getIsbnMobi()->getIsbn());

> App\Entity\Titelmeta::getIsbnMobi(): Return value must be of type
> ?App\Entity\Isbn, string returned

Symfony,无法链式调用getter方法

I don't get it. Can someone please help.

英文:

Symfony 6
Doctrine 3

Titelmeta Entity Class

<?php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use DateTime;

/**
 * Titelmeta
 *
  
  …

 * @ORM\Entity
 */
class Titelmeta
{
    …
	
    /**
     * @var \Isbn
     *
     * @ORM\Column(name="ISBN_mobi", type="bigint", nullable=false, options={"unsigned"=true})
     * @ORM\ManyToOne(targetEntity="Isbn")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ISBN_mobi", referencedColumnName="isbn")
     * })
     */
    private $isbnMobi;

	…

    public function getIsbnMobi(): ?Isbn
    {
        return $this->isbnMobi;
    }


}

Isbn Entity Class

<?php

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
 * Isbn
 *
 * @ORM\Table(name="isbn", uniqueConstraints={@ORM\UniqueConstraint(name="isbn", columns={"isbn"})})
 * @ORM\Entity
 */
class Isbn
{
    /**
     * @var int
     *
     * @ORM\Column(name="isbn", type="bigint", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $isbn;

	…

    public function __toString()
    {
        return (string) $this->getIsbn();
    }

    public function getIsbn(): ?string
    {
        return $this->isbn;
    }

	…

}

I have an result object named $record

dump($record);

> App\Entity\Titelmeta {#901 ▼
-isbnMobi: "97812345678"
}

dump($record->getIsbnMobi()->getIsbn());

> App\Entity\Titelmeta::getIsbnMobi(): Return value must be of type
> ?App\Entity\Isbn, string returned

Symfony,无法链式调用getter方法

I don't get it. Can someone please help.

答案1

得分: 1

isbnMobi 不应该是一个数字。在ORM中,如果对象之间有关系,它应该始终是null、一个实际对象、对象的代理,或者在存在多个对象的情况下是一个(代理)集合。由于您的isbnMobi不是一个对象而是一个数字,这意味着您的ORM配置错误。

isbnMobi属性上,Titlemeta实体不应该有@ORM\Column。该信息通过ManyToOneJoinColumn处理。移除它,然后应该没问题。

Column的属性大多数情况下可以分配给JoinColumn。默认情况下,数据库列类型必须(自动)与引用的数据库列类型匹配,其中nullable是一个值得注意的例外。

另外,同一属性上的@var应该只是Isbn而不是\Isbn,因为前导的\表示它在根命名空间中。

如果您使用的是php 8,则应该使用属性而不是注解,并将类型提示添加到属性上,而不是使用@var(在技术上不是绑定)。

英文:

The isbnMobi should NOT be a number. In an ORM if you have relations between objects, it should always be null, an actual object, a proxy of an object or in case of multiple objects, a (proxy) collection. Since your isbnMobi is not an object but a number, this implies your ORM was misconfigured ...

The Titlemeta entity should NOT have @ORM\Column on the isbnMobi property. That information is handled via ManyToOne's JoinColumn. Remove it and you should be fine.

The Columns attributes can mostly be assigned to the JoinColumn instead. By default, the database column type must (automatically) match the referenced database column type, with nullable being the notable exception.

Also, the @var on that same property should be just Isbn not \Isbn, since the leading \ would mean it's in the root namespace.

If you're using php 8, use attributes instead of annotations and add the typehints to the property instead of @var (not technically binding).

huangapple
  • 本文由 发表于 2023年6月9日 02:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定