queries in MY_fuel_layout.php

edited September 2014 in Modules
Basically i need to do some queries in MY_fuel_layout.php

i tried to use $this->load->database() then call $this->db->query() - failed to work

so i think maybe because at the time $this is not reference to any class.

so i switch to CI way of doing

require_once(FUEL_PATH.'models/fuel_pages_model.php');
$CI =& get_instance();
$CI->load->model('fuel_pages_model');
$CI->fuel_pages_model->join('fuel_page_variables', 'fuel_page_variables.page_id = fuel_pages.id');
$result=$CI->fuel_pages_model->find_all(array('fuel_pages.layout'=>'whatsnew_article'));

print_r($result);
exit;

----- above works--------

require_once(FUEL_PATH.'models/fuel_pages_model.php');
$CI =& get_instance();
$CI->load->model('fuel_pages_model');
$CI->fuel_pages_model->join('fuel_page_variables', 'fuel_page_variables.page_id = fuel_pages.id');
$result=$CI->fuel_pages_model->find_all(array('fuel_pages.layout'=>'whatsnew_article','fuel_page_variables.name'=>'title'));

print_r($result);
exit;


------above failed, error, join no a method for model------


How to solve this?

Comments

  • edited September 2014
    As you probably know, $this refers to the current object. In your case, you are not in an object but a config file. For $this->load->database() to work, you need to be in the controller file in which the database object get's attached to. In addition, you need to target the correct module to load from. So the correct way to load the model is this way:
    $CI =& get_instance(); $CI->load->module_model(FUEL_FOLDER, 'fuel_pages_model'); $CI->fuel_pages_model->db()->join('fuel_page_variables', 'fuel_page_variables.page_id = fuel_pages.id'); $result=$CI->fuel_pages_model->find_all(array('fuel_pages.layout'=>'whatsnew_article')); print_r($result); exit;
  • edited 11:12PM
    strange, i tried this

    require_once(FUEL_PATH.'models/fuel_pages_model.php');
    $CI =& get_instance();
    $CI->load->model('fuel_pages_model');
    $CI->fuel_pages_model->join('fuel_page_variables', 'fuel_page_variables.page_id = fuel_pages.id');
    $result=$CI->fuel_pages_model->find_all(array('fuel_pages.layout'=>'whatsnew_article','fuel_page_variables.name'=>'title'));

    print_r($result);
    exit;


    which is quite similar to your solution, but does not work, saying join is not a valid method for the model
  • edited September 2014
    You need to use "$CI->fuel_pages_model->db()->join" (I modified my response above). Also, you don't need the require_once at the top.
  • edited 11:12PM
    just curious, how to retrieve the fuel_page_varaibles table in this example. i checked only fuel_pages table is retrieved when using find_all
  • edited 11:12PM
    What is the query that is outputted after running find_all (you can use $CI->fuel_pages_model->debug_query() to output)?
  • edited 11:12PM
    Only returns fue_pages and fuel_users, i managed to use CI active record such as $CI-〉model-〉db()-〉select to get the result, thanks
Sign In or Register to comment.