[英]How should I register my Sonata application's new admin class?
我正在使用Sonata管理員捆綁包為博客構建后端。 我創建了一個新的Post實體,並使用sonata:admin:generate
生成了一個名為PostAdmin的管理類。 該管理類擴展了AbstractAdmin。 到現在為止還挺好。
根據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
}
...但是我的die()語句似乎沒有被調用。 (此外,當我刪除die()調用時,這些斷言似乎被忽略了,因為我可以將“作者”字段保留為空白,並仍然保存記錄。)
====
更新#1:根據https://symfony.com/doc/3.x/bundles/SonataAdminBundle/reference/conditional_validation.html ,我嘗試拋出異常而不是死亡。 即使使用這種更好的調試技術,也似乎沒有調用該方法。
更新#2:看來我的PostAdmin類中的所有方法都沒有被調用。 是否需要注冊該PostAdmin類以便調用其方法的地方?
我找到了答案。 這讓人很難堪。
有一個名為PostAdminOld.php的舊文件,其中包含我的PostAdmin類的副本。 絕望地在我的項目上運行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.
...就是那樣。 刪除舊的admin類定義后,我可以查看最新的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.