Managing entity persistence (model, resource, collection)

With InstallSchema in place, we now have conditions for entity persistence. Our next step is to define model, resource, and collection classes for the Ticket entity.

The Ticket entity model class is defined under the app/code/Foggyline/Helpdesk/Model/Ticket.php file with content as follows:

<?php

namespace FoggylineHelpdeskModel;

class Ticket extends MagentoFrameworkModelAbstractModel
{
    const STATUS_OPENED = 1;
    const STATUS_CLOSED = 2;

    const SEVERITY_LOW = 1;
    const SEVERITY_MEDIUM = 2;
    const SEVERITY_HIGH = 3;

    protected static $statusesOptions = [
        self::STATUS_OPENED => 'Opened',
        self::STATUS_CLOSED => 'Closed',
    ];

    protected static $severitiesOptions = [
        self::SEVERITY_LOW => 'Low',
        self::SEVERITY_MEDIUM => 'Medium',
        self::SEVERITY_HIGH => 'High',
    ];

    /**
     * Initialize resource model
     * @return void
     */
    protected function _construct()
    {
        $this->_init('FoggylineHelpdeskModel ResourceModelTicket');
    }

    public static function getSeveritiesOptionArray()
    {
        return self::$severitiesOptions;
    }

    public function getStatusAsLabel()
    {
        return self::$statusesOptions[$this->getStatus()];
    }

    public function getSeverityAsLabel()
    {
        return self::$severitiesOptions[$this->getSeverity()];
    }
}

Reading the preceding code, we see it extends the MagentoFrameworkModelAbstractModel class, which further extends the MagentoFrameworkObject class. This brings a lot of extra methods into our Ticket model class, such as load, delete, save, toArray, toJson, toString, toXml, and so on.

The only actual requirement for us is to define the _construct method that, through the _init function call, specifies the resource class the model will be using when persisting data. We have set this value to FoggylineHelpdeskModelResourceModelTicket, which will be the next class we will define, the so-called resource class.

We have further defined several constants, STATUS_* and SEVERITY_*, as a sign of good programming practice and not to hardcode values that we will use across the code, which we can centralize into a class constant. These constants, in a way, map to our module requirements.

Additionally, we have three additional methods (getSeveritiesOptionArray, getStatusAsLabel, and getSeverityAsLabel) that we will use later on in our block class and template file.

The Ticket entity resource class is defined under app/code/Foggyline/Helpdesk/Model/ResourceModel/Ticket.php with content as follows:

<?php

namespace FoggylineHelpdeskModelResourceModel;

class Ticket extends MagentoFrameworkModelResourceModelDbAbstractDb
{
    /**
     * Initialize resource model
     * Get table name from config
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('foggyline_helpdesk_ticket', 'ticket_id');
    }
}

We can see the code extends the MagentoFrameworkModelResourceModelDbAbstractDb class, which further extends the MagentoFrameworkModelResourceModelAbstractResource class. This brings a lot of extra methods into our Ticket resource class, such as load, delete, save, commit, rollback, and so on.

The only actual requirement for us is to define the _construct method, through which we call the _init function that accepts two parameters. The first parameter of the _init function specifies the table name foggyline_helpdesk_ticket and the second parameter specifies identifying the ticket_id column within that table where we will be persisting data.

Finally, we define the Ticket entity collection class under app/code/Foggyline/Helpdesk/Model/ResourceModel/Ticket/Collection.php with content as follows:

<?php

namespace FoggylineHelpdeskModelResourceModelTicket;

class Collection extends MagentoFrameworkModel ResourceModelDbCollectionAbstractCollection
{
    /**
     * Constructor
     * Configures collection
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('FoggylineHelpdeskModelTicket', 'FoggylineHelpdeskModelResourceModelTicket');
    }
}

The collection class code extends the MagentoFrameworkModelResourceModelDbCollectionAbstractCollection class, which further extends the MagentoFrameworkDataCollectionAbstractDb class, which further extends MagentoFrameworkDataCollection. The final parent collection class then implements the following interfaces: IteratorAggregate, Countable, MagentoFrameworkOptionArrayInterface, and MagentoFrameworkDataCollectionDataSourceInterface. Through this deep inheritance, a large number of methods become available to our collection class, such as count, getAllIds, getColumnValues, getFirstItem, getLastItem, and so on.

With regard to our newly defined collection class, the only actual requirement for us is to define the _construct method. Within the _construct method, we call the _init function to which we pass two parameters. The first parameter specifies the Ticket model class FoggylineHelpdeskModelTicket and the second parameter specifies the Ticket resource class FoggylineHelpdeskModelResourceModelTicket.

The three classes we just defined (model, resource, collection) act as an overall single entity persistence mechanism. With the currently defined code, we are able to save, delete, update, lookup with filtering, and list our Ticket entities, which we demonstrate in the upcoming sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.238.70