I do have two tables which are employee and loan
When I enter loan for particular user I am picking user employee_id and loan amount.
// loan form
https://imgur.com/VH6hflw
these details stores in loan table. As well as I have stored some employees details already in employee table like account number, employee official id, userpic.
When I submitting the loan form I am getting employee unique id. I will store the employee unique id to loan field called loan_employee_id.
My problem is When I retrieve the data from the loan table I will get loan amount but some more field are needs from employee table. So I have joined with two tables which are employee and loan.
my db structure https://imgur.com/XYxGjVH
public function fetchLoan()
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('loan','employee.emp_id = loan.employee_loan_id');
$this->db->where(array('loan.employee_loan_id' => "employee.emp_id") );
$query= $this->db->get();
return $query->result_array();
}
Here where condition should match with employee_loan_id from the loan table and emp_id from employee table
How Do i join the tables with above condition, where conditions both valion getting from the database.
First of all, the corresponding MySQL query would be:
SELECT * -- Put field names which are required
FROM loan
INNER JOIN employee ON employee.emp_id = loan.employee_loan_id
WHERE loan.employee_loan_id = 12; -- Consider we're fetching records for emp_id = 12
Corresponding code would be:
-- Pass a parameter for employee id for which you want to get loan details.
public function fetchLoan($empId)
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('loan', 'employee.emp_id = loan.employee_loan_id');
$this->db->where('employee.emp_id', $empId);
$query = $this->db->get();
return $query->result_array();
}
Please pass employe_unique_id on this based data geting
public function fetchLoan($employe_unique_id)
{
$this->db->select('*');
$this->db->from('employee');
$this->db->join('loan','employee.id = loan.employee_id');
$this->db->where('employee.id',$employe_unique_id);
$query= $this->db->get();
return $query->result_array();
}
Related
I have a pivot table. The name is users_banks table. The table have field id, user_id, bank_id, status, account_name and account_number
Now, the pivot have 3 records
I display contain from pivot table using code like this :
$user_id = auth()->user()->id;
$user = $this->user_repository->find($user_id);
$account = array();
foreach($user->banks as $bank) {
$account[] = $bank;
}
dd($account);
The result display all record in the form of array. There exist value of field user_id, bank_id, status etc. But I don't find value of id
How can I find id from pivot table?
If the relationship is defined correctly you can use:
$bank->pivot->id
but make sure the user belongsToMany(Bank::class) and the bank belongsToMany(User::class) or else it wont work.
I have 2 tables that i want to join & show the name of user's role. here's the situation
My 2 tables are users_mlh & user_roles_mlh
on the role column of users_mlh table i'm storing the ID of user role, user_roles_mlh contains the name & id of user role. what i want to do is show the name of the user role in my view.
my tables as follows.
i have tried this in my model
$this->db->select('*');
$this->db->from('user_roles_mlh');
$this->db->join('users_mlh', 'users_mlh.role = user_roles_mlh.id');
$this->db->where('users_mlh.role = user_roles_mlh.id');
$query = $this->db->get();
return $query->result_array();
but from above i get something like this
at the moment it lists all user level not the role of each individual user
Case 1 : If you Directly want to Access all data without using where condition
$this->db->select( "*" );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
Case 2: With Where Condition and specific column data
$this->db->select( 'user_roles_mlh.role_type,users_mlh.name' );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$this->db->where('users_mlh.id =',1);
And finally get the results by
$query = $this->db->get();
return $query->result();
No need to write separate query for getting role name. Join Roles table when you fetching users data..
$this->db->select('users_mlh.*,user_roles_mlh.role_type');
$this->db->from('users_mlh');
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$query = $this->db->get();
return $query->result_array();
Try this.
If I use your tables, you should have the following request:
SELECT users_mlh.*, JOIN user_roles_mlh.role_type
FROM users_mlh -- list all users
LEFT JOIN user_roles_mlh -- and joind by roles
ON users_mlh.role = user_roles_mlh.id -- condition for joining
With this request, you will have all your users, with the role text associated.
So, if this request works, use the same logic on your ORM
Model:
function get_all() {
$query = $this->db->get('loan');
return $query->result();
}
function get_loan_name($code) {
$this->db->where('loan_type_code',$code);
$query = $this->db->get("loan_type");
return $query->row();
}
Controller:
function index()
{
$this->data['loans'] = $this->loan_approval->get_all();
$this->build_view("loan_approval/list.php");
}
In "LOAN TABLE" it have a "loan_type_code" column i want to get all the value in this column and match to 2nd function.
Example
Loan table Loan Type Table
ID loan_type ID loan_type_code loan_type_name
1 EDL 1 EDL Education Loan
2 DF 2 DF Damayan Fund
I need to match the "loan_type" in table "LOAN" to "loan_type_code" in "Loan Type" table.
Im thinking if of query like this "$this->controler->get_loan_name($this->data['loans']->loan_type);" but its not working.
Why not doing it at DB level using a JOIN?
$query = $this->db->select('loan.*, loan_type.loan_type_name')
->from('loan')
->join('loan_type','loan.loan_type = loan_type.loan_type_code')
->get();
return $query->result();
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.
This is my LeaveApplicationsController.php code:-
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.
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
Imp links: find('all'), find('first') and 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
I need full table from customer_account and one column commission from company commission
in table ewallet_fund_transfer primary key is id, that is reference key in company_commission. I want commission in front of this reference id.
function account_summary($userId, $acId)
{
$this->db->select('customer_account.*,company_commission.commission');
$this->db->from('customer_account');
$this->db->join('company_commission', 'customer_account.customerID=company_comession.customer_id','inner');
$this->db-> where('customerID', $userId);
$this->db-> where('curr_ac_id', $acId);
$this->db-> where('ewallet_fund_transfer.id=company_comession.reference_id');
$query = $this->db->get();
return $query->result();
}