Code Igniter Model To Model Relationship - codeigniter

In CI, how do you relate each other the models?I have four models right now Users, UsersDepartment, UsersToDepartment, UserStatus and I need to join those four models to be able to pick up all the data.
I have this code in my controller to pick all users data from the Users Table:
function view($user_id){
$data['user'] = $this->User_model->get_by_id($user_id)->row();
}
The user_status saved in the Users Table is only the status_id so I need to connect to the UserStatus table to get the equivalent name of the users_status_id. I need to know the list of group of which the user belongs to. So I need to get it from the UsersToDepartment Table based on the Users.userid. Then get the equivalent groupname in the UsersDepartment Table. Please see my diagram to explain further.
I know in the native PHP, this can be done by using join. How is that done in CI?
I know with yii, you can do it this way
$posts=Post::model()->with(
'author.profile',
'author.posts',
'categories')->findAll();
Is this possible with CI too?

example u have table_one and want to join table_two using their id
$this->db->select('columns');
$this->db->from('table_one');
$this->db->join('table_two', 'table_two.id = table_one.id');
//then do the query
you can read this link below for more complete tutorial :
https://www.codeigniter.com/userguide2/database/active_record.html

code igniter is not a ORM framework for php...
you can not treat it like ORM frameworks(Laravel is good example for ORM frameworks).
but you can simulate that with join on query.
this work just get you the others models data and not get you those models object ...

Refer to $this->db->join(); heading in Active Record: CodeIgniter
I know codeigniter is not that good here. So I always prefer Yii over it.

Try use this query of joining table
Select a.*,b.*
from table_one a
inner join table_two b where b.id=a.id

Related

rails HABTM to model

I have 2 models user and category. each user will select multiple categories of data, I'm using HABTM. Is this right method?. If yes How can I store multiple category details with each user in join table?
Never use HABTM, if you want to add attributes to your join table and interact with your join table.
Use has_many though. You can read more about it in the docs below
http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

Laravel Eloquent ORM - Get first and third table data

Category Table
Service Table
Branch Table
I have 3 tables link together. Above is my code, I want to produce the result same as below. Can I use a line of code to do that?
Or I have to get result from 2 tables first then only get the Branch table?
Thanks.
If you would like to obtain a collection of categories with service and branches you can eager load them in a single eloquent query.
$categories = Category::with('service.branches')->get();
But you still have to write a bit more if you would like to achieve your requirement.
Fractal Transformers might help you to do this. See their documentation here.

laravel 5.2 work with belongs to with two different queries

I have write down this query
Controller
$data = User::where('name',$name)->with('country');
In User model
function country () {
return $this->belongsTo('App\Country');
}
In view
echo $data->country->name;
It is working fine but it run 2 queries :(
Select * from user where name = "xyz"
Select * from country where id = "745"
I want to stop this, I want to fetch data with one query only. Join is the solution, Is any other solution for this?
Unfortunately this is the way Eloquent works. It uses two queries because it's a simpler task to initialise your models and to avoid column naming conflicts.
If you are concerned about performance but still want some sort of querying tool, use the Query Builder shipped with Laravel.
To answer your question, joins will be your best bet.
$data=user::with('country')->where('id',745)->where('name','xyz')->get();
i hope that will help you

Laravel Eloquent model data from 2 tables

I've just started using Laravel and I'm coming from a different system using an existing database. In this system there are 2 users table, one stock with the CMS and one custom one.
I want to create an Eloquent model which retrieves data from both tables into one model. I know I can use the following to create a relationship.
$this->hasOne('App\SecondUser', 'id', 'id);
But this results in 2 sql queries, and I want to join results from 2 tables before returning the model, in one join statement. How do I do this?
That might be a bit more complicated that you would expect.
First of all you have to use \DB facade to join the two collections(arrays) and then recreate the Eloquent collection from these arrays using Collection's make method.
More info about the Collection class here
An easier way might be to join them using standard laravel relationships and user something like Model::user->with('relation')->get.
But this will still create 2 queries (still relatively fast).

Codeigniter active record nested select issue

I am using codeigniter for my php project, but am not much experience in it. So, please help me.
I have two database. 1. Kalix2 2. Asterisk
In both the database I am having some table. I want to make a join on the tables from two different database. I have the below query, it's working fine in back-end, but I don't know how to implement it using Active record.
SELECT a.CompanyName, sum(ceil((b.billsec)/(c.Call_Limit*60))) as totalcall,
hour(b.calldate) as use_hour FROM Kalix2.ph_Companies a
INNER JOIN Asterisk.cdr b ON b.clid LIKE CONCAT('%', a.CompanyName, '%')
INNER JOIN Kalix2.ph_Plans c ON c.Comp_ID= a.Comp_ID
where date(b.calldate)>='2013-01-15' and date(b.calldate)<='2013-1-20'
and c.Plan_Type='Per_Call' and a.CompanyName='ABCD'
group by hour(b.calldate);
Please help me to convert this query into active record format.
You should be able to convert this to active record if you want but you will gain better performance by just converting this to Query Bindings. Common mistake is trying to run a query off of multiple classes:
$this->db1
$this->db2
This will not work because running $this->db1->get() because it is only aware of the data in db1 and not db2. Verbosely keep the database.table info in the joins, use only one db object, and use the profiler to debug the query generation and you should be good.
Connecting to Multiple Databases
Active Record Class and Joins
Debug the query using Profiler

Resources