• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loaded model available in view without passing it explicitly from controller

#1
Hello dear forum members,

Today I suddenly notices a - for me - strange behavior in CI 2. Hopefully I find some insight here of whether this a bug is or not..?

When I load a self made test model in a controller and DON'T pass this to a view, this model is still available in this particular view. This to my amazement I may add! Because I thought this should not be possible.

This is the situation:

Model
PHP Code:
class Test_model extends CI_Model {

 
   function __construct()
 
   {
 
       parent::__construct();
 
   }

 
   function getData()
 
   {
 
       $query $this->db->get('products');
 
       return $query;
 
   }


Controller
PHP Code:
class Welcome extends CI_Controller
{
    function 
__construct()
    {
        
parent::__construct();

        
$this->load->helper('url');
        
$this->load->library('tank_auth');
    }

    

 
       function test()
 
       {
 
           $this->load->model('test_model');
 
           
            $this
->load->view('welcome_message');
 
       }



View
PHP Code:
<!DOCTYPE html>
<
html lang="en">
<
head>
    <
meta charset="utf-8">
    <
title>Welcome to CodeIgniter</title>
</
head>
<
body>


<
ul>
 
   <?php
    foreach 
($this->test_model->getData()->result() as $product) {
 
       echo "<li>";
 
       print_r($product);
 
       echo "</li>";
 
   }
</
ul>
?>

</body>
</html> 

Output
The output of this is a page showing a list with all the product details per list-item. 

Conclusion and question
The model was not passed to the view in an array as second parameter in the controller ( like: $this->load->view('path/to/view', $data); ), but still is available in the view through "$this->model_name". 
Is this intentionally?
It is my understanding that the query in the model should be passed as a variable in the controller to the view. So that the view cannot get to perhaps sensitive data in the model. The availabillity of the model data should be controlled in the controller! Or do overlook something..?
Reply

#2
It's not a bug, but not something that I would recommend taking advantage of very often, either.

It is due to the way that the $this object (or get_instance()) in CodeIgniter points back to the controller. Since you're in the controller any calls to $this that don't match anything in the class itself, will look to the main controller instance, which happens to have links to the models.

So, while it's technically possible - don't rely on it because it gets pretty confusing for devs that don't understand the nuance. Stick with the way you mentioned - pass it to a variable in the controller to the view. It's all good.
Reply

#3
Thank you for your answer. But still I think it is strange this is technically possible..
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.