如何在Symfony 5 EasyAdmin 3中显示与其详细信息页面的链接的关联对象?

发布于 2025-02-06 10:43:41 字数 1622 浏览 4 评论 0原文

我有两个实体,即要约和候选人,它们之间有一定的关系。相关代码零件是以下内容:

invor.php:

    /**
     * @ORM\OneToMany(targetEntity=Candidate::class, mappedBy="offer", orphanRemoval=true)
     */
    private $candidates;

    public function __construct()
    {
        $this->candidates = new ArrayCollection();
    }

in cantifate.php:

    /**
     * @ORM\ManyToOne(targetEntity=Offer::class, inversedBy="candidates")
     * @ORM\JoinColumn(nullable=false)
     */
    private $offer;

in fivecrudcontroller.php:

    public function configureFields(string $pageName): iterable
    {
        return [
            ArrayField::new('candidates', new TranslatableMessage('easyadmin.candidates'))
                ->onlyOnDetail(),
            AssociationField::new('candidates', new TranslatableMessage('easyadmin.candidates'))
                ->onlyOnIndex()
        ];
    }

in candidatecrudcontroller.php:

    public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('offer', new TranslatableMessage('easyadmin.candidate.offer'))
        ];
    }

我的问题是,在候选人的情况下,EasyAdmin显示了每个候选人链接到其详细信息页面的报价, ,对于报价,它仅显示候选人的不可添加的字符串表示形式,如图像所示:

“不可识别的候选人”

也可以通过适当的链接显示候选人的适当链接他们的详细信息页面?

I have two entities, Offer and Candidate, with a OneToMany relationship between them. The relevant code parts are the followings:

In Offer.php:

    /**
     * @ORM\OneToMany(targetEntity=Candidate::class, mappedBy="offer", orphanRemoval=true)
     */
    private $candidates;

    public function __construct()
    {
        $this->candidates = new ArrayCollection();
    }

In Candidate.php:

    /**
     * @ORM\ManyToOne(targetEntity=Offer::class, inversedBy="candidates")
     * @ORM\JoinColumn(nullable=false)
     */
    private $offer;

In OfferCrudController.php:

    public function configureFields(string $pageName): iterable
    {
        return [
            ArrayField::new('candidates', new TranslatableMessage('easyadmin.candidates'))
                ->onlyOnDetail(),
            AssociationField::new('candidates', new TranslatableMessage('easyadmin.candidates'))
                ->onlyOnIndex()
        ];
    }

In CandidateCrudController.php:

    public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('offer', new TranslatableMessage('easyadmin.candidate.offer'))
        ];
    }

My problem is that while in the case of Candidates EasyAdmin displays the Offer linked to its details page for each Candidate, for the Offers it displays only the non-clickable string representations of the Candidates, as the images show it:

clickable Offers

non-clickable Candidates

Is it possible to display the Candidates too with their appropriate links to their details page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2025-02-13 10:43:41

是的,这是可能的,但是您应该为此创建一个自定义模板。
假设您创建了/templates/admin/field/ffer/detail/candidates.html.twig。然后,在中,要gresscrudcontroller需要为候选人字段*:

public function configureFields(string $pageName): iterable
{
    // ...
    yield AssociationField::new('candidates', 'Candidates')->onlyOnDetail()
            ->setTemplatePath('admin/field/offer/detail/candidates.html.twig');
}
  • 使用Generator示例创建的模板,但是可以像您的示例一样将其写成阵列项目,例如在您的示例中(而不是Arrayfield)。在这种情况下,我只想使用发电机,因为配置更方便。

在模板中,仅生成每个实体的URL:

# /templates/admin/field/offer/detail/candidates.html.twig
{% if field.value is not empty %}
    {% for candidate in field.value %}
        {% set candidateDetailUrl = ea_url()
            .setController('App\\Controller\\Admin\\CandidateCrudController')
            .setAction(constant('EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Action::DETAIL'))
            .setEntityId(candidate.id)
        %}
        <a href="{{ candidateDetailUrl }}" style="display: block">
            {{ candidate.name }}
        </a>
    {% endfor %}
{% else %}
    No candidates
{% endif %}

Yes, it's possible, but you should create a custom template for this.
Let's say, you've created /templates/admin/field/offer/detail/candidates.html.twig. Then in OfferCrudController need to set created template for candidates field*:

public function configureFields(string $pageName): iterable
{
    // ...
    yield AssociationField::new('candidates', 'Candidates')->onlyOnDetail()
            ->setTemplatePath('admin/field/offer/detail/candidates.html.twig');
}
  • example with generator, but it can be easily written as array item, like in your example (instead ArrayField). I just prefer to use generator in that case, because that's more convenient to configure.

And in template just generate URL for each entity:

# /templates/admin/field/offer/detail/candidates.html.twig
{% if field.value is not empty %}
    {% for candidate in field.value %}
        {% set candidateDetailUrl = ea_url()
            .setController('App\\Controller\\Admin\\CandidateCrudController')
            .setAction(constant('EasyCorp\\Bundle\\EasyAdminBundle\\Config\\Action::DETAIL'))
            .setEntityId(candidate.id)
        %}
        <a href="{{ candidateDetailUrl }}" style="display: block">
            {{ candidate.name }}
        </a>
    {% endfor %}
{% else %}
    No candidates
{% endif %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文