DMZ object in foreach - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: DMZ object in foreach (/showthread.php?tid=33679) |
DMZ object in foreach - El Forum - 09-04-2010 [eluser]frist44[/eluser] I'm trying to iterate through an object in a view. So my controller has: Code: $o = new Order(); In my view: Code: foreach ($obj as $order){ But $order->product->id, doesn't show up. if I do : foreach ($obj->product as $product), I can get the info out. Why isn't the deeper relationships passed through with the foreach variable? Instead, it seems I have to dig through each one on the loop: Code: foreach ($obj as $order){ Controller: Code: $o = new Order(); DMZ object in foreach - El Forum - 09-05-2010 [eluser]slowgary[/eluser] In your controller, you're assigning $data['obj'] = $o, so using a foreach() in your view is not necessary, since $obj is not an array. foreach() doesn't chop any of your data out, it just iterates through array elements, assigning their value to a temporary variable for you to display or make calculations on. It looks like maybe you'd want to do something like this: Code: //controller I wouldn't recommend creating new Product objects in your view, especially if these make database calls. The general idea with MVC is that your controllers do that work. I hope this helps. DMZ object in foreach - El Forum - 09-05-2010 [eluser]frist44[/eluser] I actually never wanted to call that product from the view, plus it created a select sql statement foreach in the loop and that seemed excessive. For some reason I'm still having trouble getting it out. I'm trying to essentially get the following sql statement out. Notice that there's only one product, for each order, and the product is joined to a sheet as well. So i'd like to have all 3 levels accessible in the view: Code: SELECT `orders`.`id`, `orders`.`quantity`, `orders`.`reconciled`, `orders`.`created`, `products`.`finish`, `sheets`.`geometry` So each order will have one product, so it doesn't necessary to have to loop foreach on each product within the foreach of the order right? DMZ object in foreach - El Forum - 09-05-2010 [eluser]slowgary[/eluser] I'm confused Just to make sure I understand... based on your query, it looks like you're selecting all orders. This would lead me to believe that you're attempting to show many orders in your view. Also, there is one product and one sheet (whatever that is per order. The query looks fine, however it appears that you're only creating one order() object in your controller to pass to your view. I prefer to work with array, since I grew up in functional land, but if you plan to pass a collection of order objects to your view, I would expect to see something like this in your controller: Code: $this->load->model('order_model'); Code: <?php foreach($orders as $order): ?> Does that make sense for what you're trying to do? DMZ object in foreach - El Forum - 09-05-2010 [eluser]frist44[/eluser] Nevermind. I realized there's an include_related function in DMZ to help with this. |