2017-12-15 21 views
0
에() 데이터베이스에

시험 후 데이터를 유지하고 오류 :전화가 null

Uncaught Error: Call to a member function persist() on null in

public function addNewPostAction() 
    { 
     // Create new Post entity.. 
    // $entityManager = $container->get('doctrine.entitymanager.orm_default'); 
     $post = new Post(); 
     $post->setTitle('Top 10+ Books about Zend Framework 3'); 
     $post->setContent('Post body goes here'); 
     $post->setStatus(Post::STATUS_PUBLISHED); 
     $currentDate = date('Y-m-d H:i:s'); 
     $post->setDateCreated($currentDate); 
     $this->entityManager->persist($post); 
     $this->entityManager->flush(); 

    } 

UPDATE 얻을 : 오류 : 젠드 \ MVC \ 컨트롤러 \ PluginManager를을 :: 얻을 위의 두 샘플에서

public function addNewPostAction() 
{ 
    // Create new Post entity.. 
    // $entityManager = $container->get('doctrine.entitymanager.orm_default'); 
    $post = new Post(); 
    $post->setTitle('Top 10+ Books about Zend Framework 3'); 
    $post->setContent('Post body goes here'); 
    $post->setStatus(Post::STATUS_PUBLISHED); 
    $currentDate = date('Y-m-d H:i:s'); 
    $dm = $this->get('doctrine.odm.mongodb.document_manager'); 
    $dm->persist($post); 
    $dm->flush(); 
} 
+0

https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in- : 당신이 이름 myAwesomePlugin으로 플러그인을 정의하면, 당신은 당신의 행동에 액세스 할 수 있습니다 PHP/12769983 # 12769983 – Sammitch

답변

1

을 얻기 위해 가져 오거나 인스턴스를 만들 수 없습니다, 당신이 교리 '의 엔티티 관리자를 얻기 위해 노력하고있다 명백하다.

1 샘플 : 아마 컨트롤러의 재산 $ 위해 entityManager이 주석 코드 "$ 위해 entityManager = $ 컨테이너 -> GET ('doctrine.entitymanager.orm_default'에서,도 설정되어 있지

$this->entityManager 

); " 엔티티 관리자를 얻으려고하는 것은 명백합니다.

2 샘플 :

$this->get('doctrine.odm.mongodb.document_manager'); 

나는이 심포니 예로부터 가정. 어쨌든

이 컨트롤러의 교리 관리자를 얻으려면, 당신은 그것을 주입 인수로 받아 컨트롤러 생성자를 변경해야합니다 :

class IndexController extends AbstractActionController 
{ 
    private $doctrineManager; 

    public function __construct($doctrineManager) 
    { 
     $this->doctrineManager = $doctrineManager; 
    } 

다음에 컨트롤러 공장에 교리 관리자를 주입하여 module.config.php :

'controllers' => [ 
    'factories' => [ 
     Controller\IndexController::class => function ($container) { 

      return new Controller\IndexController(
       $container->get('doctrine.odm.mongodb.document_manager') 
      ); 
     }, 
     // ... 
    ], 
], 

사이드 참고 : 젠드가 정의되지 않은 m를 시도하기 때문에 "젠드 \ MVC \ 컨트롤러 \ PluginManager를이 :: 가져 오거나을 얻을에 대한 인스턴스를 생성 할 수 없습니다 얻을"발생하는 오류 그 (것)들을 플러그 접속 식, 예를 들면.

$this->myAwesomePlugin();