you might want to peek into the tree behavior - it makes multi-level stuff much easier (haven't used the behavior personally, but have used MPTT quite a bit)
As for your models: You mentioned your states and countries have the city foreign key, which I assume is stricly for their capital. This is a belongsTo; when the key is present in the CURRENT table.. state: $belongsTo( 'CapitalCity' => array( 'className' => 'City'), 'Country' ); // city_id on state maps to the capital, country_id maps to the country $hasMany('City'); // state_id on the cities table map to the current state (gets all the cities) country: $belongsTo('City') // city_id on country maps to capital (gets the capital city) $hasMany( 'State') // country_id on the states table maps to the current country (gets all the states); (note, if you need the whole chain, you can use containable - it would let you specify the link such as: $this->contain('City', 'State' => 'CapitalCity', 'City') // this gets the countries capital city, states and that states capital and cities.. cool, eh? ) city: $belongsTo = array('State') // city has state_id, which gets it's parent state ( again, you can use containable to get further into state, like get the state's country and ITs capital, etc etc) The use of containable is much more flexible than recursive, as it lets you specify the relationships. http://book.cakephp.org/view/1323/Containable ) hope it helps, dan On Aug 5, 8:43 am, DerBjörn <b.unkh...@googlemail.com> wrote: > Hi, > > i have three tables: countries, states and cities. > > Every country has its capital city, so does every state. > 2 relations are very obvious: > > state belongsTo country > city belongs to state > > Now i have a problem to realize the relations referring to the capital > cities. > In my opinion there are two hasOne relations: > > state hasOne city > country hasOne city > > Both tables (states and countries) has a column names city_id as > foreign_key, but if i use two hasOnes like > > [code] > var $hasOne = array( > 'CapitalCity' => array( > 'className' => 'City', > 'foreignKey' => 'city_id', > 'conditions' => '', > 'fields' => '', > 'order' => '' > ) > ); > [/code] > > i get following error message: > [quote]Warning (512): SQL Error: 1054: Unknown column > 'CapitalCity.capitalcity_id' in 'on clause' [CORE/cake/libs/model/ > datasources/dbo_source.php, line 681][/quote] > > [quote]Query: SELECT COUNT(*) AS `count` FROM `countries` AS `Country` > LEFT JOIN `cities` AS `CapitalCity` ON (`CapitalCity`.`city_id` = > `Country`.`id`) WHERE 1 = 1 [/quote] > > Actually it has to be (`CapitalCity`.`id` = `Country`.`city_id`) to > make sense. What i did wrong? > In a forum i asked i ve been told that to realize the capital city > connections i have to use 2 belongsTo relations, but it doesn't make > any sense to me. > > Somebody can enlight me? :) > Thanks! Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com 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?hl=en