2
votes

I have Silex setup with Doctrine2 ORM. I am trying to build a pagination class that I can use with my entities. I am well aware of the existing pagination classes that exist within Doctrine2 but because this project is for my school research I am trying to create this component myself.

Below is the fatal error I get when accessing this page:

Fatal error: Class 'PlayGround\Model\Helper\UserRepository' not found in D:\web\playground-solutions\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php on line 689

I have defined an interface called PaginateableInterface with two methods count and paginate. I went on to define a custom EntityRepository class that extends Doctrine\ORM\EntityRepository. Below is my custom EntityRepository.

<?php

 namespace PlayGround\Service\Doctrine;

 use Doctrine\ORM\EntityRepository as ParentEntityRepository;

 class EntityRepository extends ParentEntityRepository{

   public function count(){

      $em       = $this->getEntityManager();

      $builder  = $em->createQueryBuilder();
      /**
      * ToDo: @entity
      *
      * Still need to find a better way of getting entity class name.
      */
      $entity   = $em->getClassMetadata(get_class(__CLASS__))->getName();

      //Dynamically get a count of records on any entity we happen to call this on.
      $builder->select($builder->expr()->count('e'))
       ->from($entity, 'e');

      $query   = $builder->getQuery();

      //Try-Catch block ommitted
      return $query->getSingleScalarResult();
    }
  }

 <?php

   namespace PlayGround\Model\Helper;

   use PlayGround\Service\Doctrine\EntityRepository as CustomRepository;
   use PlayGround\Contract\PaginateableInterface as IPaginate;

   class UserRepository extends CustomRepository  implements IPaginate
   {
    }

In my understanding this should suffice as the count and paginate methods are sitting within the custom repository.

Inside my Paginator class I call the entity I want to paginate as shown below:

<?php

  //Paginator class
  $model = $this->getModel($model);
  //Count should be inherited from CustomRepository aliased object.
  $totalRecords = $model->count(); 

Below is another pierce of meet with regards to this where I add an annotation to my model to point it to the repository class it is suppose to use.

<?php
  namespace Application\Model\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Application\Model\Entity\UserGroup;


 /**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Entity(repositoryClass="PlayGround\Model\Helper\UserRepository")
 */
 class User{ /* Rest of the code goes here... */ }

Given all this setup what could I have missed in getting this to work? I have even ran two commands on my doctrine console but that didn't help either.

Luyanda.Siko@ZACT-PC301 MINGW64 /d/web/playground-solutions
$ php app/Console/bin/doctrine.php orm:clear-cache:metadata
  Clearing ALL Metadata cache entries
  Successfully deleted cache entries.

Luyanda.Siko@ZACT-PC301 MINGW64 /d/web/playground-solutions
$ php app/Console/bin/doctrine.php orm:clear-cache:query
  Clearing ALL Query cache entries
  Successfully deleted cache entries.

EDIT:

Below is my file structure found in D:\web\playground-solutions.

enter image description here

3
What are the paths you use to store the classes in?xabbuh

3 Answers

4
votes

You declare twice @ORM\Entity. Once with the repositoryClass and once without. Remove the one without:

@ORM\Entity

and leave this:

@ORM\Entity(repositoryClass="PlayGround\Model\Helper\UserRepository")

@ORM\HasLifecycleCallbacks should be declared without parentheses ()...

Also make sure that the EntityRepository is in the correct namespace and the corresponding folder:

your namespace is PlayGround\Model\Helper\UserRepository meaning the file should be in folder PlayGround\Model\Helper and the class file name should be UserRepository.php.

Fix and check that and if it still doesn't work leave a comment.

UPDATE:

Your UserRepository is in the wrong module. Is now in app should be in PlayGround

1
votes

The file should be in:

src/PlayGround/Model/Helper/UserRepository.php

It's all about that problem.

0
votes

Clearly this has really consumed my thought process. All I was doing was pointing to an incorrect namespace as pointed out by @Witt.

I changed my annotation entry in the User entity and the error went away.

 <?php
   /** @ORM\Entity(repositoryClass="Application\Model\Helper\UserRepository") */

Thanks you guys.