Codeigniter active record nested select issue - codeigniter

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

Related

GraphQL Query for Inner Join

how shall I make Query to GraphQL for inner join of 3 tables within same data source?
I have to get List of Fields from SQL DB from 3 different tables within same server.
I am passing Query from UI(angular) as string to=> API(c#,.net - REST API architecture) wherein I'll get service called based on my query.
I could not find any feature of GraphQL which is specific to getting data from more than 1 table at a time.
Can anyone help me out on how to get inner join done in GraphQL? also if not then what should be optimistic approach for same?
Note : we are using EF on backend with .net to get/filter data from SQL.
in this I have tried using Linq to filter and map data from all 3 diffrent sources :
Also I have tried using Include to directly map the data

Run complex SQL Query with spring boot repositories

i have a little complex scenario using spring data and jpa currently.
My data structure is like:
And i like to create a filter: give me all events which belongs to a list of structures, within a given period and is assigned to a list of categories.
I was able to create a sample SQL statement:
select * from event e inner join period p on e.period_id=p.id
inner join category_item_ids ci on e.id=ci.item_ids
inner join category c on ci.category_id=c.id
inner join event_assignment_structure_ids es on es.event_assignment_id=e.id
where p.start between '2020-05-01 12:00:00.000000' and '2020-11-14 12:00:00.000000' and
c.id in (1) and
es.structure_ids in (1,2)
But my objects are currently not wired together by all the JPA annotations.
e.g. the "Same ID" is something i did by convention to make the parts a little more independend.
So using the JPA way is currently no option i guess due to the missing relations.
Introducing them will be also quite a lot of work.
So i was wondering if there is a possiblity to run the sql query directly (i could use native query, but thats also no option, cause the filter values are not always given, so i need 13 native queries)
I used enityManager and query Builder in the past, but thats also no option due to the missing jpa relations.
Any ideas are much welcome :-)
Regards
Oliver

Optimizing Hibernate layer

After moving the logic from a legacy application (SQL/coldfushion) to Spring Rest with Hibernate, we have experienced a slowness in the application. The main reason is with Hibernate we noticed many queries are generated which we used to do with one single query in the legacy application (two pages long query).
Write now, I'm looking at selecting proper fetch strategies and try to optimize code. Could you please give me any other areas that I need to investigate to optimize the Hibernate layer or any other sujjestions?
Try to use DTO not entities(you can load DTO directly from the database)
Review the loading strategy (Eager or Lazy)
Try to use Native Queries more
Try to use more parameters to restrict the result set
You also can leverage some caching technique (cache all static data)
Try to implement hashCode and equals for each entity
If you use, HQL queries, then add the 'join fetch', It avoids the n+1 query problems. For more information on join fetch
e.g:
select a from Model a
inner join fetch a.b as b
Add 'indexes' for columns which are using in where condition.
e.g: Add index for the column 'name' which is used in where condition.
select a from Model a where a.name ='x'
Follow the below links:
http://www.thoughts-on-java.org/tips-to-boost-your-hibernate-performance/
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html

Database specific queries in a Spring Hibernate application

In a dao class implementation,I want to use different sql query depending upon the underlying database. Since my SQL query is complex which selects from a database view and uses "UNION" key word and uses database specific functions, I can not use JPQL (or HQL). I did some search on Stackoverflow and threads suggest the good way would be to find out the dialect used in the application. Can anyone provide some code example?
EDIT : My apologies, I did not explain my question well enough. In my dao class implementation , I want to determine the database ( say mysql or oracle) on which my application is running and then execute the appropriate query. I need the code like jdbcTemplate.findOutTheDialect().
JPA have the native queries for that.
An example you can find here.
You can use spring JdbcTemplate to connect and query from your database.
For Example..
String query = "SELECT COUNTRY_NAME FROM COUNTRY WHERE MCC_COUNTRY NOT IN ('Reserved') ORDER BY COUNTRY_NAME";
jdbcTemplate.query(query, new ObjectRowMapper());
Where "ObjectRowMapper" will be a class to map return resultset to list of Objects.

Code Igniter Model To Model Relationship

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

Resources