yii2:autofill on multiple condtions - php

I have models/tables as follows:
table room_category
id room_category
1 Classic
2 Deluxe
table room_charges
id room_category room_name room_charges
1 1 c1 600
2 2 d1 800
table ipd_charges
id doctor room_category charges_cash charges_cashless
1 1 1 200 300
2 1 2 300 400
table patient_detail(patient_admission)
id patient_name tpa_name(if not null, equivalent to charges_cashless)
1 1 Null
2 2 1
table daily_ward_entry
id patient_name room_name(id) doctor_name(id) charges ( from ipd_charges)
1 1 1 1 200
2 2 2 1 400
Now there is a dropdown field in daily_ward_entry table for doctor. When I select the drop-down field the charges field needs to be autofilled.
I achieve this using Jason and ajax with the following code without taking into account the room_category. but the charges vary for room_category.(this is only working after saving the record, I prefer if there is someway to pull the charges before save.)
Here is my code in the controller:
public function actionChargesCash($id){
$model = \app\models\IpdCharges::findOne(['doctor'=>$id]);
return \yii\helpers\Json::encode(['visit_charges'=>$model->charges_cash]);
}
public function actionChargesCashLess($id){
$model= \app\models\IpdCharges::findOne(['doctor'=>$id]);
return \yii\helpers\Json::encode(['visit_charges'=>$model->charges_cashless]);
}
I have also tried this variaton:
public function actionChargesCash($id){
$model = \app\models\IpdCharges::find()
->where(['doctor'=>$id])
->andwhere(['room_category'=>'daily_ward_entry.room_name'])->one();
If I replace the 'daily_ward_entry.room_name' with room_name id like 3 or 6, I am getting the result, which means I am not using the correct syntax for referring the column from current table.
How can I include the condition for room_name in the above code?
Thanks in advance for your valuable help.

daily_ward_entry.room_name is meaningless without any relation or join or sub-query. Actually, the query does not know the daily_ward_entry.
Suggestions:
1- Create a relation and use with or Join With
2- Create a query with Join of daily_ward_entry and ipd_charges on room_name=room_category
3- Create a query with a sub-query, to find all|one IpdCharge(s) that have room_category in SELECT room_name FROM daily_ward_entry
All of above suggestions satisfy your requirements.
Another important note:
andWhere()/orWhere() are to apply where to the default condition.
where() is to ignore the default condition
I don't see any default condition (Overridden Find()), So, there is no need to use andWhere, Just:
where(['doctor'=>$id,'room_category'=>3,...])
Would be sufficient.

Related

How to get data from different tables in MySQL database and display in a table with php

I have read many topics but none have given me exactly what i need
I have 3 tables
products
id
product_name
1
Heineken
2
Budweiser
transaction
id
user_id
date
amount
32
4
01/23/2023
10000
45
2
01/23/2023
20000
57
4
01/23/2023
5000
transaction_details
id
transaction_id
product_id
1
32
1
2
32
2
3
45
2
4
45
1
5
57
1
Now how can i achieve this
SELECT FROM transaction WHERE user_id = 4 then use the results to SELECT FROM transaction_details and display it in a table using a single sql query
And when displaying the results from transaction_details i want to use product_name
This is the result i want
transaction_id
products_id and name
32
1. Heineken
32
2. Budweiser
57
1. Heineken
I have tried to select from transaction table where id is equal to 4 and i got two results, i don't know what to do next
Using Mysqli Join Query with select concat option in in your select query
Please Using This Way
select tran.id,CONCAT(pro.id,'.',pro.product_name) as products_id_and_name from transaction as tran join transaction_details as trand on tran.id = trand.transaction_id JOIN products as pro on pro.id = trand.product_id WHERE tran.user_id = 4;

Multiple leftJoins using Laravel's Query Builder producing incorrect counts

I am using Laravel 5.4's Query Builder to perform a series of leftJoins on three tables. Here are my tables:
items
id type title visibility status created_at
-- ---- ----- ---------- ------ ----------
1 1 This is a Title 1 1 2017-06-20 06:39:20
2 1 Here's Another Item 1 1 2017-06-24 18:12:13
3 1 A Third Item 1 1 2017-06-26 10:10:34
count_loves
id items_id user_id
-- ------- -------
1 1 2
2 1 57
3 1 18
count_downloads
id items_id user_id
-- ------- -------
1 1 879
2 1 323
And here is the code I am running in Laravel:
$items_output = DB::table('items')
->leftJoin('count_loves', 'items.id', '=', 'count_loves.items_id')
->leftJoin('count_downloads', 'items.id', '=', 'count_downloads.items_id')
->where('items.visibility', '=', '1')
->where('items.status', '=', '1')
->orderBy('items.created_at', 'desc')
->select('items.*', DB::raw('count(count_loves.id) as loveCount'), DB::raw('count(count_downloads.id) as downloadCount'))
->groupBy('items.id')
->get();
When I return the results for this query, I am getting the following counts:
count_loves: 6
count_downloads: 6
As you can see, the actual count values should be:
count_loves: 3
count_downloads: 2
If I add another entry to the count_loves table, as an example, the totals move to 8. If I add another entry to the count_downloads table after that, the totals jump to 12. So, the two counts are multiplying together.
If I die and dump the query, here's what I get:
"query" => "select 'items'.*, count(count_loves.id) as loveCount,
count(count_downloads.id) as downloadCount from 'items' left join
'count_loves' on 'items'.'id' = 'count_loves'.'items_id' left join
'count_downloads' on 'items'.'id' = 'count_downloads'.'items_id'
where 'items'.'visibility' = ? and 'items'.'status' = ? group by
'items'.'id' order by 'items'.'created_at' desc"
How do I perform multiple leftJoins using Query Builder and count on several tables to return the proper sums?
NOTE:
This is intended as a HELP answer not the total absolute answer but I could not write the code in a comment. I am not asking for votes (for those who just can't wait to downvote me). I have created your tables and tried a UNION query on raw sql. I got correct results. I dont have laravel installed, but maybe you could try a UNION query in Laravel.
https://laravel.com/docs/5.4/queries#unions
select count(count_downloads.user_id)
from count_downloads
join items
on items.id = count_downloads.items_id
UNION
select count(count_loves.user_id)
from count_loves
join items
on items.id = count_loves.items_id

mysql 2 tables updation/deletion based on condition

Hello friends I have 2 Mysql tables with 1:N relationship between category and category_Dates
Category:
ID Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
category_Dates:
ID CatID Date
1 1 01-Jan-15
2 1 01-Jul-15
3 2 01-Jan-15
4 2 01-Apr-15
5 2 01-Jul-15
6 2 01-Oct-15
based on the category frequency I am entering number of records automatically in category_date. Eg
When category frequency = quarterly, I am entering 4 records in category_date with ID of that category. And dates will be entered later.
I am little confused if in case on wants to edit the frequency from halfyearly to yearly. How to change number of records. Please help with your valuable suggestions. I am using laravel 4 framework with mysql
best way would be with 3rd table joining Dates and Categories. See little carefully ,you can see its actually Many to Many relationship (N to N) as 1 category can have multiple dates. and one date may be part of multiple categories, like say 01-Jan-15 is part of Category 1 and 2 as well.
So use
category table
id Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
date table
id Date
1 01-Jan-15
2 01-Apr-15
3 01-Jul-15
4 01-Oct-15
categories_dates table
ID CatID Date_id
1 1 1
2 1 3
3 2 1
4 2 2
5 2 3
6 2 4
If you change the frequency in Category table, retrieve the update category_id,
delete all from category_dates where CatId=category_id then insert the new entries in category_Dates.
Hope this help.
I assume your models are Category and CategoryDates.
let's update category id 1 from Half-yearlyto to Quarterly
$query = Category::find(1);
$query -> Frequency = 'Quarterly';
$query -> save();
return $query -> id;
in the CategoryDates model you would delete the catID = 1 and insert new data
$catID = 1;
$query = CategoryModel::where('CatId',$catId) -> delete();
$data = ['CatId' => $catID,'date' => 01-Jan-15, ....];
CategoryModel::create($data);
of course assuming that you would return the newly updated category id to your controller and call a funtionn to do the update in your CategoryModel.
Hope this help.

update rows with same column value mysql and delete one

TABLE cartitems
CartItemId|CartId|sku_id|prod_id|quantity
1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1
2 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1
I want to merger the rows having same sku_id and update the quantity and delete one duplicate row like this
CartItemId|CartId|sku_id|prod_id|quantity
1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 2
if delete doesnt happens its okay. i l manage it later.
it's simple Aggregate function with an ORDER BY Keyword to
use.
try,
select
min(CartItemId),
min(CartId),
sku_id,
Sum(quantity) from cartitems group by sku_id

CakePHP Associated Models returning null?

I am having trouble with variables being returned having NULL values randomly.
I have this model association structure:
Truck $hasOne Chassis
Chassis $hasMany Make
Chassis $hasMany Model
Truck $hasOne Type
Randomly results will have null results for type or null results for make and model, but other results will have the correct results..
0 (array)
Truck (array)
Chassis (array)
id 1
make_id 2
model_id 2
title Basic Chassis
description This Basic Chassis is good for a lot of things, but advanced stuff is not one..
Truck_make (empty)
Truck_model (empty)
1 (array)
Truck (array)
Chassis (array)
id (null)
make_id (null)
model_id (null)
title (null)
description (null)
Above you can see that 2 trucks are loaded, the first one's chassis loaded and the make and model id's, but not the array's for the make and model. Also the second vehicle had nothing load..
Here's a screenshot of associations from MySQL Workbench:
http://d.pr/i/JgMt
These are the queries that are called.
Query Affected Num. rows Took (ms) Actions
SELECT `Truck`.`id`, `Truck`.`chassis`, `Truck`.`type`, `Truck`.`title`, `Truck`.`ref`, `Truck`.`hide_truck`, `Truck`.`featured_gallery`, `Truck`.`featured_home`, `Truck`.`created`, `Chassis`.`id`, `Chassis`.`make_id`, `Chassis`.`model_id`, `Chassis`.`title`, `Chassis`.`description`, `Type`.`id`, `Type`.`title`, `Type`.`desc` FROM `douglass_cake`.`trucks` AS `Truck` LEFT JOIN `douglass_cake`.`chassis` AS `Chassis` ON (`Chassis`.`id` = `Truck`.`id`) LEFT JOIN `douglass_cake`.`types` AS `Type` ON (`Type`.`id` = `Truck`.`id`) WHERE 1 = 1
SELECT `Truck_make`.`id`, `Truck_make`.`make`, `Truck_make`.`hide_make` FROM `douglass_cake`.`truck_makes` AS `Truck_make` WHERE `Truck_make`.`id` = (1)0
SELECT `Truck_model`.`id`, `Truck_model`.`model`, `Truck_model`.`hide_model` FROM `douglass_cake`.`truck_models` AS `Truck_model` WHERE `Truck_model`.`id` = (1)
I figured out this issue. Looks like MySQL Workbench wrote the tables with an offset of 2 instead of 1. Cakephp was expecting the default offset so when the rows were returned to cakephp it read it starting at 1 while MySQL was told to start at 2. FIxes. Stupid error

Categories