0
votes

I have an abstract class:

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *     "LegalInsuranceProof" = "LegalInsuranceProofDocument",
 *     "SalesReceipt" = "SalesReceiptDocument"
 * })
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Table(name="document_abstract")
 * @ORM\Entity(repositoryClass="App\Repository\DocumentRepository")
 */
abstract class AbstractDocument implements CreateFolderInterface
{
.
.
.
}

and the class, that extends this abstract class:

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Table(name="document_sales_receipt")
 */
class SalesReceiptDocument extends AbstractDocument
{
.
.
. 
}

In the repo, I have defined the method getReviewListPaginator:

class DocumentRepository extends ServiceEntityRepository {

    use PaginatorTrait;

    public function __construct(RegistryInterface $registry) {
        parent::__construct($registry, AbstractDocument::class);
    }

    public function getReviewListPaginator($limit, $offset) {
        $this->assertQueryParameters($offset, $limit, "asc");

        $qb = $this
            ->createQueryBuilder('d')
            ->select('PARTIAL d.{id, pageCount}')
            ->innerJoin('d.case', 'c')
            ->addSelect('PARTIAL c.{id}')
            ->setFirstResult($offset)
            ->setMaxResults($limit);

        return new Paginator(
            $qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY),
            true
        );
    }
}

If I do

$this->em->getRepository(AbstractDocument::class)->getReviewListPaginator(5,2);

the method getReviewListPaginator is called.

But If I do

$paginator = $this->em->getRepository(SalesReceiptDocument::class)->getReviewListPaginator(5,2);

I get en error message:

BadMethodCallException : Undefined method 'getReviewListPaginator'. The method name must start with either findBy, findOneBy or countBy!

But why? Should I define a repo for the SalesReceiptDocument entity, that extends the App\Repository\DocumentRepository?

2

2 Answers

1
votes

I don't think the Repository are extended by default.

I think you need to do a SalesReceiptReporsitory that explicitly exteands your DocumentRepository And add the repositoryClass options to your @Entity on SalesReceiptDocument.

1
votes

Your @Entity annotations do not have the repository specified, change them to:

@Entity(repositoryClass="..namespace..\DocumentRepository")

See the @Entity documentation.

Edit 1:
I just noticed your AbstractDocument has duplicate @Entity annotation, you can just delete the empty one

Edit 2:
To select different document types you need separate repositories, to keep your code simple and non-repeating, you can use the $_entityName attribute of EntityRepository if you are extending it or have your own private attribute that would indicate the entity name for a repository and then use this entity name in the getReviewListPaginator to dynamically query the type of entity you want.
As far as I can tell, you cannot achieve this without having separate repositories for each type of document - even if each is empty, just extending the base repository and doing the parametrized query building as I described.