2015-11-04 44 views
2

我從幾個月起就開始學習,並且無法解決此問題。致命錯誤:調用未定義的方法MyModule Entity MyEntity :: findAll()doctrine orm 2 zf2

以及細節

composser.json

"name" : "zendframework/skeleton-application", 
    "description" : "Skeleton Application for ZF2", 
    "require" : { 
    "php" : ">=5.3.3", 
    "zendframework/zendframework" : "~2.4", 
    "doctrine/doctrine-orm-module" : "0.9.2", 
    "doctrine/doctrine-module" : "0.9.0", 
    "zendframework/zend-developer-tools" : "0.0.2", 
    "bjyoungblood/BjyProfiler" : "v1.1.0" 
    }, 
    "license" : "BSD-3-Clause", 
    "keywords" : [ "framework", "zf2" ], 
    "homepage" : "http://framework.zend.com/" 
} 

application.config.php

<?php 
return array(
    'modules' => array(
     'ZendDeveloperTools', 
     'DoctrineModule', 
     'DoctrineORMModule', 
     'BjyProfiler', 
     'Application', 
     'Publicacion', 
    ), 
     'module_paths' => array(
      './module', 
      './vendor','./module','./module', 
     ), 
     'config_glob_paths' => array(
      'config/autoload/{,*.}{global,local}.php', 
     ), 
    ), 
); 

module.config.php

namespace Publicacion; 
return array(
    'controllers' => array(
     'invokables' => array(
      'Publicacion\Controller\Publicacion' => 'Publicacion\Controller\PublicacionController', 
      'Publicacion\Controller\Categoria' => 'Publicacion\Controller\CategoriaController', 
     ), 
    ), 
    'router' => array(
     'routes' => array(
      'publicacion' => array(
       'type' => 'Literal', 
       'options' => array(
        // Change this to something specific to your module 
        'route' => '/publicacion', 
        'defaults' => array(
         // Change this value to reflect the namespace in which 
         // the controllers for your module are found 
         '__NAMESPACE__' => 'Publicacion\Controller', 
         'controller' => 'Publicacion', 
         'action'  => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        // This route is a sane default when developing a module; 
        // as you solidify the routes for your module, however, 
        // you may want to remove it and replace it with more 
        // specific routes. 
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '/[:controller[/:action]]', 
          'constraints' => array(
           'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
           'action'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
          ), 
          'defaults' => array(
          ),),), 

        'categoria' => array(
         'type' => 'segment', 
         'options' => array(
          // Change this to something specific to your module 
          'route' => '/categoria', 
          'defaults' => array(
           // Change this value to reflect the namespace in which 
           // the controllers for your module are found 
           '__NAMESPACE__' => 'Publicacion\Controller', 
           'controller' => 'Categoria', 
           'action'  => 'index', 
          ),),),),),),), 

    'view_manager' => array(
     'template_path_stack' => array(
      'Publicacion' => __DIR__ . '/../view', 
     ), 
     'display_exceptions' => true, 
    ), 

    'doctrine' => array(
     'driver' => array(
      __NAMESPACE__ . '_driver' => array(
       'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
       'cache' => 'array', 
       'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity') 
      ), 
      'orm_default' => array(
       'drivers' => array(
        __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
       ))))); 

控制器

namespace Publicacion\Controller; 
use Zend\Mvc\Controller\AbstractActionController; 
use Publicacion\Entity\Publicacion; 
use Doctrine\ORM\EntityManager; 

class PublicacionController extends AbstractActionController 
{ 
    public function getEntityManager() 
    { 
     return $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); 
    } 
    public function indexAction() 
    { 
     //var_dump($this->getEntityManager()); 
     $publicaciones = $this->getEntityManager()->getRepository('Publicacion\Entity 
\Publicacion')->findAll(); // <-- HERE 
     return array('publicaciones' => $publicaciones); 
    } 
} 

index.phtml

<table> 
<?php 
foreach ($publicaciones as $publicacion){ 
    echo "<tr>"; 
    echo "<td>".$publicacion->getId()."</td>"; 
    echo "<td>".$publicacion->getTitulo()."</td>"; 
    echo "</tr>"; 
} 
?> 
</table> 

文字錯誤是:

Fatal error: Call to undefined method Publicacion\Entity\Publicacion::findAll() in C:\xampp\htdocs\MyProyect\module\Publicacion\src\Publicacion\Controller\PublicacionController.php on line 29

的Publicacion實體

namespace Publicacion\Entity; 

use Publicacion\Entity\Categoria; 
use Publicacion\Entity\Referencia; 
use Doctrine\ORM\Mapping as ORM; 
use Zend\Form\Annotation; 
use Doctrine\Common\Collections\Collection; 
use Zend\Db\Sql\Ddl\Column\Boolean; 

/** 
* 
* @author Darwin 
* 
* Publicacion 
* 
* @ORM\Table(name="publicacion") 
* @ORM\Entity(repositoryClass="Publicacion\Entity\Publicacion") 
* @Annotation\Name("Publicacion") 
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods") 
*/ 

class Publicacion 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer") 
    * @Annotation\Exclude() 
    * @var int|null 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string") 
    * 
    * @var string 
    */ 

    private $titulo; 

    /** 
    * @ORM\Column(type="string") 
    * @var string 
    */ 

    private $introduccion; 

    /** 
    * @ORM\Column(type="string") 
    * @var string 
    */ 

    private $contenido; 


    // relación muchos a muchos una referencia puede referenciar a muchas publicaciones y viserversa 

    /** 
    * @ORM\ManyToMany(targetEntity="Referencia", inversedBy="publicaciones") 
    * @ORM\JoinTable(name="publicacion_referencia") 
    * @var Collection 
    */ 

    private $referencias; 

    // relación muchos a muchos, una categoria puede referenciar a muchas publicaciones y viserversa 

    /** 
    * @ORM\ManyToMany(targetEntity="Categoria", inversedBy="publicaciones") 
    * @ORM\JoinTable(name="publicacion_categoria") 
    * @var Collection 
    */ 

    private $categorias; 

    //****** MODULO COMENTARIO ********** 
    // relación uno a muchos (una publicación puede referenciar muchos comentarios/un comentario solo puede referenciar una publicación o otro comentario) 
    /** 
    *-ORM\OneToMany(targetEntity="Comentario", mappedBy="publicacion") 
    *-var Collection 
    */ 

    //private $comentarios; 

    /** 
    * @ORM\Column(type="datetime") 
    * @var \DateTime 
    */ 

    private $fechaCreado; 

    /** 
    * @ORM\Column(type="datetime") 
    * @var \DateTime 
    */ 
    private $fechaModificado; 

    /** 
    * @ORM\Column(type="boolean") 
    * @var Boolean 
    */ 

    private $publicado; 

// property methods .... 
} 

?> 

任何人都可以幫忙嗎?

+1

請包括您的實體。特別是EntityRepository註釋。 –

+0

包括Publicacion實體,我想我正在理解問題... –

+0

問題是註釋* @ORM \ Entity(repositoryClass =「Publicacion \ Entity \ Publicacion」),我沒有使用自定義存儲庫,並指出與回購相同的實體...現在正常工作,更改註釋以便* @ORM \ Entity。非常感謝您的幫助! –

回答

0

布拉姆Gerritsen的,與您的意見,我可以看到錯誤,問題是註釋

* @ORM\Entity(repositoryClass="Publicacion\Entity\Publicacion") 

,我沒有使用自定義的存儲庫,並表示相同的實體回購...現在工作正常,更改註釋以便

* @ORM\Entity 

非常感謝您的幫助!

+0

很高興我能幫你解決問題。 –

相關問題