1

Today I made my first Joomla component following their tutorial. Everything in my component was working as intended until I decided to add a second view to my component. I added a new folder in the /views/ directory of my component and added all the necessary files : view.html.php, index.html, tmpl/default.xml, tmpl/default.php, tmpl/index.html.

Here is my view.html.php

class VacanciesViewSingle extends JViewLegacy
{
    var $vacancy = null;

    function display($tpl = null)
    {
        $this->vacancy = $this->get('Vacancy');

        // // Check for errors.
        if (count($errors = $this->get('Errors')))
        {
            JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

            return false;
        }

        parent::display($tpl);
    }
}

As long as I understood how components in Joomla work this should call the getVacancy() method from my model class.

Inside my model class I have only 2 functions: getVacancies() which works perfectly with my other view and getVacancy() which for debugging purposes return just a number:

public function getVacancy()
{
    return "10";
}

So I'm expecting my view to output "10". This is my `tmpl/default.php' file:

defined('_JEXEC') or die('Restricted access');
?>
<?php echo $this->vacancy;?>

PS. Opening /index.php?option=com_vacancies&view=single doesn't return any errors, just my page with blank space where the component output should be... PS2. If I change $this->vacancy = $this->get('Vacancy'); to $this->vacancy = "10" in my view class I get the expected output of '10' so I suppose getVacancy() is not called for some reason.

3

It sounds like you only have one model, rather than the expected one-model-per-view.

As such, it is looking for the function in a model related to your singles view, which does not exist.

Try passing a second parameter to the get function to confirm the name of the model you want to use, ie

$this->vacancy =$this->get('vacancy','vacancies');

(replacing 'vacancies' if you have called your model something else)

Further details of how the get function works are here.

  • So is it possible to have multiple views in a component with a single model? – VTodorov Oct 13 '17 at 11:49
  • 1
    Yes, but you'll need to tell your other views which model to use, as per the answer above. There may be better ways (I'm wondering if a simple second model that just inherits the other one may be a tidier approach), but I tend to use JModelLegacy::getInstance to get access to a different model's functions if I need to do that. – Richard B Oct 13 '17 at 12:01

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.