【问题标题】:How to set a default value in AssociationField EasyAdmin 3如何在 AssociationField EasyAdmin 3 中设置默认值
【发布时间】:2021-10-01 18:37:55
【问题描述】:

我从 EasyAdmin v3 开始。 我想在 easyAdmin 中设置一个默认值。 在 php/symfony 中我会提供:

$article = new Article::class;
$article->setAuthor($user)

在创建表单以在我的实体中设置 currentUser 但使用 EA3 之前,我不知道如何管理它。

谢谢

【问题讨论】:

    标签: php symfony easyadmin3


    【解决方案1】:

    您可以覆盖 createEntity() 之类的方法:

    class ArticleCrudController extends AbstractCrudController
    {
        public static function getEntityFqcn(): string
        {
            return Article::class;
        }
    
        public function createEntity(string $entityFqcn)
        {
            $article = new Article();
            $article->setAuthor($this->getUser())
    
            return $article;
        }
    
        // ...
    }
    

    更多信息请参见this part of the documentation

    【讨论】:

    • 不客气!只需阅读文档,我就在几分钟内找到了这一点。我没有使用 EasyAdmin 的经验。
    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 2010-11-14
    • 2021-09-22
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多