i have 2 models linked in hasmany/belongsto relationship. here hasmany definition:
//table hr_emp_ids. each employee can have many hr cases. public $hasmany = array( 'hrcase' => array( 'classname' => 'hrcase', 'foreignkey' => 'emp_user_id' ) );
and here belongsto definition:
//table hr_cases. each hr case owned employee. public $belongsto = array( 'hrempid' => array( 'classname'=> 'hrempid', 'foreignkey' => 'emp_user_id' );
the controller view dead simple:
public function view($id = null) { $this->hrcase->id = $id; if (!$this->hrcase->exists()) { throw new notfoundexception(__('invalid case id')); } $options = array('conditions' => array('hrcase.' . $this->hrcase->primarykey => $id)); $this->set('case', $this->hrcase->find('first', $options)); }
all i'm trying display hire_date , ssn hr_emp_ids table based on hr_cases.emp_user_id = hr_emp_ids = emp_user_id. here view code:
<tr> <td><strong>employee: </strong><br><?php echo h($case['hrcase']['full_name']); ?></td> <td><strong>date of hire: </strong><br><?php echo h($case['hrempid']['hire_date']); ?></td> <td><strong>ssn: </strong><br><?php echo h($case['hrempid']['ssn']); ?></td>
table structures:
desc hr_emp_ids; +-------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-------------+-------------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | hire_date | date | yes | | null | | | ssn | varchar(11) | no | | null | | | emp_user_id | int(11) | no | | null | | +-------------+-------------+------+-----+---------+----------------+ desc hr_cases; (truncated) +--------------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +--------------------+-------------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | emp_user_id | int(11) | no | | null | |
nothing hrempid model display. i'm not sure i'm doing wrong here. i've made associations dozens of times no trouble. might missing?
i don't see immediate reason why associated data isn't showing. find code here
$options = array('conditions' => array('hrcase.' . $this->hrcase->primarykey => $id)); $this->set('case', $this->hrcase->find('first', $options));
looks simplified using getbyid()
, because condition in $options
checking $id
. it's worth adding containable behavior
hrcase model , setting second argument of getbyid()
true. should allow specify associated data returned.
edit:
in hrcase model setting $belongsto
relationship foreign key emp_user_id
, not primary key in hr_emp_ids
. why associated data isn't being returned. need set this:
public $belongsto = array( 'hrempid' => array( 'foreignkey' => false, 'conditions' => array( 'hrempid.emp_user_id = hrcase.emp_user_id' ), ) );
see more info how associate model in cakephp fields not named convention?
Comments
Post a Comment