2

In PHP Storm I am always tells me that the CMSObject->get() function is deprecated and that I should implement my own getter. I would, if I could... This message comes with standard things like:

...
$item = parent::getItem($pk);
$params = json_decode($item->get('params'));
...

This call is made within a model, for example. Get the item - Get the params - how should I do it differently? $item is already a "CMSObject" here.

Thanks for your suggestions. It would be nice if a new Extension would also run on J6 then...

1 Answer 1

1

This is my getItem (J6) in a single record model example. Hope this helps.

    /**
     * Method to get an object.
     * @param   integer $id The id of the object to get.
     * @return  mixed    Object on success, false on failure.
     * @throws Exception
     */

public function getItem($id = null)
{
    if ($this->_item === null) {
        $this->_item = false;

        if (empty($id)) {
            $id = $this->getState('application.id');
        }

        // Get a level row instance.
        $table = $this->getTable();

        // Attempt to load the row.
        if ($table->load($id)) {

            // Check published state.
            if ($published = $this->getState('filter.published')) {
                if (isset($table->state) && $table->state != $published) {
                    throw new \Exception(Text::_('COM_MYCOMPONENT_ITEM_NOT_LOADED'), 403);
                }
            }

            // Convert the Table to a clean Object.
            $properties  = $table->getProperties(1);
            $this->_item = ArrayHelper::toObject($properties, 'stdClass');

        }

        if (empty($this->_item)) {
            throw new \Exception(Text::_('COM_MYCOMPONENT_ITEM_NOT_LOADED'), 404);
        }
    }

    if (isset($this->_item->created_by)) {
        $this->_item->created_by_name = MycomponentHelper::getSpecificUser($this->_item->created_by)->name;
    }

    if (isset($this->_item->modified_by)) {
        $this->_item->modified_by_name = MycomponentHelper::getSpecificUser($this->_item->modified_by)->name;
    }

    return $this->_item;
}

As for params, I get these from the populateState function.

// Load the parameters.
$params       = Factory::getApplication('com_mycomponent')->getParams();
$params_array = $params->toArray();
2
  • This is site side of website. So what parameters are you after, component params or menu params and do you need them in the model? Mar 22 at 0:40
  • It's a bit weird to explain - i load the params of a Custom Module Instance so my Model / Table gets the information from #_modules - Based on your answer, I thought about it again and came up with the following solution: $item = parent::getItem($pk); $params = new Registry($item->params); $sourceMethod = $params->get('source_method', null); ... In Fact i need the "item" Params that way its working
    – Marco
    Mar 22 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.