0

I want to call a function from model inside controller

my model : httpdocs/components/com_storemanager/models/searchmanager.php

<?php

/**
 * @version    CVS: 1.0.2
 * @package    Com_storemanager
 * @author     LYT
 * @copyright  2017 LYT
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;

jimport('joomla.application.component.modellist');

/**
 * Methods supporting a list of storemanager records.
 *
 * @since  1.6
 */
class storemanagerModelSearchmanager extends JModelItem
{
    function __construct() {
        parent::__construct ();
    }

    /**
     * Method to auto-populate the model state.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @return void
     *
     * @since    1.6
     *
     */
    public function managerList($name) 
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true)
            ->select($db->qn("title"))  
            ->from($db->qn("jos_storemanager_manager"))
            ->where($db->quoteName('manager') . ' LIKE '. $db->quote('\''.$name.'.%\''));


        return $db->setQuery($query)->loadObjectList(); 
    }

}

I did try this inside my controller : /httpdocs/components/com_storemanager/controller.php

function searchmanager()
    {
        $jinput = JFactory::getApplication()->input;
        $name = $jinput->get('name');

        $managers = $this->getModel( 'Searchmanager' , 'Model' )->managerList($name);

        foreach($managers as $manager) 
        {
            echo '<br><h3>'.$manger->title . '</h3>';
        }

        exit;
    }

but no result;

I tried all solutions

require_once JPATH_COMPONENT_SITE.'/models/searchmanager.php'; 

    $tags_model = JModelLegacy::getInstance( 'Searchmanager', 'searchmanager' );
$tags_model->managerList($name);

Not working

$model = $this->getModel() ;
$articles = $model->managerList($name);

This one too not working

3
  • I need to bring up that your WHERE clause is not constructed in alignment with Joomla's advice on security. joomla.stackexchange.com/a/22712/12352 At the moment, you are single-quoting the string twice. Since you are only checking from the start of the column value, you might perfer this simpler alternative: LOCATE() ...written as ->where("LOCATE(" . $db->q($name) . ", manager) = 1"); To help you debug, use the enqueueMessage() lines from joomla.stackexchange.com/a/24161/12352
    – mickmackusa
    Aug 27, 2019 at 20:54
  • I am commenting this advice because I don't know if fixing this one thing will solve the whole problem. If it does, I am happy to transfer to an answer.
    – mickmackusa
    Aug 27, 2019 at 20:55
  • Also, replace the prefix you've got jos_ with #__.
    – Lodder
    Aug 27, 2019 at 22:10

3 Answers 3

0

I use this piece of code in my controller to access the model.

    JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_' . $feature . DIRECTORY_SEPARATOR . 'models');
    $model = JModelLegacy::getInstance('Items', 'InventoryModel');

That is cut and pasted straight out of a working piece of code, no edits so I expect it should work for you if you change the code around com_ . $feature and whats inside the the getInstance brackets.

2
  • To implement the DRY coding principle (which pleasantly reduces horizontal scrolling), you might consider declaring an array of directory names and glue them together with DIRECTORY_SEPARATOR like this: 3v4l.org/cIrLP
    – mickmackusa
    Aug 28, 2019 at 7:29
  • @mickmackusa thanks for the suggestion. I agree about the horizontal scroll. I use similar code in a few places so I will have a look if I can use your code to reduce things elsewhere as well.
    – Irata
    Aug 29, 2019 at 5:43
0

Well, I originally posted this as a comment, but advice that may resolve the issue must be posted as an answer. I will hedge my post and state that there may be additional problems beyond what I am addressing. See irata's answer for the possible MVC fix.

$db = JFactory::getDBO();
$query = $db->getQuery(true)
    ->select($db->qn("title"))  
    ->from($db->qn("#__storemanager_manager"))
    ->where("LOCATE(" . $db->quote($name) . ", " . $db->quoteName(manager) . ") = 1");

JFactory::getApplication()->enqueueMessage($query->dump(), 'info');  // don't show your rendered query to the public
try {
    return $db->setQuery($query)->loadObjectList();
} catch (Exception $e) {
    JFactory::getApplication()->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');  // don't show $e->getMessage() to public
}

The adjustment to the WHERE clause still requires qualifying rows to have a manager value that begins with the value held in $name.

I have included two enqueueMessage() calls to help you to debug, but you must remove them when you are finished the dev stage.

Please read https://joomla.stackexchange.com/a/22712/12352 regarding your LIKE syntax.

0

I fixed for someone if he get the same error by using My Model : /httpdocs/components/com_storemanager/models/searchmanager.php

<?php

    defined('_JEXEC') or die;

    jimport('joomla.application.component.modellist');

    /**
     * Methods supporting a list of Hpj_storemanager records.
     *
     * @since  1.6
     */
    class storemanagerModelSearchmanager extends JModelItem
    {

        /**
         * Method to auto-populate the model state.
         *
         * Note. Calling getState in this method will result in recursion.
         *
         * @return void
         *
         * @since    1.6
         *
         */
        public function managerList($name) 
        {
            $db = JFactory::getDBO();
            $query = $db->getQuery(true)
                ->select($db->qn("title"))  
                ->from($db->qn("#__storemanager_manager"))
                ->where("LOCATE(" . $db->quote($name) . ", " . $db->quoteName("manager") . ") = 1");

            JFactory::getApplication()->enqueueMessage($query->dump(), 'info');  // don't show your rendered query to the public
            try {
                return $db->setQuery($query)->loadObjectList();
            } catch (Exception $e) {
                JFactory::getApplication()->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');  // don't show $e->getMessage() to public
            }

        }

    }

And in my Controller : /httpdocs/components/com_storemanager/controller.php

function searchmanager()
    {
        $jinput = JFactory::getApplication()->input;

        $name = $jinput->get('text'));

        $model = $this->getModel('Searchmanager');

        $manegers = $model->managerList($name)

        foreach($managers as $manager) 
        {
            echo '<br><h3>'.$manager->title.'</h3><p>';
        }

        exit;
    }
1
  • While this may give you the desired result, it is in breach of the MVC design model to print anything from the Controller -- a View is where printing should occur. When I suggested loadObjectList() it was to allow the addition of other columns in the result set. However, if you are only extracting one column, the best method would be loadColumn() which will generate a 1-dimensional (indexed) array. This will allow reduced syntax while looping through the results. exit should not be necessary. You have a typo @ $manegers. What is your view directory structure?
    – mickmackusa
    Aug 29, 2019 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.