CodeIgniter Forums
Is it possible to pass a variable from MODEL to CONTROLLER?! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Is it possible to pass a variable from MODEL to CONTROLLER?! (/thread-25462.html)



Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-12-2009

[eluser]developer10[/eluser]
First of all, my query runs twice (once for counting, once for limiting and displaying data):

Code:
0.0687       SELECT * FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE  d_seo = 'informatika-elektronika-racunari-i-oprema' AND  _f_grad = 'Sarajevo' AND  f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC  
0.0390       SELECT * FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE  d_seo = 'informatika-elektronika-racunari-i-oprema' AND  _f_grad = 'Sarajevo' AND  f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC LIMIT 0, 10

This is my model method:

Code:
function fetch_all($getDjelatnost, $getGrad, $getSlovo, $getStart, $per_page)
    {    
        $qry = " SELECT * FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE ";
            if($getDjelatnost && $getDjelatnost != 'Sve')
                $append = " d_seo = '$getDjelatnost' AND ";
            else $append = "";
                $qry .= $append;
                
            if($getGrad && $getGrad != 'Svi')
                $append = " _f_grad = '$getGrad' AND ";
            else $append = "";
                $qry .= $append;
                
            if($getSlovo)
                $append = " _f_ime LIKE '$getSlovo%' AND ";
            else $append = "";
                $qry .= $append;
                
            if($getFid)
                $append = " f_id = '$getFid' AND ";
            else $append = "";
                $qry .= $append;
                
            if($getPortfolio)
                $append = " _F_KEYWORD = '$getPortfolio' AND ";
            else $append = "";
                $qry .= $append;
                        
        $qry .= " f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC ";
                

        $limited = $qry . "LIMIT ";
            if($getStart)
                $append = $getStart;
            else $append = 0;
                $limited .= $append;
                
        $limited .= ", " . $per_page;
            
        
        $noLimit =            $this->db->query($qry);
        $withLimit =        $this->db->query($limited);
            
            //// pagination config
            $pagBase = site_url() . 'prikazi/adresar/';
                if($getDjelatnost) $add = "djelatnost/{$getDjelatnost}/"; else $add=""; $pagBase .= $add;
                if($getGrad) $add = "grad/{$getGrad}/"; else $add=""; $pagBase .= $add;
                if($getSlovo) $add = "slovo/{$getSlovo}/"; else $add=""; $pagBase .= $add;
            $pagBase .= "start";

            $config['num_links'] = 4;
            $config['uri_segment'] = $this->uri->total_segments();
            $config['base_url'] = $pagBase;
            $config['total_rows'] = $noLimit->num_rows(); //$this->db->count_all_results();
            
            $this->pagination->initialize($config);
            ////////////////
        
        if($withLimit->num_rows() > 0) {
            $result = $withLimit->result();
        }
        
        return $result;
    }

But, what i want more than preventing the query run twice (i know many people here have the same problem),
is to move those pagination configs to my controller.
My question is: Is it possible to declare a variable like this:

Code:
$varToPass = $noLimit->num_rows();

and then pass it from the model to my controller (i want to use it there as total_rows config item for pagination)? That way i think i'd be able to move everything
related to pagination to the controller.

THANKS!


Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-12-2009

[eluser]Unknown[/eluser]
You could create a function that returned the value you want to pass such as:
Code:
function varToPass() {
    return $varToPass;
}
then access it like
Code:
$this->your_model->varToPass();
I don't know if it's possible pass it so you can access it like a normal variable though :/


Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-12-2009

[eluser]developer10[/eluser]
[quote author="beckyftw" date="1260689784"]You could create a function that returned the value you want to pass such as:
Code:
function varToPass() {
    return $varToPass;
}
then access it like
Code:
$this->your_model->varToPass();
I don't know if it's possible pass it so you can access it like a normal variable though :/[/quote]

that variable should contain the value mentioned above (from the previous model function).
if i send it from a new function, it won't hold the value i need (num_rows BEFORE applying the limit) unless
i run a similar query in that new function as well.

so, i need the variable sent from the same function.

thanks anyway!


Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-13-2009

[eluser]bhbutter123[/eluser]
the way that i have been doing it is by returning an array in my model so that essential here is what my model will setup and return to my controller:

$database_information['data'] = the data from the table;
$database_information['counts'] = various counts from the table you want to use;
$database_information['other'] = any other information you would need;

In your situation I would put that variable you want to pass inside of $database_information['other'];


Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-13-2009

[eluser]jedd[/eluser]
[quote author="cold_fusion" date="1260683921"]
First of all, my query runs twice (once for counting, once for limiting and displaying data):
Code:
SELECT * FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE  d_seo = 'informatika-elektronika-racunari-i-oprema' AND  _f_grad = 'Sarajevo' AND  f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC
[/quote]

I think that for a count call you put less stress on your DB if you do this:
Code:
SELECT COUNT(id) AS count FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE  d_seo = 'informatika-elektronika-racunari-i-oprema' AND  _f_grad = 'Sarajevo' AND  f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC
- and then utilise count. I recall reading that * or COUNT(*) is more expensive than COUNT of an indexed field, though you should do some research on this to be sure.


Your model code is a bit scary.

Rather than this:
Quote:
Code:
if($getDjelatnost && $getDjelatnost != 'Sve')
    $append = " d_seo = '$getDjelatnost' AND ";
else $append = "";
    $qry .= $append;
                
if($getGrad && $getGrad != 'Svi')
    $append = " _f_grad = '$getGrad' AND ";
else $append = "";
    $qry .= $append;

- you can just do this:
Code:
if($getDjelatnost && $getDjelatnost != 'Sve')
    $qry .= " d_seo = '$getDjelatnost' AND ";
                
if($getGrad && $getGrad != 'Svi')
    $qry .= " _f_grad = '$getGrad' AND ";

With a ternary operator you could even get that down to a single line per conditional.


Quote:But, what i want more than preventing the query run twice (i know many people here have the same problem),

I don't think there's a way around this - you need to do a select that includes a count() against every qualifying record, and a separate select to pull in the specific records you want. Keep in mind that the DB will cache the first query, and if the relevant table doesn't change, then subsequent (identical) queries will be blindingly fast. Enjoy the thrill of the database engine's performance!