symfony Easy admin 4 AssociateField原则($object)必须是对象类型,给定字符串

ccrfmcuu  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(149)

当创建一个简单的管理关联字段时,我得到以下错误

TypeError:
get_class(): Argument #1 ($object) must be of type object, string given

  at
vendor\doctrine\common\src\Util\ClassUtils.php:57
  at Doctrine\Common\Util\ClassUtils::getClass()

字符串
我有以下实体:

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\NewsRepository;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * News
 */
#[ORM\Table]
#[ORM\Entity(repositoryClass: NewsRepository::class)]
class News
{
    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private int $id;

    #[ORM\Column(name: 'title', type: 'string', length: 255)]
    #[Assert\NotBlank]
    private string $title;

    #[ORM\Column(name: 'url', type: 'string', length: 255)]
    #[Assert\NotBlank]
    #[Assert\Url]
    private string $url;

    #[ORM\Column(name: 'date', type: 'datetime')]
    private DateTime $date;

    #[ORM\Column(name: 'displayLocation', type: 'string', length: 255)]
    #[Assert\NotBlank]
    private string $displayLocation;

    #[ORM\ManyToOne(targetEntity: TypeDictionary::class)]
    #[ORM\JoinColumn(name: 'type_id', referencedColumnName: 'id')]
    private string $type;

    public function getId(): int
    {
        return $this->id;
    }

    public function setUrl(string $url): News
    {
        $this->url = $url;

        return $this;
    }

    public function getUrl(): string
    {
        return $this->url;
    }

    public function setDate(DateTime $date): News
    {
        $this->date = $date;

        return $this;
    }

    public function getDate(): DateTime
    {
        return $this->date;
    }

    public function setTitle(string $title): News
    {
        $this->title = $title;

        return $this;
    }

    public function getTitle(): string
    {
        return $this->title;
    }

    public function setType(string $type): News
    {
        $this->type = $type;

        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function setDisplayLocation(string $displayLocation): News
    {
        $this->displayLocation = $displayLocation;

        return $this;
    }

    public function getDisplayLocation(): string
    {
        return $this->displayLocation;
    }
}
<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * TypeDictionary
 */
#[ORM\Table]
#[ORM\Entity]
class TypeDictionary
{
    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private int $id;

    #[ORM\Column(name: 'name', type: 'string', length: 255)]
    #[Assert\NotBlank]
    private string $name;

    public function getId(): int
    {
        return $this->id;
    }

    public function setName(string $name): TypeDictionary
    {
        $this->name = $name;

        return $this;
    }

    public function getName(): string
    {
        return $this->name;
    }

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

的数据
和下面的NewsCrudController

<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Entity\News;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class NewsCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return News::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('type'),
            TextField::new('title'),
            TextField::new('url'),
            DateField::new('date'),
            TextField::new('displayLocation'),
        ];
    }
}


如果省略configureFields方法,则不包括type字段,其余字段都可以工作。
我得到以下错误:
The Doctrine type of the "type" field is "2", which is not supported by EasyAdmin. For Doctrine's Custom Mapping Types have a look at EasyAdmin's field docs.

tcomlyy6

tcomlyy61#

我回答了我自己的问题。在新闻实体中,
private string locationprivate string $type,它们必须是实体本身
private LocationDictionary $location等。
两者的getter和setter都需要更改,现在使用string,但使用实体。

相关问题