Hi,

I am trying to understand the way that cake nests related models when
doing a find using containable. This is my situation:

Order has many Sales
Sale belongs to Product
Sale belongs to Order
Product has many ProductImage
Product hasAndBelongsToMany Category
Category HABTM Product
ProductImage belongs to Product
Product has many Sale

when I do this:

            $this->Product->contain('ProductImage',  'Category');
            $product = $this->Product->findById(1);

It gives me an array where product fields are in an array called
'Product' and 'ProductImage' fields are in an array called
'ProductImage', and these 2 arrays sit at the same level side by side.
eg:

array(
    [Product] => Array
        (
            [id] => 1
            [code] => S1002
            [name] => Bunting
            [price] => 3.99
         )

    [ProductImage] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [product_id] => 1
                    [filename] => bunting.jpg
                )

        )

    [Category] => Array
        (
            [0] => Array
                (
                    [id] => 45
                    [name] => Sew
                    [parent_id] =>
                    [lft] => 1
                    [rght] => 2
                )

        )
)

But when I do this:

            $contain = array(
                'Sale.Product',
                'Sale.Product.ProductImage',
            );

            $order_id = 28;

            $order = $this->Order->find('first', array('contain'=>$contain,
'conditions'=>array('Order.id'=>$order_id)));

Data for models associated with Product are nested within the Product
array, eg:

Array
(
    [Order] => Array
        (
            [id] => 28
            [created] => 2011-03-13 18:01:54
            [paid] => 0
        )

    [Sale] => Array
        (
            [0] => Array
                (
                    [id] => 29
                    [order_id] => 28
                    [product_id] => 1
                    [qty] => 1
                    [amount] => 3.99
                    [Product] => Array
                        (
                            [id] => 1
                            [code] => S1002
                            [name] => Bunting
                            [price] => 3.99

                            [ProductImage] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1
                                            [product_id] => 1
                                            [filename] => bunting.jpg
                                        )
                                )
                        )
                )
        )
)

(note: I have deleted some fields from this output so that it does not
go on for pages and pages - it is the nesting of arrays I am asking
about)

Why is the nesting of related models different in these 2 examples? I
want to pass the product to 1 single helper from both these
situations, but it gets messy because of this inconstancy.

I have also tried using nested arrays to define the contain parameter
instead of the dot syntax, but it is the same result.

Thanks very much for your help.


-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to