Total of all days - mysql

I'm trying to get the total budgets of the last x days. I tried the following but the days without budgets are not being returned.
<?php
class Budget extends \Eloquent {
public static function getTotalBudgetsByDay($days = 31)
{
$budgetByDays = self::where('created_at', '>=', \Carbon\Carbon::now()->subDays($days))
->groupBy('date')
->orderBy('date', 'ASC')
->get([
\DB::raw('Date(created_at) as date'),
\DB::raw('COUNT(*) as total')
]);
return array_map(function ($row) {
return $row->getAttributes();
}, $budgetByDays->all());
}
}
$budgetsByDays = \Budget::getTotalBudgetsByDay();
Result:
Array (
[0] => Array
(
[date] => 2015-01-08
[total] => 2
)
[1] => Array
(
[date] => 2015-01-09
[total] => 1
)
[2] => Array
(
[date] => 2015-01-11
[total] => 7
)
)
in this case on 10 not appear in the array

Related

JsonResponse option parameter

In controller i have:
return response()->json(
[
'number' => (float)8
],
Response::HTTP_OK,
[],
JSON_PRESERVE_ZERO_FRACTION
);
This is output:
{
"number": 8
}
Is it possible to get 8.0?
This working good:
json_encode(['number' => (float)8],JSON_PRESERVE_ZERO_FRACTION);
UPDATE:
when i set response()->json from above to $variable and then print_r($variable) i get this:
(so it looks like it working, but with return in browser i get still 8 not 8.0)
Illuminate\Http\JsonResponse Object
(
[data:protected] => {"number":8.0}
[callback:protected] =>
[encodingOptions:protected] => 1024
[headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
(
[computedCacheControl:protected] => Array
(
[no-cache] => 1
[private] => 1
)
[cookies:protected] => Array
(
)
[headerNames:protected] => Array
(
[cache-control] => Cache-Control
[date] => Date
[content-type] => Content-Type
)
[headers:protected] => Array
(
[cache-control] => Array
(
[0] => no-cache, private
)
[date] => Array
(
[0] => Thu, 08 Feb 2018 11:27:34 GMT
)
[content-type] => Array
(
[0] => application/json
)
)
[cacheControl:protected] => Array
(
)
)
[content:protected] => {"number":8.0}
[version:protected] => 1.0
[statusCode:protected] => 200
[statusText:protected] => OK
[charset:protected] =>
[original] => Array
(
[number] => 8
)
[exception] =>
)
Use number_format():
json_encode(['number' => number_format(8, 1)]);
If number is coming as a string, use (float)$stringNumber instead of $stringNumber

Yii2 - Count and sum not include of result of ActiveQuery

Basically I have a legacy query in mysql that I will be implement into ActiveRecord,
Legacy Way, as expected
SELECT
a.container_seal,
a.size ,
count(a.size) as bundles,
sum(a.piece) as pieces
FROM pipe a
WHERE a.outgoing_pipe_id IS NULL
GROUP by a.container_seal, a.size
ORDER by a.container DESC
AR Way
$modelPipe = Pipe::find()
->select([
"container_seal",
"size",
"count(size) as bundles",
"sum(piece) as pieces"
])
->where(['outgoing_pipe_id' => NULL])
->groupBy(['container_seal', 'size'])
->orderBy(['container' => SORT_DESC])
->all();
But why, when I debug it,
<pre>
<?php
print_r($modelPipe);
?>
</pre>
The count and sum is not into.
Array
(
[0] => app\models\hanwa\Pipe Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[container_seal] => TEMU6099067/ EMCCSQ6566
[size] => 15 x 35 x 0.85 x 6000
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[container_seal] => TEMU6099067/ EMCCSQ6566
[size] => 15 x 35 x 0.85 x 6000
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[1] => app\models\hanwa\Pipe Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[container_seal] => TEMU6099067/ EMCCSQ6566
[size] => 35 x 35 x 0.75 x 6000
)
Is it Yii2 have another way to use sum and count in case 'select' ?
You shoudl add the pubblic var for bundles and pieces in your pipe model
(for recive the result of query )
class Pipe extends \yii\db\ActiveRecord
{
public $bundles;
public $pieces;
......

Cakephp Table query: SUM returning 0

I'm having problems when using find to sum the field num_days from table events.
$events = TableRegistry::get('Events');
$query = $events->find('all')
->select(['used' => 'sum(num_days)'])
->first();
Why $query->used is always 0?
print_r($events->find('all')->select(['used' => 'sum(num_days)'])->toArray()) gives,
Array ( [0] => App\Model\Entity\Event Object ( [used] => 6 [[new]] => [[accessible]] => Array ( [*] => 1 ) [[dirty]] => Array ( ) [[original]] => Array ( ) [[virtual]] => Array ( ) [[errors]] => Array ( ) [[invalid]] => Array ( ) [[repository]] => Events ) )
6 is exactly the correct answer for the query and print_r shows it but $query->used is returning always 0.
try $query->order(['used' => 'DESC']); before $query->used
Also, we can add used as protected $_virtual = ['used']; inside Event.php Entity File

How to get the first row of a result in Yii2? (queryRow Equivalent)

how can i get the first row of result? Below is my code which is giving me an error like this Undefined index: module
if ( substr( $action, 0, 4 ) === "stl_" )
{
$query = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
$action = \Yii::$app->db->createCommand( $query )
->queryAll();
//print_r($action);die();
$module = $action[ 'module' ];
$action = $action[ 'action' ];
}
$action has value
Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
in Yii1 i would have used
$action = \Yii::$app->db->createCommand( $query )
->queryRow();
You can use queryOne()
\Yii::$app->db->createCommand( $query )
->queryOne();
QueryOne()

Elasticsearch request how get the top 20 videos between two times

I need to find the 20 most views videos between two times, i did this:
$Param['body']='
{
"query" : {
"match_all" : {}
},
"facets" : {
"WhatIwant" : {
"range" : {
"key_field" : "time",
"value_field" : "video_id",
"ranges" : [
{ "from" :1399236597 , "to" : 1400331247 }
]
}
}
}
}';
I get a result where i can see some params, but i don't have the entire list look:
[facets] => Array
(
[WhatIwant] => Array
(
[_type] => range
[ranges] => Array
(
[0] => Array
(
[from] => 1400331247
[count] => 4585
[min] => -1
[max] => 3584
[total_count] => 4585
[total] => 12884198
[mean] => 2810.0758996728
)
)
)
)
)
I want something like this if it's possible :
[facets] => Array
(
[WhatIwant] => Array
(
[_type] => range
[ranges] => Array
(
[0] => Array
(
[time] => 1400331247
[video_id] => 4585
)
[1] => Array
(
[time] => 1400331248
[video_id] => 4582
)
[2] =>Array()
.....
....
[19]
)
)
)
)
Thanks for reading me
You just need a simple term facet, grouped by video_id with applied range filter