Write php array in HBase using thrift - php

I have a Thrift php client and I want to write in a HBase table and I'm doing the following:
$mutations = array(
new Mutation( array(
'column' => 'entry:num',
'value' => array('a','b','c')
) ),
);
$client->mutateRow( $t, $row, $mutations );
The problem is that when inserting in HBase the value, which is an array, gets converted to 'Array' instead of storing the elements of the array.
How can I store the list as an array (or byte array)

A HBase mutation object has required three fields with boolean/text values, not arrays. So you need to turn any structured value into a string, something like.
$mutations = array(
new Mutation( array(
'isDelete' => FALSE,
'column' => 'entry:num',
'value' => serialize(array('a','b','c'))
) ),
);
$client->mutateRow( $t, $row, $mutations );
The definition of the HBase Thrift API is here;
http://svn.apache.org/viewvc/hbase/trunk/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift?view=markup
I haven't tested this.

I must admit that I do not have a clue what you're trying to do (perhaps due to a lack of knowledge regarding Thrift and HBase), but if I understood your question correctly, you're trying to write some PHP data structure (array in this case) to a storage media. To achieve this you have to serialize your data somehow. That could be using a custom XML serialization, a custom binary serialization or, perhaps the most simple solution, the PHP internal serialization mechanism provided by serialize() and the corresponding unserialize().
If you strive for inter-language-interoperability you should use a custom serialization or you have to write a unserialization function that unserializes the PHP serialization format in your target language.
Just a quick example - I don't know where you'd have to put this code, as I don't know exactly what you're doing:
$mutations = array(
new Mutation(array(
'column' => 'entry:num',
'value' => array('a','b','c')
)),
);
$data = serialize($mutations); // $data now is a string
// write $data to storage
// read $readData from storage
$readMutations = unserialize($readData);
// $readMutations == $mutations
// (but the Mutation instances are not the same instances any more)
Please seee
serialize()
unserialize()
Serializing objects - objects in sessions

Related

Updating Meta Value doesn't work properly with serialized data in WordPress

I was wondering if someone could help with this. I'm trying to do a php query to update post meta values on Wordpress. Pb : the string has to be very long and full of unorthodox characters like double quotes.
The result is somewhat strange as it gets updated but adds another serial around the value, breaking it, like this :
s:5086:"a:28:{i:0;a:7:.....
Here's the initial query, if someone could figure out how to make it compliant that'd be amazing :
update_post_meta( $product_id, '_wc_booking_pricing', 'a:28:{i:0;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.027";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"1";s:2:"to";s:1:"3";}i:1;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.02914814815";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"4";s:2:"to";s:1:"4";}i:2;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0312962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"5";s:2:"to";s:1:"5";}i:3;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03344444444";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"6";s:2:"to";s:1:"6";}i:4;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03559259259";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"7";s:2:"to";s:1:"7";}i:5;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03774074074";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}i:6;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03988888889";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"9";s:2:"to";s:1:"9";}i:7;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04203703704";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"10";s:2:"to";s:2:"10";}i:8;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04418518519";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"11";s:2:"to";s:2:"11";}i:9;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04633333333";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"12";s:2:"to";s:2:"12";}i:10;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04848148148";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"13";s:2:"to";s:2:"13";}i:11;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05062962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"14";s:2:"to";s:2:"14";}i:12;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05277777778";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"15";s:2:"to";s:2:"15";}i:13;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05492592593";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"16";s:2:"to";s:2:"16";}i:14;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05707407407";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"17";s:2:"to";s:2:"17";}i:15;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05922222222";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"18";s:2:"to";s:2:"18";}i:16;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06137037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"19";s:2:"to";s:2:"19";}i:17;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06351851852";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"20";s:2:"to";s:2:"20";}i:18;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06566666667";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"21";s:2:"to";s:2:"21";}i:19;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06781481481";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"22";s:2:"to";s:2:"22";}i:20;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06996296296";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"23";s:2:"to";s:2:"23";}i:21;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07211111111";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"24";s:2:"to";s:2:"24";}i:22;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07425925926";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"25";s:2:"to";s:2:"25";}i:23;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07640740741";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"26";s:2:"to";s:2:"26";}i:24;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07855555556";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"27";s:2:"to";s:2:"27";}i:25;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0807037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"28";s:2:"to";s:2:"28";}i:26;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.08285185185";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"29";s:2:"to";s:2:"29";}i:27;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.085";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"30";s:2:"to";s:2:"30";}}');
First, it's worth noting that you are not doing a PHP query, but rather you are using WordPress functions (update_post_meta) to save this data. That is probably where the confusion lies.
The problem you are having is that you are passing in serialized data. In WordPress, even though it stores the data in the DB as serialized data, you do not have to serialize / unserialize yourself.
WordPress stores the data serialized "automatically". That means that when you call update_post_meta, you should simply pass in the array you are trying to save.
This is a minimal example based on your code above:
$data = array(
array(
'type' => 'blocks',
'cost' => NULL,
'modifier' => NULL,
'base_cost' => 0.027,
'base_modifier] => 'times',
'from' => 1,
'to' => 3
),
array(
'type' => 'blocks',
'cost' => NULL,
'modifier' => NULL,
'base_cost' => 0.02914814815,
'base_modifier] => 'times',
'from] => 4,
'to] => 4
)
);
update_post_meta( $product_id, '_wc_booking_pricing', $data );
Then, when you retreive the data using get_post_meta, it will return it as an array, exactly as you saved it.
The string that you are trying to pass through update_post_meta() is serialized data and needs first to be unserialized.
Two possible cases:
1) If you have serialize that data, just don't do it and pass the data array directly in update_post_meta().
Note that a "passed array will be serialized into a string" when using update_post_meta(), so in your case to avoid a kind of double serialization that will make your data unreadable.
2) If you have not serialize that data yourself, you might need to unserialize this data first. For that you can use the WordPress function maybe_unserialize() before using update_post_meta(), this way:
// Unserializing the data
$data = maybe_unserialize( 'a:28:{i:0;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.027";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"1";s:2:"to";s:1:"3";}i:1;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.02914814815";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"4";s:2:"to";s:1:"4";}i:2;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0312962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"5";s:2:"to";s:1:"5";}i:3;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03344444444";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"6";s:2:"to";s:1:"6";}i:4;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03559259259";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"7";s:2:"to";s:1:"7";}i:5;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03774074074";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}i:6;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03988888889";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"9";s:2:"to";s:1:"9";}i:7;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04203703704";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"10";s:2:"to";s:2:"10";}i:8;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04418518519";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"11";s:2:"to";s:2:"11";}i:9;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04633333333";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"12";s:2:"to";s:2:"12";}i:10;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04848148148";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"13";s:2:"to";s:2:"13";}i:11;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05062962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"14";s:2:"to";s:2:"14";}i:12;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05277777778";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"15";s:2:"to";s:2:"15";}i:13;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05492592593";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"16";s:2:"to";s:2:"16";}i:14;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05707407407";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"17";s:2:"to";s:2:"17";}i:15;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05922222222";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"18";s:2:"to";s:2:"18";}i:16;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06137037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"19";s:2:"to";s:2:"19";}i:17;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06351851852";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"20";s:2:"to";s:2:"20";}i:18;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06566666667";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"21";s:2:"to";s:2:"21";}i:19;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06781481481";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"22";s:2:"to";s:2:"22";}i:20;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06996296296";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"23";s:2:"to";s:2:"23";}i:21;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07211111111";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"24";s:2:"to";s:2:"24";}i:22;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07425925926";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"25";s:2:"to";s:2:"25";}i:23;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07640740741";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"26";s:2:"to";s:2:"26";}i:24;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07855555556";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"27";s:2:"to";s:2:"27";}i:25;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0807037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"28";s:2:"to";s:2:"28";}i:26;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.08285185185";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"29";s:2:"to";s:2:"29";}i:27;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.085";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"30";s:2:"to";s:2:"30";}}' );
// Saving the data
update_post_meta( $product_id, '_wc_booking_pricing', $data );
Now your data is correctly saved in database and can be read and/or processed.

How to construct a graphql mutation query request in php

I am having the hardest time figuring out how to properly format a graphql api mutation POST request in php.
If I hard code the string and use it as the data in my POST request it works like this:
'{"query":"mutation{addPlay(input: {title: \"two\"}){ properties { title } } }"}'
But if I have a php array of the input values:
$test_data = array(
'title' => 'two'
);
I can't seem to format it correctly. json_encode also puts double quotes around the keys which graphql is rejecting with the error Syntax Error GraphQL request (1:26) Expected Name, found String.
I ultimately need a solution that will convert a larger more complex array to something usable.
Reformatting the query allowed me to use JSON directly.
So my new query looks like this:
$test_data = array(
'title' => 'two'
);
$request_data = array(
'query' => 'mutation ($input: PlayInput) { addPlay(input: $input) { properties { title } }}',
'variables' => array(
'input' => $test_data,
),
);
$request_data_json = json_encode($request_data);
Then the $request_data_json is used in a POST http request.

How To Add Additional Field Mappings To Code Pulling from Third Party API

I am working with the Church Community Builder API (official docs, search for "group profile" to see relevant code) and with CCB Core WordPress Plugin to sync groups data from the API into custom post types (fields, taxonomies).
CCB Core maps a lot of this data automatically, but it doesn't map the group ID value CCB uses to distinguish one group from another and I'd like to sync it.
The relevant code is kept in the functions get_groups_custom_fields_map() and get_groups_taxonomy_map().
I've tried adding something like the following to get_groups_taxonomy_map():
'group_id' => array (
'api_mapping' => 'group',
'data_type' => 'integer',
),
But this doesn't work. Any suggestions on writing code to pull in this field?
UPDATE 9/10/16 7:25 PM:
I think I am wicked close.
I took the XML from the API documentation under Group Profile and ran it through simplexml and generated this output.
I then compared the output with how the get_groups_custom_fields_map and get_groups_custom_taxonomy_map was organized, which led me to believe the following should work:
'group' => array (
'api_mapping' => 'group',
'data_type' => 'object',
'child_object' => array(
'api_mapping' => '#attributes',
'data_type' => 'object',
'child_object' => array(
'group_id' => array(
'api_mapping' => 'id',
'data_type' => 'integer'
)
)
)
),
But it doesn't. What am I missing?
The SimpleXML representation of the response that comes back from CCB just so happens to have a really frustrating way of deserializing the IDs for the entities (Groups, Events, Individuals, etc) that come out of the API. (And it only happens on the IDs).
Even though Group ID looks like a property in the XML response string, it's actually an attribute which is slightly different. In your object response, notice that # symbol in the [#attributes] node. PHP will not allow direct access to that.
See "At sign" # in SimpleXML object?
As a workaround I actually obtain the Group ID like so right here:
https://github.com/jaredcobb/ccb-core/blob/1a6e88b47ad7d5293e88bac277e72cbc4e33a602/admin/class-ccb-core-sync.php#L589-L595
Code snippet of the above reference:
$group_id = 0;
foreach( $group->attributes() as $key => $value ) {
if ( $key == 'id' ) {
$group_id = (int) $value;
break;
}
}
So in general, we do have access to that group id, we just need to now save it as post meta. I'll update the plugin to take advantage of these IDs, but in the mean time you can use the $group_id variable directly and save the post meta in this function.

Symfony convert entity to json format by looping foreach

I have a little REST API in my project. And ofcourse i use json as my return data to work with.
I am using symfony in the backend and angularJs in the frontend. At the moment i convert my entity to json by looping true my result and filling an data array to return as json.
EXAMPLE:
public function getAction($id)
{
$em = $this->getDoctrine()->getManager();
$warehouseId = $this->get('session')->get('warehouse');
$warehouse = $em->getRepository('BubbleMainBundle:Warehouse')->find($warehouseId);
$trip = $em->getRepository('BubbleMainBundle:Trip')->find($id);
$data = array(
'id' => $trip->getId(),
'driver' => $trip->getDriver(),
'status' => $trip->getStatus(),
'date' => $trip->getPlanningDate()->format('Y-m-d')
);
if ( count($trip->getStops()) > 0 ) {
foreach($trip->getStops() as $stop)
{
$data['assignedStops'][] = array(
'id' => $stop->getId(),
'status' => $stop->getStatus(),
'date' => $stop->getDeliveryDate()->format('Y-m-d'),
'sort' => $stop->getSort(),
'company' => array(
'name' => $stop->getToCompany()->getName(),
'lat' => $stop->getToCompany()->getLat(),
'lng' => $stop->getToCompany()->getLng(),
'address' => $stop->getToCompany()->getAddress(),
'zip' => $stop->getToCompany()->getZip()
),
);
}
} else {
$data['assignedStops'][] = '';
}
$response = new jsonResponse();
$response->setData($data);
return $response;
}
This works. But sometimes i have have (google chrome timeline) waiting responses of 6 seconds for just a simple query and json response.
Is looping true the entity to much? Or do i need another approach for converting my entities to json format?
thx anthony,
If you are using PHP 5.4 or above then considering using the JsonSerializable interface with your entities:
http://www.php.net/manual/en/class.jsonserializable.php
This will allow you to control how your entities are converted to JSON which will allow you to call json_encode directly on your entities without having to loop through and convert them to arrays.
As for the performance issue you'd need to profile your script to find out where the performance bottleneck is. From looking at your code one potential issue that you might want to look into is to make sure you are fetching all the data in your original query (i.e stops and companies) and you are not executing additional queries in the foreach loop to fetch the missing stop and company data.
I recommend you (since you are using Symfony2 as a backend and you need an API) to definitely try out this bundle... It's easy to use and to setup and as an extra you can also generate a nice documentation for it.
It will speed up your development and code.

How do you arrange PHP objects into arrays for display

I'm a PHP developer who typically uses arrays in lieu of objects, and I'd like to know if there are any best practices for object manipulation based on key values.
Take the following multi-dimensional array of events, typical of something you'd pull directly out of the database.
$events = array(
0=>array(
'eventid'=>1,
'title'=>'First Event',
'date'=>'20131010'
),
1=>array(
'eventid'=>2,
'title'=>'Second Event',
'date'=>'20131022'
),
2=>array(
'eventid'=>3,
'title'=>'Third Event',
'date'=>'20131010'
),
),
For display purposes in my template, I want to make this a multi-dimensional array based on the date first. This is fairly easy transformation that I use a helper function for.
assoc($events,'date','eventid');
Resulting in:
$events = array(
20131010=>array(
1 => array(
'eventid'=>1,
'title'=>'First Event',
'date'=>'20131010'
),
3 => array(
'eventid'=>3,
'title'=>'Third Event',
'date'=>'20131010'
),
),
20131022=>array(
2 => array(
'eventid'=>1,
'title'=>'First Event',
'date'=>'20121010'
),
),
),
In this way, I can easily run through the array on the template side for each date, create a header and for that date, and then display events for that day underneath.
I could go ahead and try to make a similar assoc() function for Objects, but it strikes me this should be a fairly common thing and there may be a standard way of doing it. If you had an object instead of an array for $events, how would you go about formatting it the second way I've described, or would you?
One additional piece of information: I am using CodeIgniter, in case it has some helper functions that I'm not aware of that could be useful.
In your assoc function you could do an is_object check and cast to an array if it is an object.
function your_assoc_func($events){
if(is_object($events)){
$events = (array)$events
}
// If the object is more complex cast any containing objects you may run across
foreach($events as $event){
if(is_object($event)){
$event = (array)$event
}
}
}

Categories