使用Joomla的自定义组件从模型中获取数据

时间:2014-04-11 09:31:28

标签: php mysql joomla

所以我一直在尝试使用Joomla中的MVC方法从数据库中获取单个项目的数据。我一直在检查com_content如何执行此操作,但我似乎无法从URL获取ID 这是获取单个项目数据的模型

<?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;
        }
}

我收到一个SQL错误,因为$ id为空..(我从表中删除了前缀)

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY date ASC' at line 4 SQL=SELECT `id`,`title`,`username`,`docname`,`docid`,`date`,`pages`,`description` FROM `#__issuu_publications` WHERE `state` = `1` AND `id` = ORDER BY date ASC

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

尝试使用以下内容替换where子句:

$query->where($db->quoteName('state') . ' = 1 AND ' . $db->quoteName('id') . ' = ' . $db->quote((int) $id));
  • 我删除了1
  • 周围的引号
  • $id替换为(int) $id
  • 并更正了quoteName
  • 上的拼写错误