Hi I have a "download_categories" table with which I wish to implement an internal hierarchy, so that each row could be a child of another row in the same table. This will require me to create a "download_category_id" field to hold the id of the internal parent row. Nothing too challenging there. However, how to I implement this association in Cake?
I did a brief search in the group for a solution to this, and came up with one similar query by GregL (http://groups.google.com/group/cake-php/browse_thread/thread/1e33db0f4d501b0b/89cbf0a737644a03?q=foreign+key+in+same+table&rnum=10#89cbf0a737644a03), where he says: "What if I wanted to link to the same foreign table in two ways for two reasons? Like a User that has User 'friends' and User 'enemies' as a hasManu relationship??" I've tried the most obvious association setup: [snip] class DownloadCategory extends AppModel { var $name = 'DownloadCategory'; var $hasMany = array('DownloadCategory'); ... } [/snip] However, when I query data, my output array looks like this: [snip] [DownloadCategory] => Array ( [id] => 2 [download_category_id] => [name] => brochures [title] => Brochures [sort_order] => 20 [0] => Array ( [id] => 6 [download_category_id] => 2 [name] => brochures_passenger [title] => Passenger Vehicles [sort_order] => 10 ... [/snip] As you can see, the numbered data indexes are returned as part of the "parent" DownloadCategory, not the child. I would expect the array to be formatted with another 'DownloadCategory' key inside the parent 'DownloadCategory' key, with the data rows inside that, ie: hierarchy, so that each row could be a child of another row in the same table. This will require me to create a "download_category_id" field to hold the id of the internal parent row. Nothing too challenging there. However, how to I implement this association in Cake? I did a brief search in the group for a solution to this, and came up with one similar query by GregL (http://groups.google.com/group/cake-php/browse_thread/thread/1e33db0f4d501b0b/89cbf0a737644a03?q=foreign+key+in+same+table&rnum=10#89cbf0a737644a03), where he says: "What if I wanted to link to the same foreign table in two ways for two reasons? Like a User that has User 'friends' and User 'enemies' as a hasManu relationship??" I've tried the most obvious association setup: [snip] class DownloadCategory extends AppModel { var $name = 'DownloadCategory'; var $hasMany = array('DownloadCategory'); ... } [/snip] However, when I query data, my output array looks like this: [snip] [DownloadCategory] => Array ( [id] => 2 [download_category_id] => [name] => brochures [title] => Brochures [sort_order] => 20 [DownloadCategory] => Array [0] => Array ( [id] => 6 [download_category_id] => 2 [name] => brochures_passenger [title] => Passenger Vehicles [sort_order] => 10 ... [/snip] Without that extra key, you can't really foreach through the data without including the parent's attributes, and this way is in-line with how other association data is normally returned. Is this a bug? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
