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
Post a Comment