Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] call MODEL functions in the VIEW, really bad idea?
#1

[eluser]basementDUDE[/eluser]
In order to avoid writing complex sql querys, I decide to call a function in the model inside the view.
Is that a really bad idea?
#2

[eluser]kgill[/eluser]
Generally if you find yourself asking is this a bad idea, you know it is.
#3

[eluser]basementDUDE[/eluser]
[quote author="kgill" date="1250061340"]Generally if you find yourself asking is this a bad idea, you know it is.[/quote]
I know it is not a good practice, but it gets the job done easily.
I mean they let me use model functions in the view that must have a reason right?
#4

[eluser]Maglok[/eluser]
They let you because it's a best practice to not do it. That means it is up to you to honour the idea.

How would writing the 'complex sql queries' in the Controller and passing it to the View be harder? Smile
#5

[eluser]basementDUDE[/eluser]
[quote author="Maglok" date="1250086112"]They let you because it's a best practice to not do it. That means it is up to you to honour the idea.

How would writing the 'complex sql queries' in the Controller and passing it to the View be harder? Smile[/quote]


I need to join 24 tables to show some data which is very hard for me to get it right.
Therefore, instead of writing a big sql query in the moduel, I discover that I could writing some small model functions and call them in the view to do the same thing(I could not pass whole bunch result set though the controller, the foreach loop in the view will mass things up).

My only concern is that the author of CI could set more restrict rule in the future update to break my code.
#6

[eluser]Maglok[/eluser]
You usually do not upgrade your CI version while developing an application, but when you start a new one.

Also I still don't see why you can't do what you are saying in the controller, cram it all in the $data array and use it in the view.
#7

[eluser]basementDUDE[/eluser]
[quote author="Maglok" date="1250095794"]You usually do not upgrade your CI version while developing an application, but when you start a new one.

Also I still don't see why you can't do what you are saying in the controller, cram it all in the $data array and use it in the view.[/quote]


ok, I need to list a product X. It has a PK key xid. In order to show the full product, 24 db tables has been created to store varies options.

Currently I use a foreach loop in the view to generate that table.

But here is the problem:
for example I need two field that shows how many images/video are linked to that product X.
I cannot do it. because I need to get the xid first then do a query to find out. Theoretically, I could combine whole bunch querys to a big one.
Call that function in controller and pass the result. However, it just too complex and risky for get it wrong.

Instead, I can just call some small model function in the view such as get_num_image(xid) for field1, get_num_video(xid) for field2 to get job done. Its not pretty, but I find out its much easier to work with.

Anyway, I guess that will be always a trade off in programming right?
#8

[eluser]Maglok[/eluser]
I'd probably go with making a big query and putting it in my model. You could still move all the small bits to the controller though.

If you simply get a set:
Code:
$data['personen'] = $this->model_persoon->get_all();

Or you need to process it first before sending it to the view like this:
Code:
foreach($query->result() as $row) {
                    $karakter_options[$row->id] = $row->naam;
                }

Regardless, you can access them in the view when gotten from the model in the Controller.
#9

[eluser]davidbehler[/eluser]
Why not do it all in the model?
Code:
//in your controller
function show_product_list()
{
  $data = array();
  $data['product_list'] = $this->Model_name->get_product_list()
  $this->load->view('my_view_name', $data);
}
//in your model
function get_product_list()
{
  $query = $this->db->get('product');
  $return = array();
  foreach($query->result_array() as $key => $product)
  {
    $return[$key] = $product;
    $return[$key]['num_image'] = $this->get_num_image($product['id']);
    $return[$key]['num_video'] = $this->get_num_video($product['id']);
    // and so on
  }
  return $return;
}

//in your view
print_r($product_list); // or whatever you want to do
#10

[eluser]Colin Williams[/eluser]
Here's a simple process to handle every JOIN query you ever do.

1. Join all tables in one go. Get the whole lot.
2. Using a primary key, or identifier of some sort, iterate over the result and group multiple values. Sometimes you need to iterate several times if you are build a complex nest.

That's it. Really. print_r() and var_dump() are your best friends here. Start by looking at the $query->result() (or similar) structure and figure out how you are going to wrangle that beast.




Theme © iAndrew 2016 - Forum software by © MyBB