0

I’m changing a relationship, in a CakePHP web application, from a belongsTo to a HABTM.

Originally we had a department table and a user table. The department table could contain many users; but a user could only belong to one department.

Now we want the ability for a user to belong to multiple departments. I created an intermediary table called departments_users and I filled it full of data.

I've modified the user model, and now I'm getting this error in the users view:

Notice (8): Undefined index: Department [APP/views/users/index.ctp, line 25]

This is the line of code in the view that is giving the error:

<?php 
echo $this->Html->link($user['Department']['name'], 
array('controller' => 'departments', 'action' => 'view', $user['Department']['id'])
); 
?>

The above code uses the HtmlHelper to create a link with the name of the department (as the link text) and then supplies the department Id as a part of the link.

This is the code I've removed and added in the user model. First I removed this code from the $belongsTo relationship:

'Department' => array(
    'className'  => 'Department',
    'foreignKey' => 'department_id'
),

Then I added this code in the $hasAndBelongsToMany relationship:

'Department' => array(
    'className'             => 'Department',
    'joinTable'             => 'departments_users',
    'foreignKey'            => 'user_id',
    'associationForeignKey' => 'department_id',
    'unique'                => true
)

Could anyone tell what I could do to make this work? Thanks.

Joe
  • 8,251
  • 3
  • 18
  • 23
  • Thanks to this post: http://stackoverflow.com/questions/3667997/how-to-display-results-from-a-habtm-relationship-in-a-view I realized I probably needed a foreach loop in my view code, to display my HABTM relationship. So I changed my view code to this: Html->link($department['name'], array('controller' => 'departments', 'action' => 'view', $department['id']) ); ?>   But I still get the same error; followed by an "Invalid argument supplied for foreach()" – Joe Dec 31 '14 at 15:39
  • Your comment is heading in the right direction. But we know from the error messages that there is no `$user['Department']` defined. What I would do is to use `print_r($user)` - or `var_dump` or whatever - and see a) if your code is actually fetching the list of the users departments, and b) in what format (so you can work out what you should be looping through). – Annabel Jan 02 '15 at 18:39
  • Thanks for your response. I did cake's debug($user); and sure enough, Department is not there. I reverted the changes (with git) and found that it was there before, of course. So somehow my new HABTM relationship isn't being picked up by the controller. The controller uses paginate, and calls the view. I’m sure I need to do something different in paginate for HABTM. – Joe Jan 02 '15 at 19:19
  • Have you tried adding `'recursive' => 1` to the paginator settings? See [CakePHP documentation](http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#pagination-with-get-parameters) for details. – Annabel Jan 02 '15 at 19:36
  • Thanks setting `'recursive' => 1` was it! (It had been set to 0 before). It works now! If you want to put your comment as the answer, I’ll accept it. Thanks again! – Joe Jan 02 '15 at 21:59

0 Answers0