how can i use two associations at once in cakephp -
i'am using cakephp 2.x have 2 tables users , jobs in database, in users table save 2 types of users job candidate , company in situation found self facing 2 relations between tables :
- (1,n)users can add (0,1)jobs. (user role here company)
- many (1,n)users can apply many (1,n)jobs. ( candidate)
can use belongsto , habtm(hasandbelongstomany) @ once between 2 models ? :
job model
class job extends appmodel{ public $belongsto=array( 'user'=> array( 'classname' => 'user', 'foreignkey' => 'user_id', 'countercache'=>true, 'fields'=>array('id','username','role'), ) ); public $hasandbelongstomany=array('user'); public $usetable = 'jobs';
user model
class user extends appmodel{ public $hasandbelongstomany=array('job');
how retrieve data depending on first or second context 1- show jobs of company. 2- show users applies.
you can this, 2 associations need different aliases. refer user
appling job
applicant
:-
class job extends appmodel { public $belongsto = array( 'user'=> array( 'classname' => 'user', 'foreignkey' => 'user_id', 'countercache' => true, 'fields' => array('id', 'username', 'role'), ) ); public $hasandbelongstomany = array( 'applicant' => array( 'classname' => 'user' ) ); }
note need tell cake applicant
uses user
model via classname
property. belongsto
association don't need specify classname
user
same alias same model name have left in clarity.
you don't need specify $usetable = 'jobs
implied using correct naming conventions.
Comments
Post a Comment