My schema is quite simple-
Each user has an id and name.
There are jobs which belong to a user. Actual schema-
CREATE TABLE `users` (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`name` varchar(20)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `jobs` (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`user_id` INT UNSIGNED ,
`type` int,
FOREIGN KEY (user_id) references users(id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
My JobsController looks like this-
<?php
class JobsController extends AppController {
public $scaffold;
public $name = 'Job';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
And UsersController as-
<?php
class UsersController extends AppController {
public $scaffold;
public $name = 'User';
public $hasMany = array(
'Job' => array(
'className' => 'Job',
'foreignKey' => 'user_id'
)
);
}
Problem:
When I visit http://hostname/Jobs/add I can see a drop-down for user but its empty.
I’ve created few users which I can see from http://hostname/Users/
Why is drop-down empty? I am able to add jobs but when I view them, user field is shown empty.
Associations belong in the Model, not the controller.
In app/Model/User.php:
In app/Model/Job.php