Php 使用Joomla的自定义组件从模型中获取数据
因此,我一直在尝试使用Joomla中的MVC方法从数据库中的单个项目获取数据。我一直在检查com_内容如何做到这一点,但我似乎无法从URL获取id 这是我获取单个项目数据的模型Php 使用Joomla的自定义组件从模型中获取数据,php,mysql,joomla,Php,Mysql,Joomla,因此,我一直在尝试使用Joomla中的MVC方法从数据库中的单个项目获取数据。我一直在检查com_内容如何做到这一点,但我似乎无法从URL获取id 这是我获取单个项目数据的模型 <?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla modelitem library jimport('joomla.application.componen
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
/**
* Issuu Model
*/
class IssuuModelItem extends JModelItem
{
/**
* @var string msg
*/
protected $item;
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @since 1.6
*
* @return void
*/
protected function populateState()
{
$app = JFactory::getApplication('site');
// Load state from the request.
$id = $app->input->getInt('id');
$this->setState('item.id', $id);
$offset = $app->input->getUInt('limitstart');
$this->setState('list.offset', $offset);
// Load the parameters.
$params = $app->getParams();
$this->setState('params', $params);
// TODO: Tune these values based on other permissions.
$user = JFactory::getUser();
if ((!$user->authorise('core.edit.state', 'com_issuu')) && (!$user->authorise('core.edit', 'com_issuu')))
{
$this->setState('filter.published', 1);
$this->setState('filter.archived', 2);
}
$this->setState('filter.language', JLanguageMultilang::isEnabled());
}
/**
* Returns a reference to the a Table object, always creating it.
*
* @param type The table type to instantiate
* @param string A prefix for the table class name. Optional.
* @param array Configuration array for model. Optional.
* @return JTable A database object
* @since 2.5
*/
public function getTable($type = 'Item', $prefix= 'IssuuTable', $config = array()) {
return JTable::getInstance($type,$prefix,$config);
}
/**
* Get the message
* @return string The message to be displayed to the user
*/
public function getItem($id = null)
{
$id = (!empty($id)) ? $id : (int) $this->getState('item.id');
if(!is_array($this->item)) {
$this->item = array();
}
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all records from the user profile table where key begins with "custom.".
// Order it by the ordering field.
$query->select($db->quoteName(array('id', 'title', 'username', 'docname', 'docid','date', 'pages', 'description')));
$query->from($db->quoteName('#__issuu_publications'));
$query->where($db->quoteName('state') . ' = `1` AND ' . $db->quoteName('id') . ' = ' . $db->qouteName($id));
$query->order('date ASC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$this->items = $db->loadObjectList();
return $this->items;
}
}
感谢您的帮助 尝试将
where
子句替换为以下内容:
$query->where($db->quoteName('state') . ' = 1 AND ' . $db->quoteName('id') . ' = ' . $db->quote((int) $id));
- 我已经删除了
1
- 将
替换为$id
(int)$id
- 并更正了
quoteName
state
=1和id
=Array”。id=Array是由$id变量实际是一个数组引起的,因此请对此进行调查。请尝试打印($id)以查看$id变量的外观。也许您可以只做(int)$id[0]而不是(int)$id您不需要使用quoteName($id),您应该只使用quote($id)-quoteName()用于数据库字段,quote()用于值。当您尝试订购时,您可能也需要在日期前后使用quote()。。。不完全是。值总是quote(),字段名quoteName()。因此,您的行应该是:$query->where($db->quoteName('state')。='$db->quote('1')。'和'$db->quoteName('id')。='$db->quote((int)$id));
$query->where($db->quoteName('state') . ' = 1 AND ' . $db->quoteName('id') . ' = ' . $db->quote((int) $id));