SQL query is not working in CAKE PHP 3? - cakephp-3.0

this query is perfectly working in mysql & return only one row.
SELECT * FROM `civil_service_mileage_rates` WHERE `min_distance` <400
and `max_distance` >400 and `min_capacity`<1300 and `max_capacity`>1300
But when i tried the same in cakephp3 , it return many rows data instead of one.
cakephp3 code:
$civilserviceMileageTable = TableRegistry::get('civil_service_mileage_rates');
$mileagedata = $civilserviceMileageTable->find('all');
$claimAmount = $mileagedata->where(['min_distance'<400
and 'max_distance'>400 and 'min_capacity'<1300 and 'max_capacity'>1300]);
please help..thanks in advance
Updated:
output of above code
"data": [
{
"id": 1,
"band": "Band 1",
"min_distance": "0",
"max_distance": "1500",
"min_capacity": "0",
"max_capacity": "1200",
"claim_amount": 37.95
},
{
"id": 2,
"band": "Band 1",
"min_distance": "0",
"max_distance": "1500",
"min_capacity": "1201",
"max_capacity": "1500",
"claim_amount": 39.86
},
{
"id": 3,
"band": "Band 1",
"min_distance": "0",
"max_distance": "1500",
"min_capacity": "1501",
"max_capacity": "2500",
"claim_amount": 44.79
}
Though, it should have display only one row.
"data": [
{
"id": 2,
"band": "Band 1",
"min_distance": "0",
"max_distance": "1500",
"min_capacity": "1201",
"max_capacity": "1500",
"claim_amount": 39.86
}
}
updated :
after print the array details, i got the result.
Array
(
[0] => Cake\ORM\Entity Object
(
[id] => 1
[band] => Band 1
[min_distance] => 0
[max_distance] => 1500
[min_capacity] => 0
[max_capacity] => 1200
[claim_amount] => 37.95
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => civil_service_mileage_rates
)
[1] => Cake\ORM\Entity Object
(
[id] => 2
[band] => Band 1
[min_distance] => 0
[max_distance] => 1500
[min_capacity] => 1201
[max_capacity] => 1500
[claim_amount] => 39.86
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => civil_service_mileage_rates
)
[2] => Cake\ORM\Entity Object
(
[id] => 3
[band] => Band 1
[min_distance] => 0
[max_distance] => 1500
[min_capacity] => 1501
[max_capacity] => 2500
[claim_amount] => 44.79
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => civil_service_mileage_rates
)
)

try this
$this->civil_service_mileage_rates = TableRegistry::get('civil_service_mileage_rates');
$this->civil_service_mileage_rates->find('all')
->where([
'min_distance <' => 400,
'max_distance >' => 400,
'min_capacity <' => 1300,
'max_capacity >' => 1300,
]);
$result = $this->civil_service_mileage_rates->toArray();

Try this one hope it will solve your problem.
$civilserviceMileageTable = TableRegistry::get('civil_service_mileage_rates');
$mileagedata = $civilserviceMileageTable->find();
$mileagedata->where(['min_distance <'=> 400]);
$mileagedata->where(['max_distance >' => 400]);
$mileagedata->where(['min_capacity <' => 1300]);
$mileagedata->where(['max_capacity >' => 1300]);
$claimAmount = $mileagedata->toArray();

Related

one query for all array data! result is by separating for each id of array

I am searching all names for each transaction id.
Eg.,
id 1 have user A, B.
id 2 have user A.
id 3 have user A, C.
I can get user names which belong to each Id like that
foreach ($Ids in $id){
$query = [
'fields' => [
'USER.name'
],
'joins' => [
[
'type' => 'LEFT',
'table' => 'users',
'alias' => 'USER',
'conditions' => [
'TransactionUser.user_id=USER.id'
]
]
],
'conditions' => [
'TransactionUser.transaction_id' => $id
]
];
$this->find('all', $query);
}
But the problem is my leader don't want to loop query and want to do with only one query.
So I try like this.
$query = [
'fields' => [
'USER.name',
'TransactionUser.transaction_id'
],
'joins' => [
[
'type' => 'LEFT',
'table' => 'users',
'alias' => 'USER',
'conditions' => [
'TransactionUser.user_id=USER.id'
]
]
],
'conditions' => [
'TransactionUser.transaction_id' => $Ids
]
];
$this->find('all', $query);
It's work in cakePHP and result is like that.
Array
(
[0] => Array
( ([name] => A), ([transaction_id] => 1))
[1] => Array
( ([name] => B), ([transaction_id] => 1))
[2] => Array
( ([name] => A), ([transaction_id] => 2))
[3] => Array
( ([name] => A), ([transaction_id] => 3))
[4] => Array
( ([name] => C), ([transaction_id] => 3))
)
But I want data like that somehow.
Array
(
[0] => Array
( ([name] => A, B), ([transaction_id] => 1))
[1] => Array
( ([name] => A), ([transaction_id] => 2))
[2] => Array
( ([name] => A, C), ([transaction_id] => 3))
)
I try to use 'group' in query but it's not work well. I'm newbee in cakePHP query. Please help me if you know howto.
I just want to know some Query keyword of cakePHP for this case to be smart but it's look like not have.
So just loop result for desired array.

CakePHP 3 - Model - Accessing data from third layer of array

CakePHP 3
My Model
Users has Many Badges
Titles has Many Badges
Badges belongs to Titles
Badges belongs to Users
In my Users View, I want to access the value of Titles but I don't know how.
**Users Controller**
public function view($id = null)
{
$this->viewBuilder()->layout('admin');
TableRegistry::get('Users');
$user = $this->Users->get($id, [
'contain' => ['Badges', 'Projects', 'Reports', 'Rewards']
]);
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
UsersTable.php
$this->hasMany('Badges', [
'foreignKey' => 'user_id'
]);
TitleTable.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Titles', [
'foreignKey' => 'title_id',
'joinType' => 'INNER'
]);
BadgesTable.php
$this->hasMany('Badges', [
'foreignKey' => 'title_id'
]);
App\Model\Entity\User Object
(
[id] => 1
[firstname] => Jl
[lastname] => C
[email] => m
[username] => admin
[password] =>
[profile_pic] => /img/noimage.jpg
[report_count] => 0
[role] => admin
[reputation] => 999999
[account_status] => Active
[confirmation] => 1
[site_name] => C
[site_email] => mi
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-27T13:46:51+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2017-03-01T10:37:50+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[badges] => Array
(
[0] => App\Model\Entity\Badge Object
(
[id] => 1
[user_id] => 1
[title_id] => 11
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-28T15:16:08+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-28T15:16:08+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => Badges
)
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[repository]] => Users
)
What I want is to get the data from title table (i.e name)
[badges] => Array
(
[0] => App\Model\Entity\Badge Object
(
[id] => 1
[user_id] => 1
[title_id] => 11
[created] => Cake\I18n\FrozenTime Object
In your contain, you can use 'Model.Model' to get further related data like so :
$user = $this->Users->get($id, [
'contain' => ['Badges.Titles', 'Projects', 'Reports', 'Rewards']
]);

Facebook AdSet API "Your Budget is Too Low" error

Problem
I made a request to Facebook's Ad Set API attempting to create an ad set with the following JSON blob, and the error I received was "Your Budget Is Too Low. The minimum budget for this ad set is $32.04."
The ad set is set to use auto_bid and a valid lifetime_target. The ad set runs for 31 days, and with a $1 minimum daily budget, setting $64 for lifetime budget should've been sufficient.
Attempted Solutions
I tried to set a higher lifetime budget: from 64 to 1000 to 5000. 1000 did not work in this case, but 5000 did.
JSON Blob Used
Array
(
[account_id] =>
[adset_schedule] =>
[bid_amount] =>
[billing_event] => IMPRESSIONS
[budget_remaining] =>
[campaign_id] => 6054825216096
[created_time] =>
[creative_sequence] =>
[daily_budget] =>
[end_time] => 2016-11-08T07:59:59+0000
[id] =>
[is_autobid] => 1
[lifetime_budget] => 64
[lifetime_imps] =>
[name] => Some-Test-Campaign
[optimization_goal] => LINK_CLICKS
[pacing_type] =>
[recommendations] =>
[rf_prediction_id] =>
[start_time] => 2016-10-07T07:00:00+0000
[updated_time] =>
[targeting] => FacebookAds\Object\TargetingSpecs Object
(
[data:protected] => Array
(
[genders] =>
[age_min] =>
[age_max] =>
[geo_locations] => Array
(
[zips] => Array
(
[0] => Array
(
[key] => US:98004
)
[1] => Array
(
[key] => US:98005
)
[2] => Array
(
[key] => US:98006
)
[3] => Array
(
[key] => US:98007
)
[4] => Array
(
[key] => US:98008
)
[5] => Array
(
[key] => US:98009
)
[6] => Array
(
[key] => US:98011
)
[7] => Array
(
[key] => US:98014
)
[8] => Array
(
[key] => US:98015
)
[9] => Array
(
[key] => US:98019
)
[10] => Array
(
[key] => US:98021
)
)
)
[geo_markets] =>
[excluded_geo_locations] => Array
(
[countries] => Array
(
)
)
[exclusions] =>
[user_adclusters] =>
[interests] =>
[user_os] =>
[user_device] =>
[wireless_carrier] =>
[page_types] => Array
(
[0] => desktopfeed
[1] => mobilefeed
[2] => mobileexternal
[3] => rightcolumn
)
[connections] =>
[excluded_connections] =>
[family_statuses] =>
[friends_of_connections] =>
[flexible_spec] => Array
(
[0] => Array
(
[interests] => Array
(
[0] => Array
(
[id] => 6003277229371
)
)
[behaviors] => Array
(
[0] => Array
(
[id] => 6017447625983
)
)
)
)
[behaviors] =>
[relationship_statuses] =>
[interested_in] =>
[life_events] =>
[location_types] =>
[politics] =>
[markets] =>
[industries] =>
[income] =>
[net_worth] =>
[home_type] =>
[home_ownership] =>
[home_value] =>
[ethnic_affinity] =>
[generation] =>
[household_composition] =>
[moms] =>
[office_type] =>
[education_schools] =>
[education_statuses] =>
[college_years] =>
[education_majors] =>
[work_employers] =>
[work_positions] =>
[locales] =>
[zips] =>
[custom_audiences] =>
[custom_locations] =>
[excluded_custom_audiences] =>
[dynamic_audience_ids] =>
[product_audience_specs] =>
[excluded_product_audience_specs] =>
)
)
[promoted_object] =>
[adlabels] =>
[product_ad_behavior] =>
[execution_options] =>
[configured_status] =>
[effective_status] =>
)
I shortened the targeting for simplicity sake, but there's over 100 postal codes being targetd.
The solution here is that lifetime_budget is denoted in cents, so $64 should be 6400.
You may check if the budget you set is not less of required.
Perform this check at the endpoint:
https://graph.facebook.com/v2.12/act_{ads_account_id}/minimum_budgets
With headers:
Authorization: Bearer {your token with 'ads_management' rights to this account}
Returns:
{
"data": [
{
"currency": "AED",
"min_daily_budget_imp": 300,
"min_daily_budget_video_views": 300,
"min_daily_budget_high_freq": 750,
"min_daily_budget_low_freq": 6000
},
{
"currency": "ARS",
"min_daily_budget_imp": 400,
"min_daily_budget_video_views": 400,
"min_daily_budget_high_freq": 1000,
"min_daily_budget_low_freq": 8000
}
]
}
PS. I see, that my response is 2 years late, but someone may find it useful.

How to read youtube data api v3 response in php

I have following code:
$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
That returns the following output:
Google_Service_YouTube_ChannelListResponse Object
(
[collection_key:protected] => items
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/sTnuE1bHO-tokx_mFFDt1ybN90g"
[eventId] =>
[itemsType:protected] => Google_Service_YouTube_Channel
[itemsDataType:protected] => array
[kind] => youtube#channelListResponse
[nextPageToken] =>
[pageInfoType:protected] => Google_Service_YouTube_PageInfo
[pageInfoDataType:protected] =>
[prevPageToken] =>
[tokenPaginationType:protected] => Google_Service_YouTube_TokenPagination
[tokenPaginationDataType:protected] =>
[visitorId] =>
[modelData:protected] => Array
(
[pageInfo] => Array
(
[totalResults] => 1
[resultsPerPage] => 1
)
[items] => Array
(
[0] => Array
(
[kind] => youtube#channel
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/ecOcHFmWyWQ7ToCD7-B1L36b4L4"
[id] => UCQO6uXy5maTpYvSa_yM--Bw
[brandingSettings] => Array
(
[channel] => Array
(
[title] => Vasim Padhiyar
[showRelatedChannels] => 1
[featuredChannelsTitle] => Featured Channels
[featuredChannelsUrls] => Array
(
[0] => UCw-TnDmYDQyjnZ5qpVWUsSA
)
[profileColor] => #000000
)
[image] => Array
(
[bannerImageUrl] => http://s.ytimg.com/yts/img/channels/c4/default_banner-vfl7DRgTn.png
)
[hints] => Array
(
[0] => Array
(
[property] => channel.featured_tab.template.string
[value] => Everything
)
[1] => Array
(
[property] => channel.banner.image_height.int
[value] => 0
)
[2] => Array
(
[property] => channel.modules.show_comments.bool
[value] => True
)
)
)
)
)
)
[processed:protected] => Array
(
)
)
I want to loop through modelData:protected variable to get the list of channels and its items data. Its json object so $yt_profiles->modelData:protected not working while accessing. Please help me to solve this issue.
Thanks in advance
You can reach it as in an array:
print_r ($yt_profiles['modelData']);
Your request:
$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
To acces to values Channel title for example:
$titleChannel = $yt_profiles["items"][0]["brandingSettings"]["channel"]["title"];
To acces to values bannerImageUrl for example:
$banner = $yt_profiles["items"][0]["brandingSettings"]["image"]["bannerImageUrl"];
Does this help you?
$yt_profiles = $youtube->channels->listChannels('id, brandingSettings', array('mine' => 'true',));
foreach ($yt_profiles['modelData']['items'] as $searchResult) {
switch ($searchResult['kind']) {
case 'youtube#channel':
$channels[]= array('id'=> $searchResult['id'],
'brandingSettings'=> $searchResult['brandingSettings']);
break;
}
}

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