简体   繁体   English

控制器joomla 3.1中的init模型

[英]init model from controller joomla 3.1

I try to call a model from one of my Joomla controller but that doesn't seem to work. 我尝试从我的Joomla控制器之一调用模型,但这似乎不起作用。

class TieraerzteControllerDatatable extends JControllerLegacy
{
        /**
         * display task
         *
         * @return void
         */

        function display($cachable = false) 
        {

                // set default view if not set

                $input = JFactory::getApplication()->input;

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

                $model->getTableData('grumpi_tieraerzte', 'id', array('id','name','strasse','telefon','fax'));

                die();

        }
}

In my model I placed a var_dump but it is not accessed. 在我的模型中,我放置了一个var_dump,但是无法访问它。 How can I debug my model call in controller? 如何在控制器中调试模型调用?

I try to access the models return with the following url /administrator /index.php?option=com_tieraerzte&task=datatable.display&{arguments for the model} 我尝试使用以下网址访问返回的模型/ administrator /index.php?option=com_tieraerzte&task=datatable.display&{arguments for the model}

trying to debug my codes piece by piece seems like the model is accessed and this code is causing the problem 试图逐段调试我的代码似乎访问了模型,并且此代码导致了问题

 // Paging
 $sLimit = "";
 if (isset(JRequest::getVar('iDisplayStart')) && JRequest::getVar('iDisplayLength') != '-1')
 {
     $sLimit = "LIMIT " . intval(JRequest::getVar('iDisplayStart')) . ", " . intval(JRequest::getVar('iDisplayLength'));
 }

The whole method: 整个方法:

public function getTableData($table, $index_column, $columns)
{
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    // Paging
    $sLimit = "";
    if (isset(JRequest::getVar('iDisplayStart')) && JRequest::getVar('iDisplayLength') != '-1')
    {
        $sLimit = "LIMIT " . intval(JRequest::getVar('iDisplayStart')) . ", " . intval(JRequest::getVar('iDisplayLength'));
    }
    // Ordering

    $sOrder = "";
    if (isset(JRequest::getVar('iSortCol_0')))
    {
        $sOrder = "ORDER BY ";
        for ($i = 0; $i < intval(JRequest::getVar('iSortingCols')); $i++)
        {
            if (JRequest::getVar('bSortable_' . intval(JRequest::getVar('iSortCol_' . $i))) == "true")
            {
                $sortDir = (strcasecmp(JRequest::getVar('sSortDir_' . $i), 'ASC') == 0) ? 'ASC' : 'DESC';
                $sOrder .= "`" . $columns[intval(JRequest::getVar('iSortCol_' . $i))] . "` " . $sortDir . ", ";
            }
        }

        $sOrder = substr_replace($sOrder, "", -1);
        if ($sOrder == "ORDER BY") {
            $sOrder = "";
        }
    }

    /*

    * Filtering

    * NOTE this does not match the built-in DataTables filtering which does it

    * word by word on any field. It's possible to do here, but concerned about efficiency

    * on very large tables, and MySQL's regex functionality is very limited

    */

    $sWhere = "";

    if (isset(JRequest::getVar('sSearch')) && JRequest::getVar('sSearch') != "")
    {
        $like = '%' . JRequest::getVar('sSearch') . '%';
        $sWhere = "WHERE (";
        for ($i = 0; $i < count($columns); $i++)
        {
            if (isset(JRequest::getVar('bSearchable_' . $i)) && JRequest::getVar('bSearchable_' . $i) == "true")
            {
                $sWhere .= "`" . $columns[$i] . "` LIKE '" . $like . "' OR ";
            }
        }

        $sWhere = substr_replace($sWhere, "", -3);
        $sWhere .= ')';
    }

    // Individual column filtering

    for ($i = 0; $i < count($columns); $i++)
    {

        if (isset(JRequest::getVar('bSearchable_' . $i)) && JRequest::getVar('bSearchable_' . $i) == "true" && JRequest::getVar('sSearch_' . $i) != '')
        {
            if ($sWhere == "")
            {
                $sWhere = "WHERE ";
            } else {
                $sWhere .= " AND ";
            }
            $sWhere .= "`" . $columns[$i] . "` LIKE ? " . $i . " ";
        }
    }

    // SQL queries get data to display

    $sQuery = "SELECT SQL_CALC_FOUND_ROWS `" . str_replace(" , ", " ", implode("`, `", $columns)) . "` FROM `" . $table . "` " . $sWhere . " " . $sOrder . " " . $sLimit;
    $statement = $this->_db->prepare($sQuery);


    // Bind parameters
    if (isset(JRequest::getVar('sSearch')) && JRequest::getVar('sSearch') != "")
    {
        $statement->bind_param('s', $like);
    }

    for ($i = 0; $i < count($columns); $i++)
    {
        if (isset(JRequest::getVar('bSearchable_' . $i)) && JRequest::getVar('bSearchable_' . $i) == "true" && JRequest::getVar('sSearch_' . $i) != '')
        {
            $statement->bind_param('s' . $i, '%' . $_GET['sSearch_' . $i] . '%');
        }
    }

    $db->setQuery($sQuery);
    $res = $db->loadObjectList();
    $rResult = array();

    foreach ($res as $row)
    {
        $rResult[] = $row;
    }

    $iFilteredTotal = current($db->setQuery('SELECT FOUND_ROWS()')->loadResultArray());

    // Get total number of rows in table

    $sQuery = "SELECT COUNT(`" . $index_column . "`) FROM `" . $table . "`";

    $iTotal = current($db->setQuery($sQuery));

    // Output

    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    // Return array of values

    foreach ($rResult as $aRow)
    {
        $row = array();
        array_push($row, '<input type="checkbox" id="someCheckbox" name="someCheckbox" />');
        for ($i = 0; $i < count($columns); $i++)
        {
            //var_dump($aRow->$columns[$i]);    
            if ($columns[$i] != '')
            {
                $row[] = $aRow->$columns[$i];
            }
        }

        array_push($row, '    
        <div class="btn-group" id="status">
            <input type="button" class="btn b1" value="On">
            <input type="button" class="btn b0 btn-danger" value="Off">
        </div>
        ');

        array_push($row, '
         <div class="clearfix">
           <a href =""><div class="label label-important" style="padding:6px 9px; margin-right:5px"><span class="icon-trash"></span></div></a>
           <a href="index.php?option=com_tieraerzte&task=tieraerzt.edit&layout=edit&id=' . $aRow->id . '"><div class="label label-success" style="padding:6px 9px"><span class="icon-cog"></span></div></a>
         </div>
        ');
        $output['aaData'][] = $row;
    }
    echo json_encode($output);
}
  1. Make sure $model inside the controller is not null. 确保控制器内的$ model不为null。 If it's not null, than the problem is somewhere in the model's method called. 如果不为null,则问题出在模型的方法中。

  2. if $model is null, somehow the model you are calling is not loaded. 如果$ model为null,则不加载您正在调用的模型。 Temporary try just to include the file with require_once JPATH_COMPONENT . '/models/datatable.php' 临时尝试仅将文件包含在require_once JPATH_COMPONENT . '/models/datatable.php' require_once JPATH_COMPONENT . '/models/datatable.php'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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