Zum Inhalt springen
nnhelpers for WordPress

nnhelpers for WordPress

A toolbox-plugin with a huge collection of one-liners to drastically speed up your plugin-development

About

  • What is nnhelpers?

Getting started

  • Installation
  • Creating a plugin

Custom Post Types (CPT)

  • Creating a Custom Post Type (CPT)
  • Adding forms and fields to your CPT
  • Adding forms and fields to WordPress posts
  • CPT – field types and configurations

Custom Widgets and Blocks

  • Creating a custom widget for Elementor

Basic Concepts

  • QueryBuilder
  • Model
  • Repository
  • Fluid Templates
  • Creating custom ViewHelpers
  • Home
  • Docs
  • Basic Concepts
  • Repository

Repository

Table of Contents
  • Creating a Repository in WordPress
    • Inherited methods from the AbstractRepository
    • Example from the Controller Context
Table of Content

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
        ]);
    }
}
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

Updated on 2. Juli 2022
ModelFluid Templates

Powered by BetterDocs

Table of Contents
  • Creating a Repository in WordPress
    • Inherited methods from the AbstractRepository
    • Example from the Controller Context
nnhelpers for WordPress
Mit Stolz präsentiert von WordPress.