How to convert Sql query to codeigniter Active Record - codeigniter

I have sql query as below, i need to convert sql to codeigniter active record.
select * from color c
left join (select pc.* from product_color pc
LEFT join product p on pc.product_id=p.product_id
WHERE p.product_id=1)x on c.id=x.color_id

In codeigniter you can get compiled query using get_compiled_select function.
Let's create inner query first.
$inner_query = $this->db->select('pc.*')
->from('product_color pc')
->join('product p','pc.product_id = p.product_id','left')
->where('p.product_id',1)
->get_compiled_select();
Now we will use inner query to create our final query.
$final_query = $this->db->select('*')
->from('color c')
->join("($inner_query) x",'c.id=x.color_id','left')
->get_compiled_select();
echo $final_query; die;

Try this into model file. and it's always better to keep sql keywords in capital.
$query = "SELECT * FROM color c
LEFT JOIN (SELECT pc.* FROM product_color pc
LEFT JOIN product p ON pc.product_id=p.product_id
WHERE p.product_id=1)x on c.id=x.color_id"
$query = $this->db->query($query);
$data = $query->result_array();

Related

Select from junction table

everybody!
What should I do if I need to make select from junction table?
For example, I develop project and I need to make chats between users. I have two entities: User and Chat, and many-to-many relation between them (accordingly, I have three tables: user, chat, chat_user). I try to get all chats, which user is member, and to get all users from these chats.
I made the following SQL query:
SELECT *
FROM chat c
INNER JOIN chat_user cu ON c.id = cu.chat_id
INNER JOIN user u ON u.id = cu.user_id
WHERE c.id IN (SELECT chat_id
FROM chat_user
WHERE user_id = <idUser>);
But I don't know how to translate in DQL subquery SELECT chat_id FROM chat_user WHERE user_id = <idUser>, because a haven't additional entity for table chat_user.
And I tried to add entity ChatUser and get data in ChatRepository smt. like this:
public function getChatsData($idUser)
{
$subQuery = $this->getEntityManager()
->getRepository(ChatUser::class)
->createQueryBuilder('chus')
->select('chus.chat')
->andWhere('chus.user = :idUser')
->setParameter('idUser', $idUser)
;
$qb = $this->createQueryBuilder('c');
return $qb
->innerJoin('c.chatUsers', 'cu')
->addSelect('cu')
->innerJoin('cu.user', 'u')
->addSelect('u')
->innerJoin('c.messages', 'm')
->addSelect('m')
->andWhere('u.id = :idUser')
->andWhere($qb->expr()->in(
'c.id',
$subQuery->getDQL()
))
->setParameter('idUser', $idUser)
->getQuery()
->getResult()
;
}
but it doesn't work. I get error [Semantical Error] line 0, col 12 near 'chat FROM App\Entity\ChatUser': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
Have Doctrine standard tools for such tasks?

Is there a way to rewrite this raw SQL query with Laravel Query Builder

I have two tables:
products
id | code | supplier_id |...
company_product
id | retail_price | copmany_id |...
There are multiple online shops from where I'm pulling the products via their API's which I store locally. It is pretty much used to compare their prices, unit stock, and so on which can be done via product code which is the same across all shops.
I'm fetching all products only once and updating them a couple of times daily. Other companies using this, let's say "platform", have different prices for products based on their contracts which are kept in the second table.
The thing I'm trying to achieve is to list all products but only the cheapest version of the product for that product code.
I am able to achieve it with the following query.
$products = DB::select(DB::raw(
"select p.*, t.price_min
from (select MAX(p.id) as id, p.code, MIN(cp.retail_price) as price_min
from products as p
left join company_product as cp on cp.product_id = p.id
group by p.code) as t
left join products as p on p.id = t.id"
));
Which gives me the result I want. However, I have lots of filters, sorts, relations, and pagination to add on top of this, which is the reason why I'm trying to rewrite it.
Every suggestion is much appreciated.
First of all, the raw MySQL query you probably want here is just an inner join:
SELECT p.*, t.price_min
FROM products p
INNER JOIN
(
SELECT p.code, MAX(p.id) AS max_id, MIN(cp.retail_price) AS price_min
FROM products p
INNER JOIN company_product cp ON cp.product_id = p.id
GROUP BY p.code
) t
ON p.id = t.max_id
As for the Laravel code, we can try creating the subquery in a separate variable, and then join to it:
$subquery = DB::table('products AS p')
->select([
'p.code',
DB::raw('MAX(p.id) AS max_id, MIN(cp.retail_price) AS price_min')])
->join('company_product AS cp', 'cp.product_id', '=', 'p.id')
->groupBy('p.code');
$query = DB::table('products AS p')
->select('p.*', 't.price_min')
->innerJoinSub($subquery, 't', function($join) {
$join->on('p.id', '=', 't.max_id');
})
->get();

Error request when I defined the column name

SQL :
select distinct b.nom, b.moyenne_pond
from previ.rdt_usine b
join previ.rdt r on (r.id_us=b.id_us)
where st_intersects(st_transform(st_setsrid(st_makepoint(55.40513629386126, -21.23713970920021), 4326), 32740),b.geom)
and r.annee=2018;
PHP :
$query = 'select distinct "b.nom", "b.moyenne_pond"
from "previ"."'.$name_table.'" b
join "previ"."rdt" r on (r.id_us=b.id_us)
where st_intersects(st_transform(st_setsrid(st_makepoint(?, ?), 4326), 32740), b.geom)
and r.annee=?';
$data = $db->select($query, $coord);
//$coord is an array of data
When I tried to use my request I have a this : column "b.nom" doesn't exist, but when I tried the query in pgAdmin the query is successfull, and you could see that the queries are exactly the same
Remove double quotes and add back tick
$query = 'select distinct `b.nom`, `b.moyenne_pond`
from `previ`.`$name_table` as b
join `previ`.`rdt` as r on (r.id_us=b.id_us)
where st_intersects(st_transform(st_setsrid(st_makepoint(?, ?), 4326), 32740), b.geom)
and r.annee=?';

left join with ActiveRecord (yii2)

I tried to send SQL request with LEFT JOIN but it doesn't display data from table2 table.
public static function top($limit)
{
return self::findBySql("
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON (g1.id = s1.g_id AND s1.id = (
SELECT MAX(id)
FROM table2 s2 WHERE s2.g_id = g1.id
))
LIMIT :limit",
[':limit' => $limit]
)->all();
}
It seems you are adding this function to the model and self represents the model itself.
Yii will not return results from another table and will be limited to the model only if you are calling the find on a model, instead you need to use a db query as below:
$query = new \yii\db\Query;
$query->select('*')
->from('table 1 g1')
->leftJoin('table2 s1', 's1.g_id AND s1.id = (SELECT MAX(id) FROM table2 s2 WHERE s2.g_id = g1.id')
->limit($Limit);
$command = $query->createCommand();
$resp = $command->queryAll();
The correct SQL query is
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON g1.some_field = s1.some_field
where g1.some_field = s1.some_field are the fields that define the join.
I have working code something like...:)
with user and user_friend_list
$query = new Query;
$query ->select(['user.id AS user_id', 'user_friend_list.id'])
->from('user')
->innerJoin('user_friend_list', 'user.email = user_friend_list.email');
$command = $query->createCommand();
$data = $command->queryAll();
foreach($data as $datakey)
{
//echo $datakey['id'];
$friendfind = UserFriendList::findOne($datakey['id']);
$friendfind->is_app_using_user_id=$datakey['user_id'];
$friendfind->save();
}

Querying 4 tables with inner join codeigniter

i have 4 tables jobs, company, employment_type & job_category the primary key for each are job_id, com_id, type_id, job_cat_id, but (com_id, type_id, job_cat_id) are foreign key to jobs table.
my query without active record work perfectly and it is as follow
select company.com_id, company.company_name, jobs.job_id, jobs.title, jobs.opening_date, jobs.closing_date, jobs.number_of_pos, employment_type.type_id, employment_type.type, job_category.job_cat_id, job_category. category from company inner join jobs on company.com_id=jobs.com_id inner join employment_type on employment_type.type_id=jobs.type_id inner join job_category on job_category.job_cat_id=jobs.job_cat_id
but if i try to use codeiginiter active record such as
$this->db->select('company.com_id, company.company_name, jobs.job_id, jobs.title, jobs.opening_date, jobs.closing_date, jobs.number_of_pos, employment_type.type_id, employment_type.type, job_category.job_cat_id, job_category. category');
$this->db->from('company');
$this->db->join('jobs','company.com_id=jobs.com_id','inner');
$this->db->join('employment_type', 'employment_type.type_id=jobs.type_id','inner');
$this->db->join('job_category', 'job_category.job_cat_id=jobs.job_cat_id','inner');
$this->db->order_by('job_id','DESC');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result_array();
i end up with the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`) INNER JOIN `jobs` ON `company`.`com_id`=`jobs`.`com_id` INNER JOIN `employmen' at line 1
SELECT `company`.`com_id`, `company`.`company_name`, `jobs`.`job_id`, `jobs`.`title`, `jobs`.`opening_date`, `jobs`.`closing_date`, `jobs`.`number_of_pos`, `employment_type`.`type_id`, `employment_type`.`type`, `job_category`.`job_cat_id`, `job_category`.` category FROM (`company`) INNER JOIN `jobs` ON `company`.`com_id`=`jobs`.`com_id` INNER JOIN `employment_type` ON `employment_type`.`type_id`=`jobs`.`type_id` INNER JOIN `job_category` ON `job_category`.`job_cat_id`=`jobs`.`job_cat_id` ORDER BY `job_id` DESC LIMIT 10
Any help would be appreciated
you have a space in job_category. category remove that and you should be golden
Format your query like this
$data = array(
'company.com_id',
'company.company_name',
'jobs.job_id',
'jobs.title',
'jobs.opening_date',
'jobs.closing_date',
'jobs.number_of_pos',
'employment_type.type_id',
'employment_type.type',
'job_category.job_cat_id',
'job_category.category'
);
$this->db->select($data);
$this->db->from('company');
$this->db->join('jobs','company.com_id=jobs.com_id','inner');
$this->db->join('employment_type', 'employment_type.type_id=jobs.type_id','inner');
$this->db->join('job_category', 'job_category.job_cat_id=jobs.job_cat_id','inner');
$this->db->order_by('job_id','DESC');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result_array();

Resources