> i would like to do the following. i have a table, lets call it > "softwaretype". in this table i want to store main classes (e.g. > Internet software), but also subclasses (e.g. Internet Browsers, Mail, > FTP...). i add an "parent_id" field which stores the id of the > "parent". if the type is a main class, the is NULL in it. > > how do i define associations? i made a hasMany association where i can > handle all "children" that worked. but how can i find the parent if i > have a subclass.
With something like this in the model: var $belongsTo = array( 'ParentSoftwareType' => array( 'className' => 'SoftwareType', 'foreignKey' => 'parent_id' ) ); This tells CakePHP that the parent ID links to the same model, SoftwareType (I'm guessing that's what you're calling it), and that the column parent_id tells it which parent it belongs to. It also specifies an arbitrary name (you can call it anything you like) to call the parent model - I've gone with ParentSoftwareType. This is so that when you read in a software type, you can get its information, and the information about its parent if it has one, without getting the two mixed up because they have different names. (In a controller, read in a software type that has a parent, and do a pr() on it to see what I mean.) > i think i have problems to understand associations at all. what if i > have no foreign key. i dont know how to make a key in a parent class > pointing to all children ??? Ah, that's more of a general database question. The answer is that you don't need to. CakePHP will automatically check the other table for anything whose parent_id matches, so you don't need to specify a child_id in the other table - that would be impossible as there could be a variable amount of children for each parent, and tables can't have a variable amount of columns. It may help to read up on some general relational database theory at this point to get all this clear in your mind before you use CakePHP too heavily, as it will make much more sense then. Hope that helps, Zoe. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---