You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
j4examplecode/src/components/com_foos/src/Model/FooModel.php

91 lines
1.8 KiB

<?php
/**
* @package Joomla.Site
* @subpackage com_foos
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace FooNamespace\Component\Foos\Site\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
/**
* Foo model for the Joomla Foos component.
*
* @since __BUMP_VERSION__
*/
class FooModel extends BaseDatabaseModel
{
/**
* @var string item
*/
protected $_item = null;
/**
* Gets a foo
*
* @param integer $pk Id for the foo
*
* @return mixed Object or null
*
* @since __BUMP_VERSION__
*/
public function getItem($pk = null)
{
$app = Factory::getApplication();
$pk = $app->input->getInt('id');
if ($this->_item === null) {
$this->_item = [];
}
if (!isset($this->_item[$pk])) {
try {
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from($db->quoteName('#__foos_details', 'a'))
->where('a.id = ' . (int) $pk);
$db->setQuery($query);
$data = $db->loadObject();
if (empty($data)) {
throw new \Exception(Text::_('COM_FOOS_ERROR_FOO_NOT_FOUND'), 404);
}
$this->_item[$pk] = $data;
} catch (\Exception $e) {
$this->setError($e);
$this->_item[$pk] = false;
}
}
return $this->_item[$pk];
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
*
* @since __BUMP_VERSION__
*/
protected function populateState()
{
$app = Factory::getApplication();
$this->setState('foo.id', $app->input->getInt('id'));
$this->setState('params', $app->getParams());
}
}