associations - CakePHP associating Users with one City -


i have user table , table cities, want associate each other. guess, city can have multiple users 1 user can linked 1 city.

i don't know how set tables up. when try $hasmany relation city , $hasone user getting error, because client_id field cannot found in cities table. cities table going fixed: no 1 ever change table. want store information city in table.

edit: here tables , models (kept simple):

clients { id, firstname, lastname, city_id } cities { id, name, some_other_data } 

and here models:

// user class client extends appmodel {     public $hasone = array(         'city'     ); }  // city class city extends appmodel {     public $usetable = 'cities';      public $hasmany = array(         'client'     ); } 

you need reverse relation; client 'belongsto' city. (i know, sounds illogical);

// user class client extends appmodel {     public $belongsto = array(         'city'     ); }  // city class city extends appmodel {     public $usetable = 'cities';      public $hasmany = array(         'client'     ); } 

your clients-table should contain city_id field make work (just looked @ table-definition , it's there, changing client-model should make work you).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -