Symfony2 Sonata: No Entity Manager in Custom Class

百般思念 提交于 2019-12-13 21:35:06

问题


I'm trying to create custom Form in Sonata-Admin and Im geting No entity manager defined for class School\ChildBirthBundle\Entity\DataChapter

My code:

namespace School\ChildBirthBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Knp\Menu\ItemInterface as MenuItemInterface;
use School\ChildBirthBundle\Entity\DataChapter;

class DataChapterAdmin extends Admin
{

protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
    ->add('name')
    ->add('status')
;
}



sonata.admin.data_chapter:
class: School\ChildBirthBundle\Admin\DataChapterAdmin
tags:
    - { name: sonata.admin, manager_type: orm, group: "Content", label: "Chapter" }
arguments: 
    - ~
    - School\ChildBirthBundle\Entity\DataChapter
    - ~
    - @doctrine.orm.default_entity_manager
calls:
    - [ setTranslationDomain, [SchoolChildBirthBundle]]

And entity looks like this

namespace School\ChildBirthBundle\Entity;
 use Doctrine\ORM\Mapping as ORM;
 /**
 * @ORM\Entity
 * @ORM\Table(name="data_chapter")
  */
  class DataChapter 
  {

   /**
   * @ORM\Column(type="integer")
    * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
    protected $id;

  /**
    * @ORM\Column(type="string", length=255)
    */
   protected $name;

   /**
   * @ORM\Column(type="integer", length=1)
    */
    protected $status;

    /**
    * @ORM\Column(type="integer")
     */
  protected $idSubject;
 }

I dont know what im doing wrong


回答1:


No entity manager defined for class School\ChildBirthBundle\Entity\DataChapter 500 Internal Server Error - RuntimeException

 in vendor/sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php at line 223  -

            $em = $this->registry->getManagerForClass($class);
            if (!$em) {
                throw new \RuntimeException(sprintf('No entity manager defined for class %s', $class));
            }
            $this->cache[$class] = $em;



回答2:


Maybe it's an issue in your admin service declaration, if you check the documentation there is no 4th argument :

https://sonata-project.org/bundles/admin/master/doc/reference/getting_started.html#step-3-create-an-admin-service

You should have :

sonata.admin.data_chapter:
    class: School\ChildBirthBundle\Admin\DataChapterAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: "Content", label: "Chapter" }
    arguments: 
        - ~
        - School\ChildBirthBundle\Entity\DataChapter
        - ~
    calls:
        - [ setTranslationDomain, [SchoolChildBirthBundle]]


来源:https://stackoverflow.com/questions/32859478/symfony2-sonata-no-entity-manager-in-custom-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!