简体   繁体   中英

Getting data from model with custom component for Joomla

So I've been trying to get data from a single item from the database using the MVC method in Joomla. I've been inspecting com_content how to do this, but I can't seem to get the id from the URL This is my model to get the data for a single item

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

I get a SQL error because the $id is empty.. (I removed the prefix from the table)

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

Any help is appreciated!

Try replacing your where clause with the following:

$query->where($db->quoteName('state') . ' = 1 AND ' . $db->quoteName('id') . ' = ' . $db->quote((int) $id));
  • I've removed the quotes around 1
  • replaced $id with (int) $id
  • And corrected a spelling mistake on quoteName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM