Laravel subquery whereIn - laravel

I have a problem with a query.
I want to make a subquery but it does not return results.
Here is my code :
$distance_representations = Representation::select('id')
->where(DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->limit(10)
->get();
$representations = Representation::select('id', 'city', DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->whereIn('id', $distance_representations)
->get();
return view('search', array('representations' => $representations));
PostgreSQL equivalent :
select id, city, round((6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude))))::numeric, 1) as distance
from representations
where id in (
select id
from representations
where round((6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude))))::numeric, 1) < 50
limit 10
)
Thank you

perhaps you can try this
$distance_representations = Representation::find('id')
->where(DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->limit(10)
->get();
$representations = Representation::all()
->where(DB::raw('round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50'))
->whereIn('id', $distance_representations.id)
->get();
hope this will help you.

I don't see why you need the sub-query? Unless I'm missing something, you should be able to select your data using a single query.
$representations = Representation::selectRaw('id, city, round(6371 * acos(cos(radians(45.0)) * cos(radians(latitude)) * cos(radians(longitude) - radians(2.7)) + sin(radians(45.0)) * sin(radians(latitude)))::numeric, 1) < 50 AS distance')
->where('distance', '<', 50)
->limit(10)
->get();

I found the solution. I can not do better. Thank you.
This solution WORKS !
$representations_with_distance = Representation::select('id', 'city', DB::raw('round(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))::numeric, 1) as distance'));
$representation = DB::table(DB::raw("({$representations_with_distance->toSql()}) as rd"))
->where('distance', '<', '?')
->orderBy('distance')
->offset(0)
->limit(10)
->setBindings([$latitude, $longitude, $latitude, $radius])
->get();
return view('search', array('representations' => $representation));

Related

How to avoid errors in trigonometry functions in Oracle?

I have a curson in my stored procedure
SELECT a.id, a.full_address, me.answer medid, me.name NAME, a.nlat, a.nlong, a.parent_table
FROM example_table_1 a,
(SELECT pnradius AS radius,
111.045 AS distance_unit,
57.2957795 AS rad2deg,
0.01745329251994 AS deg2rad
FROM dual) geo,
example_table me
WHERE a.nlat BETWEEN pnlatitude - (geo.radius / geo.distance_unit) AND
pnlatitude + (geo.radius / geo.distance_unit)
AND a.nlong BETWEEN pnlongitude - (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude)))) AND
pnlongitude + (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND geo.distance_unit * rad2deg *
(acos(cos(deg2rad * (pnlatitude)) * cos(deg2rad * (a.nlat)) * cos(deg2rad * (pnlongitude - a.nlong)) +
sin(deg2rad * (pnlatitude)) * sin(deg2rad * (a.nlat)))) < pnradius
AND a.parent_id = me.answer
AND a.parent_table = 'example_table'
pnlatitude and pnlongiture are paramenters of procedure.
In most cases this cursor works great. But sometimes in some areas in Russia this cursor cause this error:
I understand what is going on here, but I can't track where does it happen. I can adjust deg2rad value and it helps, but then this error will appear with other coordinates.
Is it possible to reduce the value of trigonometry function paramentr to 1 when it is more than 1?
Use the LEAST function to ensure you don't pass an argument greater than 1 to ACOS:
SELECT a.id, a.full_address, me.answer medid, me.name NAME, a.nlat, a.nlong, a.parent_table
FROM example_table_1 a,
(SELECT pnradius AS radius,
111.045 AS distance_unit,
57.2957795 AS rad2deg,
0.01745329251994 AS deg2rad
FROM dual) geo,
example_table me
WHERE a.nlat BETWEEN pnlatitude - (geo.radius / geo.distance_unit)
AND pnlatitude + (geo.radius / geo.distance_unit)
AND a.nlong BETWEEN pnlongitude - (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND pnlongitude + (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND geo.distance_unit * rad2deg *
(acos(LEAST(cos(deg2rad * (pnlatitude)) * cos(deg2rad * (a.nlat)) * cos(deg2rad * (pnlongitude - a.nlong)) +
sin(deg2rad * (pnlatitude)) * sin(deg2rad * (a.nlat)), 1))) < pnradius
AND a.parent_id = me.answer
AND a.parent_table = 'example_table'
This is the same as your original with a LEAST(big-long-calc, 1) added inside the ACOS call. Hopefully I counted the parentheses right - if not, adjust as necessary. :-)
Best of luck.

Simplify code using do until vb6

I'm doing a program in VB6 that simulates the process of multiplying a 6x6 to 6x6 matrix. I just did the code only for the first row and second row. To be frankly honest I'm a beginner in programming so I'm still figuring things out.
Can someone show how to simplify this code using do until loop so that I can also apply it for the remaining 3rd, 4th, 5th and 6th row?
Re:
I also added the code for rows 3rd, 4th, 5th and 6th. Pls do help me.
an(0).Text = Val(a(0).Text) * Val(b(0).Text) + Val(a(1).Text) * Val(b(6).Text) + Val(a(2).Text) * Val(b(12).Text) + Val(a(3).Text) * Val(b(18).Text) + Val(a(4).Text) * Val(b(24).Text) + Val(a(5).Text) * Val(b(30).Text)
an(1).Text = Val(a(0).Text) * Val(b(1).Text) + Val(a(1).Text) * Val(b(7).Text) + Val(a(2).Text) * Val(b(13).Text) + Val(a(3).Text) * Val(b(19).Text) + Val(a(4).Text) * Val(b(25).Text) + Val(a(5).Text) * Val(b(31).Text)
an(2).Text = Val(a(0).Text) * Val(b(2).Text) + Val(a(1).Text) * Val(b(8).Text) + Val(a(2).Text) * Val(b(14).Text) + Val(a(3).Text) * Val(b(20).Text) + Val(a(4).Text) * Val(b(26).Text) + Val(a(5).Text) * Val(b(32).Text)
an(3).Text = Val(a(0).Text) * Val(b(3).Text) + Val(a(1).Text) * Val(b(9).Text) + Val(a(2).Text) * Val(b(15).Text) + Val(a(3).Text) * Val(b(21).Text) + Val(a(4).Text) * Val(b(27).Text) + Val(a(5).Text) * Val(b(33).Text)
an(4).Text = Val(a(0).Text) * Val(b(4).Text) + Val(a(1).Text) * Val(b(10).Text) + Val(a(2).Text) * Val(b(16).Text) + Val(a(3).Text) * Val(b(22).Text) + Val(a(4).Text) * Val(b(28).Text) + Val(a(5).Text) * Val(b(34).Text)
an(5).Text = Val(a(0).Text) * Val(b(5).Text) + Val(a(1).Text) * Val(b(11).Text) + Val(a(2).Text) * Val(b(17).Text) + Val(a(3).Text) * Val(b(23).Text) + Val(a(4).Text) * Val(b(29).Text) + Val(a(5).Text) * Val(b(35).Text)
an(6).Text = Val(a(6).Text) * Val(b(0).Text) + Val(a(7).Text) * Val(b(6).Text) + Val(a(8).Text) * Val(b(12).Text) + Val(a(9).Text) * Val(b(18).Text) + Val(a(10).Text) * Val(b(24).Text) + Val(a(11).Text) * Val(b(30).Text)
an(7).Text = Val(a(6).Text) * Val(b(1).Text) + Val(a(7).Text) * Val(b(7).Text) + Val(a(8).Text) * Val(b(13).Text) + Val(a(9).Text) * Val(b(19).Text) + Val(a(10).Text) * Val(b(25).Text) + Val(a(11).Text) * Val(b(31).Text)
an(8).Text = Val(a(6).Text) * Val(b(2).Text) + Val(a(7).Text) * Val(b(8).Text) + Val(a(8).Text) * Val(b(14).Text) + Val(a(9).Text) * Val(b(20).Text) + Val(a(10).Text) * Val(b(26).Text) + Val(a(11).Text) * Val(b(32).Text)
an(9).Text = Val(a(6).Text) * Val(b(3).Text) + Val(a(7).Text) * Val(b(9).Text) + Val(a(8).Text) * Val(b(15).Text) + Val(a(9).Text) * Val(b(21).Text) + Val(a(10).Text) * Val(b(27).Text) + Val(a(11).Text) * Val(b(33).Text)
an(10).Text = Val(a(6).Text) * Val(b(4).Text) + Val(a(7).Text) * Val(b(10).Text) + Val(a(8).Text) * Val(b(16).Text) + Val(a(9).Text) * Val(b(22).Text) + Val(a(10).Text) * Val(b(28).Text) + Val(a(11).Text) * Val(b(34).Text)
an(11).Text = Val(a(6).Text) * Val(b(5).Text) + Val(a(7).Text) * Val(b(11).Text) + Val(a(8).Text) * Val(b(17).Text) + Val(a(9).Text) * Val(b(23).Text) + Val(a(10).Text) * Val(b(29).Text) + Val(a(11).Text) * Val(b(35).Text)
an(12).Text = Val(A(12).Text) * Val(B(0).Text) + Val(A(13).Text) * Val(B(6).Text) + Val(A(14).Text) * Val(B(12).Text) + Val(A(15).Text) * Val(B(18).Text) + Val(A(16).Text) * Val(B(24).Text) + Val(A(17).Text) * Val(B(30).Text)
an(13).Text = Val(A(12).Text) * Val(B(1).Text) + Val(A(13).Text) * Val(B(7).Text) + Val(A(14).Text) * Val(B(13).Text) + Val(A(15).Text) * Val(B(19).Text) + Val(A(16).Text) * Val(B(25).Text) + Val(A(17).Text) * Val(B(31).Text)
an(14).Text = Val(A(12).Text) * Val(B(2).Text) + Val(A(13).Text) * Val(B(8).Text) + Val(A(14).Text) * Val(B(14).Text) + Val(A(15).Text) * Val(B(20).Text) + Val(A(16).Text) * Val(B(26).Text) + Val(A(17).Text) * Val(B(32).Text)
an(15).Text = Val(A(12).Text) * Val(B(3).Text) + Val(A(13).Text) * Val(B(9).Text) + Val(A(14).Text) * Val(B(15).Text) + Val(A(15).Text) * Val(B(21).Text) + Val(A(16).Text) * Val(B(27).Text) + Val(A(17).Text) * Val(B(33).Text)
an(16).Text = Val(A(12).Text) * Val(B(4).Text) + Val(A(13).Text) * Val(B(10).Text) + Val(A(14).Text) * Val(B(16).Text) + Val(A(15).Text) * Val(B(22).Text) + Val(A(16).Text) * Val(B(28).Text) + Val(A(17).Text) * Val(B(34).Text)
an(17).Text = Val(A(12).Text) * Val(B(5).Text) + Val(A(13).Text) * Val(B(11).Text) + Val(A(14).Text) * Val(B(17).Text) + Val(A(15).Text) * Val(B(23).Text) + Val(A(16).Text) * Val(B(29).Text) + Val(A(17).Text) * Val(B(35).Text)
an(18).Text = Val(A(18).Text) * Val(B(0).Text) + Val(A(19).Text) * Val(B(6).Text) + Val(A(20).Text) * Val(B(12).Text) + Val(A(21).Text) * Val(B(18).Text) + Val(A(22).Text) * Val(B(24).Text) + Val(A(23).Text) * Val(B(30).Text)
an(19).Text = Val(A(18).Text) * Val(B(1).Text) + Val(A(19).Text) * Val(B(7).Text) + Val(A(20).Text) * Val(B(13).Text) + Val(A(21).Text) * Val(B(19).Text) + Val(A(22).Text) * Val(B(25).Text) + Val(A(23).Text) * Val(B(31).Text)
an(20).Text = Val(A(18).Text) * Val(B(2).Text) + Val(A(19).Text) * Val(B(8).Text) + Val(A(20).Text) * Val(B(14).Text) + Val(A(21).Text) * Val(B(20).Text) + Val(A(22).Text) * Val(B(26).Text) + Val(A(23).Text) * Val(B(32).Text)
an(21).Text = Val(A(18).Text) * Val(B(3).Text) + Val(A(19).Text) * Val(B(9).Text) + Val(A(20).Text) * Val(B(15).Text) + Val(A(21).Text) * Val(B(21).Text) + Val(A(22).Text) * Val(B(27).Text) + Val(A(23).Text) * Val(B(33).Text)
an(22).Text = Val(A(18).Text) * Val(B(4).Text) + Val(A(19).Text) * Val(B(10).Text) + Val(A(20).Text) * Val(B(16).Text) + Val(A(21).Text) * Val(B(22).Text) + Val(A(22).Text) * Val(B(28).Text) + Val(A(23).Text) * Val(B(34).Text)
an(23).Text = Val(A(18).Text) * Val(B(5).Text) + Val(A(19).Text) * Val(B(11).Text) + Val(A(20).Text) * Val(B(17).Text) + Val(A(21).Text) * Val(B(23).Text) + Val(A(22).Text) * Val(B(29).Text) + Val(A(23).Text) * Val(B(35).Text)
an(24).Text = Val(A(24).Text) * Val(B(0).Text) + Val(A(25).Text) * Val(B(6).Text) + Val(A(26).Text) * Val(B(12).Text) + Val(A(27).Text) * Val(B(18).Text) + Val(A(28).Text) * Val(B(24).Text) + Val(A(29).Text) * Val(B(30).Text)
an(25).Text = Val(A(24).Text) * Val(B(1).Text) + Val(A(25).Text) * Val(B(7).Text) + Val(A(26).Text) * Val(B(13).Text) + Val(A(27).Text) * Val(B(19).Text) + Val(A(28).Text) * Val(B(25).Text) + Val(A(29).Text) * Val(B(31).Text)
an(26).Text = Val(A(24).Text) * Val(B(2).Text) + Val(A(25).Text) * Val(B(8).Text) + Val(A(26).Text) * Val(B(14).Text) + Val(A(27).Text) * Val(B(20).Text) + Val(A(28).Text) * Val(B(26).Text) + Val(A(29).Text) * Val(B(32).Text)
an(27).Text = Val(A(24).Text) * Val(B(3).Text) + Val(A(25).Text) * Val(B(9).Text) + Val(A(26).Text) * Val(B(15).Text) + Val(A(27).Text) * Val(B(21).Text) + Val(A(28).Text) * Val(B(27).Text) + Val(A(29).Text) * Val(B(33).Text)
an(28).Text = Val(A(24).Text) * Val(B(4).Text) + Val(A(25).Text) * Val(B(10).Text) + Val(A(26).Text) * Val(B(16).Text) + Val(A(27).Text) * Val(B(22).Text) + Val(A(28).Text) * Val(B(28).Text) + Val(A(29).Text) * Val(B(34).Text)
an(29).Text = Val(A(24).Text) * Val(B(5).Text) + Val(A(25).Text) * Val(B(11).Text) + Val(A(26).Text) * Val(B(17).Text) + Val(A(27).Text) * Val(B(23).Text) + Val(A(28).Text) * Val(B(29).Text) + Val(A(29).Text) * Val(B(35).Text)
an(30).Text = Val(A(30).Text) * Val(B(0).Text) + Val(A(31).Text) * Val(B(6).Text) + Val(A(32).Text) * Val(B(12).Text) + Val(A(33).Text) * Val(B(18).Text) + Val(A(34).Text) * Val(B(24).Text) + Val(A(35).Text) * Val(B(30).Text)
an(31).Text = Val(A(30).Text) * Val(B(1).Text) + Val(A(31).Text) * Val(B(7).Text) + Val(A(32).Text) * Val(B(13).Text) + Val(A(33).Text) * Val(B(19).Text) + Val(A(34).Text) * Val(B(25).Text) + Val(A(35).Text) * Val(B(31).Text)
an(32).Text = Val(A(30).Text) * Val(B(2).Text) + Val(A(31).Text) * Val(B(8).Text) + Val(A(32).Text) * Val(B(14).Text) + Val(A(33).Text) * Val(B(20).Text) + Val(A(34).Text) * Val(B(26).Text) + Val(A(35).Text) * Val(B(32).Text)
an(33).Text = Val(A(30).Text) * Val(B(3).Text) + Val(A(31).Text) * Val(B(9).Text) + Val(A(32).Text) * Val(B(15).Text) + Val(A(33).Text) * Val(B(21).Text) + Val(A(34).Text) * Val(B(27).Text) + Val(A(35).Text) * Val(B(33).Text)
an(34).Text = Val(A(30).Text) * Val(B(4).Text) + Val(A(31).Text) * Val(B(10).Text) + Val(A(32).Text) * Val(B(16).Text) + Val(A(33).Text) * Val(B(22).Text) + Val(A(34).Text) * Val(B(28).Text) + Val(A(35).Text) * Val(B(34).Text)
an(35).Text = Val(A(30).Text) * Val(B(5).Text) + Val(A(31).Text) * Val(B(11).Text) + Val(A(32).Text) * Val(B(17).Text) + Val(A(33).Text) * Val(B(23).Text) + Val(A(34).Text) * Val(B(29).Text) + Val(A(35).Text) * Val(B(35).Text)
A Quick sample below, you need to tweak it much more until you end up with 3 loops inside each other.
To create the textboxes first -
Option Explicit
Private Sub Command1_Click()
Dim xText As Integer
xText = 1
For xText = 1 To 36
Load a(xText)
Load b(xText)
Load an(xText)
a(xText).Move a(0).Left, a(xText - 1).Top + 360, a(xText - 1).Width, 285
a(xText).Visible = True
a(xText) = a(xText - 1) + 1
b(xText).Move b(0).Left, b(xText - 1).Top + 360, b(xText - 1).Width, 285
b(xText).Visible = True
b(xText) = b(xText - 1) + 1
an(xText).Move an(0).Left, an(xText - 1).Top + 360, an(xText - 1).Width, 285
an(xText).Visible = True
Next xText
End Sub
To get the values calculated - ONLY A SAMPLE...
Private Sub Command2_Click()
Dim xSumtotal As Long, xMatrixA As Integer, xMatrixB As Integer
xSumtotal = 0
xMatrixA = 0
xMatrixB = 0
For xSumtotal = 0 To 35
If xMatrixA >= 36 Then
xMatrixA = 0
Else
xMatrixA = xMatrixA + 1
End If
If xMatrixB >= 36 Then
xMatrixB = 0
Else
xMatrixB = xMatrixB + 6
End If
an(xSumtotal) = Val(a(xMatrixA).Text) * Val(b(xMatrixB).Text) + Val(a(xMatrixA + 1).Text) * Val(b(xMatrixB + 6).Text) + Val(a(xMatrixA + 2).Text) * Val(b(xMatrixB + 6).Text) + Val(a(xMatrixA + 3).Text) * Val(b(xMatrixB + 6).Text) + Val(a(xMatrixA + 4).Text) * Val(b(xMatrixB + 6).Text) + Val(a(xMatrixA + 5).Text) * Val(b(xMatrixB + 6).Text)
''The if statement will not work as you will get an overflow error because you will be jumping to xMatrixA = 36 on line 6...
''You needt to use another 2 FOR/NEXT loops nested inside here to calculate through the 36x36...
''Not enough time to make it all work, this will however put you on the right track...
Next xSumtotal
End Sub
Hope this helps...

How to calculate latitude and longitude based on distance in laravel

This is my function
public function getRshow(){
$lats = $tenl[0]->latitude;
$longs = $tlog[0]->longitude;
$distance =$par_dis[0]->partner_distance;
$project = "SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( $lats - latitude) * pi()/180 / 2), 2) +COS( $lats * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longs - longitude) * pi()/180 / 2), 2) ))) as distance
FROM `abserve_renterpost` having distance <= $distance order by distance";
$la=DB::SELECT($project);
$lara=array();
foreach ($project as $key => $v) {
$lara[]=(get_object_vars($v));
}
}
My query is working good if I run only the query, but if I converted my query for value and array format it's not working, error is displayed as below.
(SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' - latitude) * pi()/180 / 2), 2) +COS( 27.7 * pi()/180) * COS(latitude * ' at line 1 (SQL: SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( 27.7 - latitude) * pi()/180 / 2), 2) +COS( 27.7 * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( 74.12399600000003
- longitude) * pi()/180 / 2), 2) ))) as distance
FROM `abserve_renterpost` having distance <= 200 order by distance))
I noticed $lats was 27.7 which is odd.
try this
$lats = $tenl[0]->latitude;
$longs = $tlog[0]->longitude;
$distance =$par_dis[0]->partner_distance;
$project = <<<SQL
SELECT * , (3956 * 2 * ASIN(
SQRT(
POWER(SIN(($lats - `latitude`) * pi() / 180 / 2), 2)
+ COS($lats * pi() / 180)
* COS(`latitude` * pi() / 180)
* POWER(SIN(($longs - `longitude`) * pi() / 180 / 2), 2)
))
) as 'distance'
FROM `abserve_renterpost`
WHERE distance <= $distance
ORDER BY distance;
SQL;
$la=DB::select($project);
$lara=array();
foreach ($project as $key => $v) {
$lara[]=(get_object_vars($v));
}

Escape values in multiples DB raw select in Laravel 4

In my query I have something like:
->select('User.id', 'city', 'lat', 'lng', 'comment', 'Comments.created_at', 'disponibility',
DB::raw("User.*, (count(Comments.user_id)) as note_count"),
DB::raw("User.*, (3956 * 2 * ASIN(SQRT( POWER(SIN((:lat - lat) * pi()/180 / 2), 2) +COS(:lat * pi()/180) * COS(lat * pi()/180) * POWER(SIN((:lng - lng) * pi()/180 / 2), 2) ))/0.621371192) as distance"),
["lat" => $lat, "lng" => $lng]
)
For a reason, it give me error that I don't know why
strtolower() expects parameter 1 to be string, array given
Anyone can help me please?
Much appreciated
The problem here is probably that you put your binding not as 2nd argument for DB::raw but as argument for select method.
Just try instead of above code:
->select('User.id', 'city', 'lat', 'lng', 'comment', 'Comments.created_at', 'disponibility',
DB::raw("User.*, (count(Comments.user_id)) as note_count"),
DB::raw("User.*, (3956 * 2 * ASIN(SQRT( POWER(SIN((:lat - lat) * pi()/180 / 2), 2) +COS(:lat * pi()/180) * COS(lat * pi()/180) * POWER(SIN((:lng - lng) * pi()/180 / 2), 2) ))/0.621371192) as distance", ["lat" => $lat, "lng" => $lng])
)

yii query not caching when using createCommand

I have the following query being logged in mysql which is not being cached.
SELECT
*,
round(priceperkm * IFNULL(KMPlanned, projects.KM),
4) AS NEWVALUE,
round(PRICE * IFNULL(KMPlanned, projects.KM),
2) AS TotalPriceForItem,
SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
2) AS TotalPrice,
if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)) AS TotalPercent
FROM
hdb.projects
join
biditems ON projects.id = biditems.project_id
join
lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id
left join
jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id
left join
detailsseismic ON jobsprocesscomplete.JOBNO = detailsseismic.JOBNO
where
projects.PROJID = '1407075'
and lookupprocess.ID = 16299
The Yii code is as follows.
$dependancy = new CDbCacheDependency("select last_modified_date from projects where PROJID = $projid");
$countJobs = Jobs::model()->cache(CACHE_TIMEOUT,$dependancy)->count(array("condition"=>"PROJID=$projid"));
foreach ($processstages as $k=>$v) {
$sql = "SELECT
*,
round(priceperkm * IFNULL(KMPlanned, projects.KM),
4) AS NEWVALUE,
round(PRICE * IFNULL(KMPlanned, projects.KM),
2) AS TotalPriceForItem,
SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
2) AS TotalPrice,
if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)) AS TotalPercent
FROM
hdb.projects
join
biditems ON projects.id = biditems.project_id
join
lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id
left join
jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id
left join
details".$type['type']." ON jobsprocesscomplete.JOBNO = details".$type['type'].".JOBNO
where
projects.PROJID = :pid
and lookupprocess.ID = :lid
";
$command = Yii::app()->db->cache(CACHE_TIMEOUT,$dependancy)->createCommand($sql); // need to set memecahce to 50m to cache this query
//Yii::log("select last_modified_date from projects where PROJID = $projid",CLogger::LEVEL_INFO, __METHOD__);
$command->bindValue(":lid",$k,PDO::PARAM_INT);
$command->bindValue(":pid",$projid,PDO::PARAM_INT);
Yii::log((memory_get_peak_usage(true))/1024/1024 . "MB",CLogger::LEVEL_INFO, __METHOD__);
$query = $command->query();
}
I cannot seem to figure out why it is not caching these results as the last_modified_date is not changing. i have set max item cache to 32m and memcache memory to 512.
I have rewrote my query to as below which gets cached but the createCommand way it does not
$criteria=new CDbCriteria;
$criteria->select = '
round(priceperkm * IFNULL(KMPlanned, t.KM),
4) AS NEWVALUE,
round(PRICE * IFNULL(KMPlanned, t.KM),
2) AS TotalPriceForItem,
SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
2) AS TotalPrice,
if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, t.KM)) * 100,
2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, t.KM)) * 100,
2)) AS TotalPercent';
$criteria->join = 'join biditems ON t.id = biditems.project_id ';
$criteria->join .= 'join lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id ';
$criteria->join .= 'left join jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id ';
$criteria->join .= 'left join details'.$type['type'].' ON jobsprocesscomplete.JOBNO = details'.$type['type'].'.JOBNO ';
$criteria->compare('t.PROJID',$projid);
$criteria->compare('lookupprocess.ID',$k);
Projects::model()->cache(CACHE_TIMEOUT,$dependancy)->findAll($criteria);
Try to put the following:
$result = Yii::app()->db->cache(self::CACHE_TIMEOUT,$dependancy)
->createCommand($sql)
->bindValue(":lid",$k,PDO::PARAM_INT);
->bindValue(":pid",$projid,PDO::PARAM_INT)
->findAll();
I assumed that CACHE_TIMEOUT was a constant in this class, but if it is defined in another class, then you need to adapt this part

Resources