Welcome Guest, Not a member yet? Register   Sign In
Could I use the model in the view? Who can help me?
#1

[eluser]sexy22[/eluser]
Could I use the model in the view?

I want to use the mothods of model direct in the view,but I do not know do that is right?

for example,

this is a file named test.html


<td>
&lt;input type="text" name="year" size="5" value=&lt;?php

$this-&gt;load->model('hrsModel/projectCl');

$this->projectCl->getYear($project_started);

?&gt;
>
Like this writes may?

<td>
#2

[eluser]wiredesignz[/eluser]
It is ok, the View displays data from the Model, but is not supposed to change it.

I set the Model up in my Controller via a query... store the resultset in the Model and then access the Model resultset property from my View.

This does however make the View dependent on the Model.


Edit: don't load the Model in the View. (I don't think you can anyway)
#3

[eluser]gtech[/eluser]
[edit] ignore this reply I was talking rubbish
#4

[eluser]gtech[/eluser]
ah now I read the example .... you dont obviously need js.. in your example you could do:

controller
Code:
....
funtion func_name() {

  $this->load->model('projectCl');
  $data=array();
  $data['year']=$this->projectCl->getYear($project_started);
  // passing data array will make $year available in view
  // I have made an assumption that the model only returns one year.
  // and is a string/integer
  $this->load->view('viewname',$data);
}
....

view
Code:
<table>
  <tr>
    <td>
      &lt;input type="text" name="year" size="5" value="&lt;?=$year?&gt;"&gt;
    </td>
  </tr>
</table>
#5

[eluser]CRHayes[/eluser]
I thought the point of MVC was the the controller 'talks' to both the view and the model...but the model and the view have no interaction (other than through the controller)? So you would pass info from the model to the controller, and then to the view?
#6

[eluser]wiredesignz[/eluser]
The view obtains data from the model and presents it to the user. The view represents the output of the application.

The view generally have free access to the model, but should not change the state of the model. Views are read only representations of the state of the model. The view reads data from the model using query methods provided by the model.

In MVC, The controller is NOT a Mediator between the view and the model. The controller does not sit in between the model and the view. Both the controller and the view have equal opportunity to access the model.

http://www.phpwact.org/pattern/model_view_controller
#7

[eluser]xwero[/eluser]
[quote author="wiredesignz" date="1195824698"]
In MVC, The controller is NOT a Mediator between the view and the model. The controller does not sit in between the model and the view. Both the controller and the view have equal opportunity to access the model.
[/quote]
That is the theory but i think in practice there will be a lot of code to your view. I'm thinking about an update form for instance. You get the data from your model to fill the fields. If you use raw model methods you will have to process the values so they can be read by the viewers of the form. And you should check if there was a false validation to fill the fields with the posted data and not the model data.

So i guess using models for forms is ruled out. Then we have views that only display database content. The raw model methods and processing to readable problem still exists. You could add a method to your model to process the raw data, ok solved. But what if you have to load multiple models to show the content.
Code:
$this->load->model('model1'); $this->load->model('model2'); $this->load->model('model3');
For me this looks like too much typing. I could live with
Code:
$this->load->models(array('model1','model2','model3'));
// or
$this->load->models('model1,model2,model3');
Maybe they should add plural methods to the loader library?

And as a final obstacle to the theory a lot of people want to keep the php code in the views to a bare minimum. It seems like a lot of developers don't trust designers enough to handle if (else) structures.
#8

[eluser]wiredesignz[/eluser]
None of that is really an issue.

Controllers do Validation and decide what the Model and View does after that.

Raw data can be modified by a Library or Helper.

Models are loaded by the Controller in any case not in a View. (And soon to be autoloaded in CI 1.5.5)

$multi_model = array('model_1','model_2','model_3');
foreach($multi_model as $blah) $this->load->model($blah);
#9

[eluser]xwero[/eluser]
It's not an issue i agree but i just wanted to show which consequences the 'Both the controller and the view have equal opportunity to access the model' theory has.

According to me equal opportunity includes loading a model in the view, but if i misinterpreted it please let me understand the meaning of the sentence.

if in your view you do something like this
Code:
$data = $this->model->getformdata();
You will have to add some kind of check if the validation returns errors
Code:
$data = (is_empty($this->validation->error_string))? $this->model->getformdata():$_POST;
#10

[eluser]wiredesignz[/eluser]
I only access properties of my Models not the query functions.

I store the results of a query made by a Controller within the Model which makes data available to both.

Code:
class Showroom_Model extends Model {

    function Showroom_Model()
    {
        parent::__construct();
    }
    
    var $resultsetA = array();
    
    function findInventory($limit=0, $offset=0, $sql='1', $orderby='sellprice,make,model,myear')
    {
        $query = $this->db->select('SQL_CALC_FOUND_ROWS *')->
                 from('stock')->
                 where($sql)->
                 limit($limit,$offset)->
                 orderby($orderby)->
                 get();
                
        $this->resultsetA = $query->result_array();
    }


I'm pretty sure you can't load a Model within a View so thats not an issue.




Theme © iAndrew 2016 - Forum software by © MyBB