fuel_model() does not work

edited April 2017 in Modules
Hi, i have a problem with a new site i've dowloaded the last version of fuel and i'm not able to get some row with a fuel_model() function this is my code:

<?php $data = fuel_model('catalog_categories_model'); ?>
<?php
foreach ($data as $key => $val):
?>
"><?= $val['category'] ?>
<?php
endforeach;
?>

and this function does not work at all in view, also try to use this model and works fine in a controller this is the model code

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

require_once(FUEL_PATH . 'models/Base_module_model.php');

class Catalog_categories_model extends Base_module_model {

function __construct() {
parent::__construct('catalog_categories'); // table name
}

function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc', $just_count = FALSE) {
$data = parent::list_items($limit, $offset, $col, $order, $just_count);
echo($this->db->last_query());
return $data;
}


function get_items() {
$data = $this->db->get('catalog_categories');
return $data->result();
}

function get_product_category($product) {
$this->db->where('id', $product->id_category);
$data = $this->db->get('catalog_categories');
if ($data->num_rows() == 1) {
$row = $data->row();
return $row;
}

return NULL;
}

function get_item($key, $id = NULL) {
if ($key != NULL) {
$this->db->where('key', $key);
}
if ($id != NULL) {
$this->db->where('id', $id);
}
$data = $this->db->get('catalog_categories');
if ($data->num_rows() == 1) {
$row = $data->row();
return $row;
}

return NULL;
}

}

class Product_category_model extends Base_module_record {
// put your record model code here
}

could someone help me please, thanks in advance.

Comments

  • edited 8:11PM
    Try the following without the suffix "_model":
    fuel_model('catalog_categories')
  • edited April 2017
    Hi, Admin.

    i tried but no results again this is my backtrace i hope could help

    A PHP Error was encountered

    Severity: Warning

    Message: Invalid argument supplied for foreach()

    Filename: SPC/menu.php

    Line Number: 3

    Backtrace:

    File: /home/ispc/sandbox.ispc.com.mx/fuel/application/views/_blocks/SPC/menu.php
    Line: 3
    Function: _error_handler

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/core/Loader.php
    Line: 396
    Function: include

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/core/Loader.php
    Line: 323
    Function: _ci_load

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/libraries/Fuel_blocks.php
    Line: 267
    Function: view

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/helpers/fuel_helper.php
    Line: 96
    Function: render

    File: /home/ispc/sandbox.ispc.com.mx/fuel/application/views/_layouts/SPC.php
    Line: 18
    Function: fuel_block

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/core/Loader.php
    Line: 396
    Function: include

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/core/Loader.php
    Line: 323
    Function: _ci_load

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/core/Loader.php
    Line: 462
    Function: view

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/libraries/Fuel_pages.php
    Line: 1034
    Function: module_view

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/controllers/Page_router.php
    Line: 169
    Function: variables_render

    File: /home/ispc/sandbox.ispc.com.mx/fuel/modules/fuel/controllers/Page_router.php
    Line: 45
    Function: _remap_variables

    File: /home/ispc/sandbox.ispc.com.mx/index.php
    Line: 364
    Function: require_once
  • edited 8:11PM
    To debug, instead of using fuel_model, you can use the following (which is essentially what fuel_model is an alias to):
    $CI->load->model('catalog_categories_model'); $data = $CI->catalog_categories_model->find_all(); $CI->catalog_categories_model->debug_query();
    This will output the raw query being used which you can test out.
  • edited 8:11PM
    hi again, this code works fine the code return select query to the table, but now my question is why fuel_model() does not work am i doing something wrong? thanks in advance
  • edited 8:11PM
    Is there a catalog_categories module setup in MY_fuel_modules?
  • edited 8:11PM
    that was the mistake thanks for all admin
  • Hi Admin,

    Same issue for me but I have module setup in MY_fuel_modules:

    $config['modules']['home_sliders'] = array(
    'module_name' => 'Home Sliders',
    'module_uri' => 'home_sliders',
    'model_name' => 'home_sliders_model',
    'model_location' => '',
    'display_field' => 'slide_title',
    'preview_path' => '',
    'permission' => 'home_sliders',
    'instructions' => 'Here you can manage the slider for your home page.',
    'archivable' => TRUE,
    'nav_selected' => 'home_sliders'
    );

    in _layouts/home.php

    $slides = fuel_model('home_sliders', array('find' => 'all', 'where' => array('published' => 'yes'), 'id asc'));
    print_r($slides);

    did not work.

    Please help.

  • edited July 2018

    Hello Admin,

    I notice that when I use print_r($slides);

    I get HTTP ERROR 500. If I comment the line print_r, error goes.

    Please help me to fix this error.

    Thanks,
    Prayas

  • The error may be due to running out of memory. Because the data records have a reference to the CI object, If you do a print_r on the result set, it will cause an infinite loop. If you do something like the following, does it print out data:

    foreach ($slides as $slide) :
    print_r($slide->values());
    endforeach;
    
  • Yes, it print the data.

    I did not get why print_r($slides) did not work. I did it in my localhost.

    Thanks.

Sign In or Register to comment.