Hi there,

It seems that Models associated via $hasMany are not done so via JOIN's. Rather, they are called as a separate query with the id of the calling model. Why is this?

This results in some strange behavior when certain conditions in the $fields array are defined.

e.g.
Product hasMany StockedItem

The StockedItem model has fields for buy_price and sell_price.

If I want to get a product with associated StockedItem entries, ordered by profit, I can do either of the folowing:

$data = ""
);
$this->Product->id = $data['id'
];
$modelData = $this->Product->StockedItem->findAll(null
, '*, (unit_price - buy_price) AS profit', 'profit DESC'
);

This will output the 'profit' field as an array, i.e.

Array
(
    [0] => Array
        (
            [StockedItem] => Array
                (
                    [item_id] => 10001
                    [supplier_id] => 1
                    [quantity] => 10
                    [unit_price] => 45.50
                    [buy_price] => 41.00
                    [created] => 2006-08-19 23:23:23
                    [modified] => 2006-08-19 23:23:23
                )

            [Product] => Array
                (
                    [id] => 10001
                    [description] => Product10001
                    [category_id] => 1
                    [brand_id] => 5
                    [reorder_level] => 4
                    [tax_percent] => 21
                    [image] => no-image.gif
                )

            [0] => Array
                (
                    [profit] => 4.50
                )

        )

    [1] => Array
        (
            [StockedItem] => Array
                (
                    [item_id] => 10001
                    [supplier_id] => 1
                    [quantity] => 10
                    [unit_price] => 45.50
                    [buy_price] => 42.00
                    [created] => 2006-08-19 23:23:23
                    [modified] => 2006-08-19 23:23:23
                )

            [Product] => Array
                (
                    [id] => 10001
                    [description] => Product10001
                    [category_id] => 1
                    [brand_id] => 5
                    [reorder_level] => 4
                    [tax_percent] => 21
                    [image] => no-image.gif
                )

            [0] => Array
                (
                    [profit] => 3.50
                )

        )

....
....etc
...
)


Alternatively I can set the 'fields' and 'order' entries in the hasMany array in the model and just issue a findAll on Product. This results in a slightly shorter output but the 'profit' field is still set as an array, i.e
.





Array
(
    [0] => Array
        (
            [Product] => Array
                (
                    [description] => Product10001
                    [category_id] => 1
                    [brand_id] => 5
                    [reorder_level] => 4
                    [tax_percent] => 21
                    [image] => no-image.gif
                )


            [StockedItem] => Array
                (
                    [0] => Array
                        (
                            [item_id] => 10001
                            [supplier_id] => 1
                            [quantity] => 10
                            [unit_price] => 45.50
                            [buy_price] => 41.00
                            [created] => 2006-08-19 23:23:23
                            [modified] => 2006-08-19 23:23:23
                            [0] => Array
                                (
                                    [profit] => 4.50
                                )

                        )

                    [1] => Array
                        (
                            [item_id] => 10001
                            [supplier_id] => 1
                            [quantity] => 15
                            [unit_price] => 46.00
                            [buy_price] => 42.50
                            [created] => 2006-08-19 23:25:41
                            [modified] => 2006-08-19 23:25:41
                            [0] => Array
                                (
                                    [profit] => 3.50
                                )

                        )

                    [2] => Array
                        (
                            [item_id] => 10001
                            [supplier_id] => 2
                            [quantity] => 20
                            [unit_price] => 46.50
                            [buy_price] => 46.00
                            [created] => 2006-08-19 23:26:17
                            [modified] => 2006-08-19 23:26:17
                            [0] => Array
                                (
                                    [profit] => 0.50
                                )

                        )

                )

        )
)

Can anyone tell me why this is happening?
Also, is there a better way to do what I'm trying to do? I would use custom SQL but I'm worried this wouldn't scale well if the DBMS was changed.

Cheers,

Psychie

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to