Welcome Guest, Not a member yet? Register   Sign In
DMZ object in foreach
#1

[eluser]frist44[/eluser]
I'm trying to iterate through an object in a view. So my controller has:

Code:
$o = new Order();
        $o->get();
        $o->product->get();

In my view:

Code:
foreach ($obj as $order){
                echo '<tr>';
                echo '<td>' . $order->id . '</td>';
                echo '<td>' . $order->product->id . $order->product->finish . '</td>';
                echo '<td>' . $order->quantity . '</td>';
                echo '<td>' . $order->reconciled . '</td>';
                echo '<td>' . mysql_datetime_to_php($order->created) . '</td>';                
                echo '</tr>';
            }

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){
                $p = new Product();
                
                echo '<tr>';
                echo '<td>' . $order->id . '</td>';
                echo '<td>' . $p->get_by_id($order->product_id)->geometry . '-' . $p->get_by_id($order->product_id)->finish . '</td>';
                echo '<td>' . $order->quantity . '</td>';
                echo '<td>' . show_reconcile($order->reconciled) . '</td>';
                echo '<td class="textleft">' . mysql_datetime_to_php($order->created) . '</td>';                
                echo '</tr>';
            }

Controller:

Code:
$o = new Order();
        $o->get_iterated();
        $o->product->get();      
      
        $data['obj'] = $o;
#2

[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
$o = new Order();
$o->get_iterated();
$o->product->get();

$data['order'] = $o;


//view
<td>&lt;?php echo $order->id; ?&gt;</td>
<td>
     <ul>
&lt;?php foreach($order->product as $product): ?&gt;
          <li>&lt;?php echo $product->name; ?&gt;</li>
&lt;?php endforeach; ?&gt;
     </ul>
</td>

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.
#3

[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`
FROM (`orders`)
JOIN `products` ON `orders`.`product_id` = `products`.`id`
JOIN `sheets` ON `products`.`sheet_id` = `sheets`.`id`
ORDER BY `orders`.`id` DESC

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?
#4

[eluser]slowgary[/eluser]
I'm confused Smile

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 Smile 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');
$orders = $this->order_model->get_all_orders();
foreach($orders as $order)
{
     $temp = new Order();
     $temp->set_properties($order);
     $data['orders'][] = $temp;
}

$this->load->view('orders', $data);
Then in your view:
Code:
&lt;?php foreach($orders as $order): ?&gt;
<tr>
     <td>&lt;?php echo $order->id; ?&gt;</td>
     <td>&lt;?php echo $order->product_name; ?&gt;</td>
     <td>&lt;?php echo $order->something_else; ?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;

Does that make sense for what you're trying to do?
#5

[eluser]frist44[/eluser]
Nevermind. I realized there's an include_related function in DMZ to help with this.




Theme © iAndrew 2016 - Forum software by © MyBB