![]() |
Get image url from model good practice? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Get image url from model good practice? (/showthread.php?tid=63496) Pages:
1
2
|
Get image url from model good practice? - sebastianvirlan - 11-06-2015 Is this method a good practice? Controller: PHP Code: $produse = $this->produse->entries_by_limit((($page-1) * $config_pagina['per_page']), Model: PHP Code: function entries_by_limit($start, $limit, $order_by = "order by id_produs asc", $rez) { and view PHP Code: <img class="primary-image" src="<?= $produs->nume_imagine ?>" alt=""> Should I use any helper or is ok like this? RE: Get image url from model good practice? - sebastianvirlan - 11-10-2015 I think a good start would be OOP, so I started: PHP Code: class Product The problem is I dont't know where to call and how to call. In model? In controller? RE: Get image url from model good practice? - Martin7483 - 11-10-2015 In the world of MVC each request is routed to a Controller. You could ignore the M and V completely and do everything in your controller. But you will quickly see that this means duplicating a lot of code. So you start to use Models for Database communication. In your models you perform some logic to the DB results and return it to your controller. Again you will start to notice code duplication. So you start using helpers for common functions and libraries for more complex data handling. And thus you may start to use a business layer for your models. I would say your Product class is a library and you pass a data package to this class. So how you call this could be 1. Controller --> request data from your model and return to Controller 2. Controller --> pass the data to the Product class and return to Controller 3. Controller --> pass the data to the view Hopes this helps you out RE: Get image url from model good practice? - sebastianvirlan - 11-10-2015 (11-10-2015, 01:16 AM)Martin7483 Wrote: In the world of MVC each request is routed to a Controller. You could ignore the M and V completely and do everything in your controller. But you will quickly see that this means duplicating a lot of code. So you start to use Models for Database communication. In your models you perform some logic to the DB results and return it to your controller. Again you will start to notice code duplication. So you start using helpers for common functions and libraries for more complex data handling. And thus you may start to use a business layer for your models. Thank you. Helped a lot, see my last post with my class and tell me if I can refactor something. Then in my controller I have: PHP Code: $best_offer = new Product($this->products->best_offer(), 270); Is this the best practice MVC and OOP? RE: Get image url from model good practice? - sintakonte - 11-10-2015 i think your way of thinking is on the right way but needs to get organized Your Problem is that you need sometimes methods or variables where it seems they don't belong to a model or a controller. i try to explain it on your current example First of all we need to adapt the MVC principle into a MVOC term. make a folder in your application directory called objects the next thing is you have to make sure that your objects get autoloaded open your hooks.php in application/config/ and write the following down PHP Code: $hook['pre_system'][] = array( after that create a class named AppAutoLoadObjects.php in your applications/hooks/ directory this class should look like PHP Code: class AppAutoLoadObjects { now to your object create a Product_Object in the application/objects/ directory (be aware in order to avoid name collision add always the _Object Suffix to your Objects) PHP Code: class Product_Object your model should look like PHP Code: public function entries_by_limit($start, $limit, $order_by = "order by id_produs asc", $rez) your controller function could be PHP Code: public function listProducts() and finally your view PHP Code: <?php with this approach everything is on its own place and if you want to manipulate product data you can do this in your own object and you could be sure - every time a product gets instantiated you have the right data and methods. RE: Get image url from model good practice? - sebastianvirlan - 11-10-2015 I am not very convinced about this method because I must do a foreach in models. And let me tell you why: in Products model I may have: select_by_id select_by_slug select_by_category_id_by_limit select_by_category_slug_by_limit entries_by_limit And think that I must make a foreach inside all this models. A lot of duplicate foreach .... RE: Get image url from model good practice? - sintakonte - 11-10-2015 I don't understand what you mean in your example you have a Model which uses exactly the foreach approach Quote:foreach($result->result() as $single) {... and if you have a function where you know exactly that you expect only one result than you can write something like PHP Code: $result = $this->db->get(); RE: Get image url from model good practice? - sebastianvirlan - 11-10-2015 Yes my Model used the foreach and because of this I posted ... Yes, sometimes I have result and sometimes row with 1 result. RE: Get image url from model good practice? - sintakonte - 11-10-2015 foreach loops in a model are perfectly fine and if you are concerned about performance issues because you've to use this foreach in one of your views too - don't worry its absolutely no problem RE: Get image url from model good practice? - sebastianvirlan - 11-11-2015 (11-10-2015, 02:07 PM)dubefx Wrote: @sintakonte very interesting aproach, i learned something new today, so thanks. But CI3 already have this functionality custom_result_object and custom_row_object and can be used in your example model like: So is still need for hook? Please post your full final code. I don't see where you pass $rez. |