Welcome Guest, Not a member yet? Register   Sign In
Model working in one controller, but not in another.
#1

[eluser]mdrisser[/eluser]
Hi all. I have a rather perplexing problem. I have a Model with to functions each does a select * on a different table. I'm using these values to populate two select lists. In one controller everything works as expected, in a second controller, using the exact same code, it doesn't work. That is, it doesn't return anything.

Here's the code:

In both controllers:
Code:
$this->load->model('Search_model', 'Search');
$this->data['types'] = $this->Search->get_types();
$this->data['cities'] = $this->Search->get_cities();

And the Model is:
Code:
class Search_model extends Model {
    function Search_model() {
        parent::Model();
    }
    
    function get_types() {
        $query = $this->db->query('SELECT * FROM types ORDER BY types ASC');
        return $query->result_array();
    }
    
    function get_cities() {
        $query = $this->db->query('SELECT * FROM cities ORDER BY cities ASC');
        return $query->result_array();
    }
}

Function in 1st controller, which works fine:
Code:
function search() {
        if($this->input->post('term') || $this->input->post('types[]') || $this->input->post('cities[]')) {
            $this->data['search_term'] = '';
            if($this->input->post('term')) {
                $this->data['search_term'] .= ' '.$this->input->post('term');
            }
            if($this->input->post('types[]')) {
                $this->data['search_term'] .= ' '.$this->input->post('types[]');
            }
            if($this->input->post('cities[]')) {
                $this->data['search_term'] .= ' '.$this->input->post('cities[]');
            }
            
            $this->data['search'] = true;
            
            $this->db->select('id, name, address, summary, link, img_thumb, img');
            
            if($this->input->post('term')) {
                $term = $this->input->post('term');
                
                $this->db->like('name', $term);
                $this->db->orlike('address', $term);
                $this->db->orlike('summary', $term);
                $this->db->orlike('img', $term);
            }
            
            if($this->input->post('types[]')) {
                $types = $this->input->post('types[]');
                foreach($types as $type) {
                    $this->db->orlike('types', $type);
                }
            }
            
            if($this->input->post('cities[]')) {
                $cities = $this->input->post('cities[]');
                foreach($cities as $city) {
                    $this->db->orlike('cities', $city);
                }
            }
            
            $this->db->limit(50);
            $this->db->orderby('name', 'asc');
            $results = $this->db->get('places');
            
            if($results->num_rows() > 0) {
                foreach($results->result_array() as $row) {
                    $this->data['places'][] = $row;
                }
            }
            
            $results->free_result();
            
            $this->load->view('places_view', $this->data);
        } else {
            $this->data['search'] = false;
                    
                        // This works here
            $this->load->model('Search_model', 'Search');
                $this->data['types'] = $this->Search->get_types();
                $this->data['cities'] = $this->Search->get_cities();
            
            $this->load->view('search_view', $this->data);  
        }
    }

Function in 2nd Controller, which doesn't work:
Code:
function process_form() {
        $validation_rules = array(
            array(
                'field' => 'placeName',
                'label' => 'Name',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'placeAddress',
                'label' => 'Address',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'placeDesc',
                'label' => 'Summary',
                'rules' => 'trim'
            ),
            array(
                'field' => 'placeUrl',
                'label' => 'URL',
                'rules' => 'trim|prep_url'
            )
        );
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules($validation_rules);
        
        if($this->form_validation->run() === FALSE) {
                        // This doesn't work here
            $this->load->model('Search_model', 'Search');
                $this->data['types'] = $this->Search->get_types();
                $this->data['cities'] = $this->Search->get_cities();
            
            $this->load->view('addplace_view', $this->data);
        } else {
            $this->load->view('success_view', $this->data);
        }
    }
Trying a print_r() on both $cities and $types in the view loaded by the 2nd controller results in:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: types

Filename: views/addplace_view.php

Line Number: 19
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: cities

Filename: views/addplace_view.php

Line Number: 20

I'm sure I must be doing something wrong, but I can't for the life of me figure out what it is.
#2

[eluser]jcopling[/eluser]
I can't really speak to why this particular bit of code will work in one place but not in another. But my suggestion would be to try a print_r() on the variable $this->types and $this->cities inside the view.

Personally, extending CI to contain your data with:
Code:
$this->data['types'] = $this->Search->get_types();
$this->data['cities'] = $this->Search->get_cities();
makes me a bit uncomfortable. At least with using such a common variable name as `data`. I would achieve the same result by using a variable to contain the results:
Code:
$data['types'] = $this->Search->get_types();
$data['cities'] = $this->Search->get_cities();
$this->load->view('addplace_view', $data);
which would be printed in the view with print_r($types) and print_r($cities).

Hope this helps.
#3

[eluser]mdrisser[/eluser]
Doing a print_r on $this->types gave me the same type of message.

I'm not seeing much difference between your code and mine, aside from my $data being a class variable and yours being local to the function. Mine is a class variable, because in the constructor I'm setting a few other parts of the array.

I've set the log_threshold to 4 and noticed in the logs that in the Controller that isn't having any problems, the Model is being initialized, but in the Controller that is broken, the Model is not being initialized. I've tried moving $this->load->model('Search_model', 'Search'); to various places within the broken Controller in the vain hope that maybe it just needed to be called in a different place, but kept coming up with the same result.
#4

[eluser]mdrisser[/eluser]
D'OH!

I found the problem, I was trying to load the Model in a function that I wasn't calling at that point.

I was calling: http://localhost/myapp/index/php/addplace and the Model was being loaded in the process_form function, which means it wasn't being loaded until I called http://localhost/myapp/index.php/add_place/process_form




Theme © iAndrew 2016 - Forum software by © MyBB