Tuesday 28 January 2014

fix strict standards issues in Joomla Component development

While developing a Joomla Component I was getting this issue. 

Strict Standards: Only variables should be assigned by reference in * on line * Strict Standards: Only variables should be assigned by reference in /* on line * Notice: Undefined variable: items in * on line *

In my case this was due to using old Joomla Component rules that now with Joomla 3.2 need updating.

Take a look at this basic principle of passing values through MVC in a Joomla Component.

The Model .  com_urcomp/models/preview.php
defined('_JEXEC') or die;

class Vm2mijoModelPreview extends JModelList
{
 
 /* lets go and get all the details from MySql and then we'll deal with the array in the view */ 

  public function getVirtuemartCategory(){
      $db = &JFactory::getDBO();
      $query  = "select ";
      $query .= "* ";
   // new xref table for product/categories is #__virtuemart_product_categories
      $query .= "from #__virtuemart_categories as jvc ";
     $query .= "LEFT JOIN #__virtuemart_categories_en_gb as jvcgb ON jvc.virtuemart_category_id=jvcgb.virtuemart_category_id ";
  $query .= "LEFT JOIN #__virtuemart_category_medias as jvcm ON jvc.virtuemart_category_id=jvcm.virtuemart_category_id ";
      $db->setQuery($query);
      $row = $db->loadObjectList();
      return $row;
    }
 
 public function getGreeting()
 {
 return 'Hello, World!';
 }

}  
The View.html.php views/preview/view.html.php

defined('_JEXEC') or die;

class Vm2mijoViewPreview extends JViewLegacy
{
   function display($tpl = null) 
        {
  $model = $this->getModel();
  
  
  $greeting = $model->getGreeting();
  $this->assignRef('greeting', $greeting);
  
                          // Display the view
                parent::display($tpl);
        }
}


views/preview/tmpl/default.php


 defined('_JEXEC') or die;

// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
 

echo $this->greeting;  


The error I was having was being caused on the line

$model = $this->getModel();

which I had as


$model = &$this->getModel();

No comments: