AR HAS_MANY relation destroys eager loading?

I have this AR query:


$products = Products::model() -> with('productDetails') -> findAll();

And the relation between ‘Products’ and ‘ProductDetails’ is that a product can have many details, since these could be described in a vary of languages. So a product HAS_MANY product details. In the database table the product details has a foreign key to a product, so thats a one to one relationship.

So the query above gives me all the products BUT it only gives me the first hit on the productDetails.

When iterating thru all the products with $product as the product record, this code gives me the same result each time:


echo $product -> productDetails[0] -> title;

But what I want is of course the corresponding productDetails for each product…

I think this post contains the answer (nested foreach).

Edit:

Actually it adds a third level so might look complicated at first sight.

/Tommy

Even if it didn’t help me directly I managed to solve it, the result looks like this:




<?php foreach ($products as $currentProduct => &$product): ?>

...

<?php echo $products -> productDetails[$currentProduct] -> title; ?>

...

<?php endforeach; ?>

What I get when I do:


$products = Products::model() -> with('productDetails') -> findAll();

is a associating array of productDetails where each productDetail belong to a product.