AnswerBun.com

Joomla 3 call model function inside controller

Joomla Asked by wbdevlop on December 28, 2020

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 JoomlaCMSFactory;

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 Answers

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

Answered by wbdevlop on December 28, 2020

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.

Answered by mickmackusa on December 28, 2020

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.

Answered by Irata on December 28, 2020

Add your own answers!

Related Questions

Disabling cache on specific part of page from above comment

2  Asked on September 14, 2020 by ruchika

 

Custom Category Field

1  Asked on August 18, 2020 by grant-g

     

How do I pass _JEXEC to a php file?

1  Asked on August 6, 2020 by firefighter

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP