Improving/editing JSON from CakePHP 2 for Ext-JS 4 - php

Ok guys, I've got this from CakePHP:
array(
(int) 0 => array(
'Worker' => array(
'id' => '1',
'username' => 'davorlozic',
'first_name' => 'Davor',
'last_name' => 'Lozic',
'active' => true,
'created' => '2012-05-12 10:39:05',
'modified' => '2012-05-12 10:39:05'
)
),
(int) 1 => array(
'Worker' => array(
'id' => '2',
'username' => 'markomarkovic',
'first_name' => 'Marko',
'last_name' => 'Markovic',
'active' => true,
'created' => '2012-05-12 10:39:20',
'modified' => '2012-05-12 10:39:20'
)
So, when I encode it with JSON_ENCODE I get this:
BUT I NEED TO GET THIS: HOW? (this shape, not these data)
{
"success": true,
"Workers": [
{"id": 1, "name": 'Ed', "email": "ed#sencha.com"},
{"id": 2, "name": 'Tommy', "email": "tommy#sencha.com"}
]
}
Can I do something with the FIRST array? Some shifting or something? Thank you! :)

your current problem is that CakePHP and ExtJS use different data structures for read requests. You will probably later figure out that there are a lot more issues, and it can become quite tricky. I have had many CakePHP and ExtJS projects always facing these problems, it also gets really annoying to have to create custom json views for each request type and controller.
That's why I started together with some colleges Bancha, which automates all these things for you. The best place is to start at banchaproject.org and see the samples and docs.
For any questions feel free to write to support#banchaproject.org
Best regards
Roland

Related

Symfony set request parameters in array format

I'm trying to set up my own request which I could send to a method for testing purposes, but I can't seem to find how to format it.
Right now I'm trying to pull array out of the request
$data = $request->request->get('itemData');
It should look like this once I get it:
[
'basic' => [
'type' => 1
'country => 1
],
'information' => [
'wadding' => 1
]
]
I have written it like this:
$request->request->set("itemData[basic][type]", 2);
$request->request->set("itemData[information][wadding]", 0);
But I get null since the key is not itemData but for example itemData[basic][type]
How do I format it in the set command so the request parameters are sent in array form?
Figured it out
$request->request->set("itemData",
[
'basic' => ['type' => 2, 'country' => 1],
'information' => ['wadding' => 0]
]
);

Unable to access comment meta?

The Setup
Using the Reviews extension for WP Job Manager, I have comments with the meta field review_average which is an average of `review_stars. I'm customizing the WP REST API so I can read and write comments remotely via the mobile app I'm building.
I've exposed these fields with
register_meta( 'comment', 'review_average', array( 'show_in_rest' => true,
'type' => 'string',
'description' => 'review_average',
'single'=> false));
register_meta( 'comment', 'review_stars', array( 'show_in_rest' => true,
'type' => 'string',
'description' => 'review_stars',
'single'=> false));
which results in this field in the REST API response for comments:
meta: {
review_average: [
"4"
],
review_stars: [
"Array"
]
},
(I can't seem to break down that array, but there's only one stars category so the average is fine)
I've written a create_review function that uses add_comment_meta to write to review_average and review_stars which successfully gives the comment the right stars. Both those meta values are required for it to work.
function create_review($param) {
$id = wp_insert_comment(array('comment_post_ID' => $param['post_id'],
'comment_author' => $param['username'],
'comment_author_email' => $param['email'],
'user_id' => $param['user_id'],
'comment_content' => $param['content']));
if ($id) add_comment_meta($id, 'review_average', $param['rating']);
if ($id) add_comment_meta($id, 'review_stars', array('Your Rating'=>$param['rating']));
return get_comment($id);
}
The Problem
I can't seem to get the ratings meta info into a response for the comments. On my way to writing the "index" function get_comments, I've written the "show" function, get_commment:
function get_review($param) {
$id = $param['id'];
$info = get_comment($id);
$res = array(
'id' => $id,
'author_name' => $info->comment_author,
'author_email' => $info->comment_author_email,
'author_id' => $info->user_id,
'date' => $info->comment_date,
'rating' => $info->review_average
);
return $res;
}
The response has rating: null. Same result with 'rating' => $info->meta->review_average, as well as using _review_average in both those scenarios.
I have another function for my custom posts, which are job_listings that in my app are customers.job_listing has a meta field that shows up in the default REST API response under meta as _job_location, but inside my get_customer function, $res['address'] = $info->_job_location; works just fine!
How do I get the damn rating_average meta!?
Well, 'rating' => get_comment_meta($id) inside my get_review method gives me this:
"rating": {
"review_average": [
"4"
],
"review_stars": [
"a:1:{s:11:\"Your Rating\";s:1:\"4\";}"
]
}
And then
'rating' => get_comment_meta($id)['review_average'][0],
'rating_info' => get_comment_meta($id),
Gives me a nice full
"rating": "4",
"rating_info": {
"review_average": [
"4"
],
"review_stars": [
"a:1:{s:11:\"Your Rating\";s:1:\"4\";}"
]
}
I'm learning php as I go, so I'd love if someone could post a comment about why
get_comment_meta($id)->review_average
returns null but
get_comment_meta($id)['review_average']
works.

Youtube embed start time convert to time in seconds directly in array from regex group

I want to extend one php package which returns embed code from YouTube. I want to extend it and make embed code with start time querystring.
Piece of Code:
'website' => 'http://youtube.com',
'ssl' => true,
'url' => [
'^(https?://)?(?:www\.)?youtu\.be/([0-9a-zA-Z-_]{11})',
'^(https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:.+&t=)(\S+)',
],
'info' => [
'id' => '{1}',
'url' => '{protocol}://youtu.be/{1}',
'dataUrl' => '{protocol}://gdata.youtube.com/feeds/api/videos/{1}?v=2&alt=jsonc',
'imageRoot' => '{protocol}://img.youtube.com/vi/{1}/',
],
'render' => [
// iframe attributes
'sizeRatio' => 1.77,
'iframe' => [
'src' => '{protocol}://www.youtube.com/embed/{1}?rel=0&wmode=transparent&start={3}',
'width' => 560,
'height' => 315,
'allowfullscreen' => null,
'frameborder' => 0,
],
{3} is a start time from YouTube in format like 1h22m59s or 22m59s or 59s. I am getting {3} from regex from the last group : (?:.+&t=)(\S+)',
Is it possible to run function directly in array value to convert {3} time in seconds? For embed videos I need &start=123 (not ?start=1h22m59s )
I solved it by editing package source. It was the best solution.

Is there a query language to filter array?

I was wondering if there is a library that implements a SQL-like interface to access data in array, e.g.
Input:
[
['name' => 'Tom', 'age' => 27, 'location' => ['country' => 'GB']],
['name' => 'Jerry', 'age' => 16, 'location' => ['country' => 'LT']],
['name' => 'Stuart', 'age' => 26, 'location' => ['country' => 'GB']]
]
Fictional query:
SELECT name, location.country FROM {input} WHERE age > 18 ORDER BY age DESC
Which would produce a variation of:
[
['name' => 'Tom', 'location.country' => 'GB'],
['name' => 'Tom', 'location.country' => 'GB']
]
Note, I am perfectly aware of array_filter and alike implementations that I could put together on the spot. I am looking for query like interface to access data.
You can use LINQ, with PHP implementations such as phpLinq or LINQ for PHP or plinq
I found the following to be the closest of all available alternatives:
https://github.com/braincrafted/arrayquery
ArrayQuery does not provide query language, though it does provide query API, e.g.
$thorinsCompany = [
[ 'name' => 'Bilbo Baggins', 'race' => 'Hobbit' ],
[ 'name' => 'Gandalf', 'race' => 'Wizard' ],
[ 'name' => 'Thorin Oakenshild', 'race' => 'Dwarf' ],
[ 'name' => 'Balin', 'race' => 'Dwarf'],
[ 'name' => 'Bifur', 'race' => 'Dwarf'],
// ...
];
$query->from($thorinsCompany);
$query->where('race', 'Dwarf')
$query->where('age', 50, '>');
$results = $query->findAll();
On a related note, Mongo PHP extension provides SQL to Mongo API translation, http://www.php.net/manual/en/mongo.sqltomongo.php. The basic logic could be used as a guiding example for someone who set their foot on developing SQL interface to PHP arrays.
I suppose the reason why there aren't any actively used SQL-like query languages for arrays in PHP is performance. Using something like ArrayQuery (which I am the author of) already results in a performance penalty. A query language would result in further decreased performance which makes most of the time no sense.
ArrayQuery was also inspired by Doctrines QueryBuilder, which is, a way to transform SQL into system based on objects that is more natural in a OOP language like PHP.

How to log dimension like AWS CloudWatch bundled metric

Can anyone explain how to use the AWS PHP SDK to log the metric in the style like the above screen.
I use the following PHP code but the select menu is showing "ELB: AvaliabiltyZone", how to make it show "Aggregated by AvaliabiltyZone"? What is the logic used here?
$response = $cw->put_metric_data("ELB", array(
array(
"MetricName" => "Latency",
"Dimensions" => array(
array("Name" => "AvaliabiltyZone" , "Value" => "us-east-1c")
),
"Timestamp" => "now",
"Value" => 1,
"Unit" => "None"
),
));
AvaliabiltyZone
You misspelled "AvailabilityZone"
This maybe won't answer the question, but it might fix some things...
$cw = new AmazonCloudWatch();
$cw->set_region('monitoring.'.$region.'amazonaws.com');
$res1 = $CW->put_metric_data('Sys/RDS' ,
array(array('MetricName' => 'Uptime' ,
'Dimensions' => array(array('Name' => 'AvaliabiltyZone',
'Value' => 'us-east-1c')),
'Value' => $Uptime,
'Unit' => 'Seconds')));
Click Here

Categories