简体   繁体   English

根据CakePHP中的条件从两个表中获取数据

[英]Fetching data from two tables depending upon condition in CakePHP

I have two controllers LeaveApplicationsController and EmployeesController. 我有两个控制器LeaveApplicationsController和EmployeesController。 In LeaveApplicationsController index action I'm displaying leave records of all employees. 在LeaveApplicationsController索引操作中,我正在显示所有员工的请假记录。 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. 我在离开应用程序表中将employee_id作为外键。 But I am not getting employee_id (which is id) from employees table. 但是我没有从employees表中获取employee_id(即id)。 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. 因此,在创建employee时,在创建该用户和新创建的用户的id之前,还将在unser_id字段下的employees表中存储该id。 So user_id is foreign key in employees table and uniquely identifies the employee. 因此,user_id是employees表中的外键,并且唯一地标识了employee。

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. 我还使用$ uses在LeaveApplicationsController中加载Employee模型。 Thanks. 谢谢。

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-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-all

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM