简体   繁体   中英

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. I have created a new Post entity and used sonata:admin:generate to generate an admin class called PostAdmin. This admin class extends 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:

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. (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.)

====

UPDATE #1: Per https://symfony.com/doc/3.x/bundles/SonataAdminBundle/reference/conditional_validation.html , I tried throwing an exception instead of dying. 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. Is there a place I need to register that PostAdmin class in order for its methods to be called?

I found my answer. It's embarrassing.

There was an old file called PostAdminOld.php that contained a duplicate of my PostAdmin class. Running make install on my project in a fit of desperation, I saw the following message that helped me pin down the problem:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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