1

I am facing a problem with the function loadFormData where there is check whether to pull data from the session or from the table.

protected function loadFormData() {
  // Check the session for previously entered form data.
  $data = JFactory::getApplication()->getUserState('com_mycomp.edit.profile.data', array());
  if (empty($data)) {
    $data = $this->getItem();
  }

  //Do further validation on $data and adjust the required values
  if (is_object($data)){
    $data->custom_field = 'custom value';
  }else if (is_array($data)){
    $data['custom_field'] = 'custom value'
  }

  return $data;
}

While pulling the data from session it pulls in Array format and while pulling the data from database it pulls as JObject. This makes our life harder by checking whether it is object or array. Is there any easier way to manage this by having both as JObject of Array?

2

You could map array to object.

$object = Joomla\Utilities\ArrayHelper::toObject($array, 'JObject');

Your code would become

$data = (array)JFactory::getApplication()->getUserState('com_mycomp.edit.profile.data', array());
$data = Joomla\Utilities\ArrayHelper::toObject($data, 'JObject');

//Do further validation on $data and adjust the required values
if (is_object($data)){
  $data->custom_field = 'custom value';
}
  • JArrayHelper is deprecated. – Sahil Purav Apr 30 '15 at 6:40
  • Yes, noticed it. Thanks for pointing out. Edited my answer. – Rene Korss Apr 30 '15 at 6:45
1

What data you are returning in getItem function? You can simply match the data you are setting in session or you are returning in your getItem function.

In your getItem function, instead of

$data = $db->loadObject();

load data using

$data = $db->loadAssoc();
  • I didn't override the getItem method. However I can try. – Malaiselvan Apr 30 '15 at 5:04
1

Instead of validating in loadFormData, you can think of overriding getItem() method of JModelAdmin class (in your model) to make it return an associative array instead of JObject.

// Make sure you are making it public if you going to call it in view.html.php

    public function getItem($pk = null)
    {
        $pk    = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id');
        $table = $this->getTable();

        if ($pk > 0)
        {
            // Attempt to load the row.
            $return = $table->load($pk);

            // If you want you can load more result from another table and merge it here.

            // Check for a table object error.
            if ($return === false)
            {
                return false;
            }
        }

        // Convert to the JObject before adding other data.
        $properties = $table->getProperties(1);
        $item       = \Joomla\Utilities\ArrayHelper::toObject($properties, 'JObject');

        if (property_exists($item, 'params'))
        {
            $registry     = new Registry;
            $registry->loadString($item->params);
            $item->params = $registry->toArray();
        }

        // Here you can add custom data as follows:

        $item->custom_field = 'custom value';

        $data = \Joomla\Utilities\ArrayHelper::fromObject($item);

        return $data;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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