Creating a Repository in WordPress #
With the nnhelpers
plugin, you can create custom Repositories to retrieve WP-Post-data that is automatically converted into Models.
Your repository must extend the Nng\Nnhelpers\Domain\Repository\CPT\AbstractRepository
Please refer to this page to find out, how to define the Model that the WordPress-Post-Data will automatically be mapped to.
<?php
namespace My\Extension\Domain\Repository\CPT;
use Nng\Nnhelpers\Domain\Repository\CPT\AbstractRepository;
class EntryRepository extends AbstractRepository
{
// your methods
}
Inherited methods from the AbstractRepository #
Every Repository that extends the AbstractRepository from nnhelpers
will automatically have the following methods:
method | description |
---|---|
->findAll() |
Get ALL entries for the current Post-Type |
->findByUid( $ID ) |
Find a single WP-Post by a given ID |
->findByPostTitle( $title) |
Find Posts by a certain title |
->findByAnyField( ... ) |
Same works for any field of the tables posts |
->createQuery() |
Creates a QueryBuilder-instance for more complicated Queries to the database. See QueryBuilder docs for examples |
Example from the Controller Context #
You can always use \nn\wp::injectClass( $className )
to create an instance of your Repository and then call any of the methods defined in your Repository or the AbstractRepository.
<?php
namespace My\Extension\Controller\Elementor;
use Nng\Nnhelpers\Controller\AbstractElementorController;
class ListController extends AbstractElementorController
{
/**
* @return void
*/
public function initializeView( $view = null )
{
$entryRepository = \nn\wp::injectClass( \My\Extension\Domain\Repository\CPT\EntryRepository::class );
$entries = $entryRepository->findAll();
$this->view->assignMultiple([
'entries' => $entries
]);
}
}