Tuesday, 15 April 2014

php - Link the same model twice to another model in CakePhp -



php - Link the same model twice to another model in CakePhp -

it's basic problem, can't wrap head around how solve properly.

in application, i've got users have multiple addresses different purposes. example, user can have address , invoiceaddress. want store both addresses in same table (the address model) , assign them 1 user.

the respective models like:

address:

public $belongsto = array( 'address' => array( 'classname' => 'address', ), 'invoiceaddress' => array( 'classname' => 'address', ), );

and user:

public $hasone = array( 'address' => array( 'classname' => 'address', ), 'invoiceaddress' => array( 'classname' => 'address', ), );

when user have 1 address, i'd add together user_id foreign key address model , done it. obviously, has done differently cakephp able distinguish between address , invoice address.

in mind solution adding address_id , invoice_address_id user table, can't seem working in way.

if want add together in user table related fields improve add together them in next form address_user_id , invoice_address_user_id. must have belongsto relation in user model looks like:

public $belongsto = array( 'address' => array( 'classname' => 'address', 'foreignkey' => 'address_user_id', ), 'invoiceaddress' => array( 'classname' => 'address', 'foreignkey' => 'invoice_address_user_id', ), );

so when execute next command $this->user->find('all'); result be

$result = array(0 => array('user' => array(...), 'address' => array(...), // bring 1 address row 'invoiceaddress' => array(...) // bring 1 address row ), 1 => ... 2 => ... );

then in address model relationships must add together are:

public $hasone = array( 'user' => array( 'classname' => 'user', 'foreignkey' => 'address_user_id', ), 'invoiceuser' => array( 'classname' => 'user', 'foreignkey' => 'invoice_address_user_id', ) );

php cakephp cakephp-2.3

No comments:

Post a Comment