I have two controllers LeaveApplicationsController and EmployeesController. In LeaveApplicationsController index action I'm displaying leave records of all employees. It is showing fine but what I need is to show only login employees leaves to that employee. I have employee_id as foreign key in leave_applications table. But I am not getting employee_id (which is id) from employees table. I also have users table and each user has one employee associated with it. So when employee is created, before that user is created and newly created user's id is also stored in employees table under unser_id field. So user_id is foreign key in employees table and uniquely identifies the employee.
我有两个控制器LeaveApplicationsController和EmployeesController。在LeaveApplicationsController索引操作中,我正在显示所有员工的休假记录。它显示正常,但我需要的是只显示登录员工离开该员工。我在leave_applications表中将employee_id作为外键。但我没有从employees表中获取employee_id(即id)。我也有用户表,每个用户都有一个与之关联的员工。因此,在创建员工时,在创建该用户之前,新创建的用户ID也存储在unser_id字段下的employees表中。因此,user_id是employees表中的外键,并唯一标识该员工。
This is my LeaveApplicationsController.php code:-
这是我的LeaveApplicationsController.php代码: -
public function index()
{
$userId = $this->Auth->user('id'); //gets current user's id
$this->LeaveApplication->recursive = 0;
$leavereqs = $this->set('leaveApplications', $this->Paginator->paginate());
$employeeId = $this->Employee->find('all', array('fields'=>array('id'), 'conditions'=>array('Employee.user_id'=>$userId))); // code for getting emp id
return $this->LeaveApplication->find('all', array('conditions'=>array('LeaveApplication.employee_id'=>$employeeId))); //code for fetching current employee record
}
But this shows error. Please tell me where am I doing it incorrect? I'm also using $uses to load Employee model in LeaveApplicationsController. Thanks.
但这显示错误。请告诉我,我在哪里做错了?我还使用$ uses在LeaveApplicationsController中加载Employee模型。谢谢。
1 个解决方案
#1
0
public function index()
{
$userId = $this->Auth->user('id'); //gets current user's id
$this->LeaveApplication->recursive = 0;
$leavereqs = $this->set('leaveApplications', $this->Paginator->paginate());
$employeeId = $this->Employee->find('list', array('fields'=>array('id'), 'conditions'=>array('Employee.user_id'=>$userId))); // code for getting emp id
return $this->LeaveApplication->find('all', array('conditions'=>array('LeaveApplication.employee_id'=>$employeeId))); //code for fetching current employee record
}
find('all') will return array with index Employee and id
find('all')将返回索引为Employee和id的数组
Imp links: find('all'), find('first') and find('list')
Imp链接:find('all'),find('first')和find('list')
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-first
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-all
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list
#1
0
public function index()
{
$userId = $this->Auth->user('id'); //gets current user's id
$this->LeaveApplication->recursive = 0;
$leavereqs = $this->set('leaveApplications', $this->Paginator->paginate());
$employeeId = $this->Employee->find('list', array('fields'=>array('id'), 'conditions'=>array('Employee.user_id'=>$userId))); // code for getting emp id
return $this->LeaveApplication->find('all', array('conditions'=>array('LeaveApplication.employee_id'=>$employeeId))); //code for fetching current employee record
}
find('all') will return array with index Employee and id
find('all')将返回索引为Employee和id的数组
Imp links: find('all'), find('first') and find('list')
Imp链接:find('all'),find('first')和find('list')
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-first
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-all
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list