简体   繁体   English

我应该如何注册Sonata应用程序的新管理员类?

[英]How should I register my Sonata application's new admin class?

I am using the Sonata admin bundle to build a backend for a blog. 我正在使用Sonata管理员捆绑包为博客构建后端。 I have created a new Post entity and used sonata:admin:generate to generate an admin class called PostAdmin. 我创建了一个新的Post实体,并使用sonata:admin:generate生成了一个名为PostAdmin的管理类。 This admin class extends AbstractAdmin. 该管理类扩展了AbstractAdmin。 So far so good. 到现在为止还挺好。

In accordance with https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html , I add the following code to my class: 根据https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html ,我将以下代码添加到类中:

public function validate(ErrorElement $errorElement, $object)
{

    die('At least the validate() method is being called.'); 

    $errorElement
        ->with('author')
        ->assertNotBlank()
        ->assertNotNull()
        ->end();

    parent::validate($errorElement, $object); // TODO: Change the autogenerated stub
}

... but my die() statement does not appear to get called. ...但是我的die()语句似乎没有被调用。 (Also, when I remove the die() call, the assertions appear to get ignored, as I can leave my "author" field blank and still save a record.) (此外,当我删除die()调用时,这些断言似乎被忽略了,因为我可以将“作者”字段保留为空白,并仍然保存记录。)

==== ====

UPDATE #1: Per https://symfony.com/doc/3.x/bundles/SonataAdminBundle/reference/conditional_validation.html , I tried throwing an exception instead of dying. 更新#1:根据https://symfony.com/doc/3.x/bundles/SonataAdminBundle/reference/conditional_validation.html ,我尝试抛出异常而不是死亡。 Even with this better debugging technique, it appears that the method is not getting called. 即使使用这种更好的调试技术,也似乎没有调用该方法。

UPDATE #2: It looks like none of the methods in my PostAdmin class are being called at all. 更新#2:看来我的PostAdmin类中的所有方法都没有被调用。 Is there a place I need to register that PostAdmin class in order for its methods to be called? 是否需要注册该PostAdmin类以便调用其方法的地方?

I found my answer. 我找到了答案。 It's embarrassing. 这让人很难堪。

There was an old file called PostAdminOld.php that contained a duplicate of my PostAdmin class. 有一个名为PostAdminOld.php的旧文件,其中包含我的PostAdmin类的副本。 Running make install on my project in a fit of desperation, I saw the following message that helped me pin down the problem: 绝望地在我的项目上运行make install ,我看到以下消息可以帮助我解决问题:

Warning: Ambiguous class resolution, "AppBundle\Admin\PostAdmin" was found in both "/usr/src/app/src/AppBundle/Admin/PostAdminOld.php" and "/usr/src/app/src/AppBundle/Admin/PostAdmin.php", the first will be used.
Warning: Ambiguous class resolution, "AppBundle\Controller\PostAdminController" was found in both "/usr/src/app/src/AppBundle/Controller/PostAdminControllerOld.php" and "/usr/src/app/src/AppBundle/Controller/PostAdminController.php", the first will be used.

... and that was that. ...就是那样。 Removing the old admin class definition allowed me to see my more recent changes. 删除旧的admin类定义后,我可以查看最新的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM