I am trying to output users chat history and then put it into a JSON object I want the messages to come out in descending order but when I try $cursor->sort() is throwing Call to a member function sort() on a non-object
$cursor = $collection->findOne(array('chatbetween' => $channel_name));
$cursor->sort(array('messages' => -1));
$messages = array();
for($i=0; $i<count($cursor['messages']); $i++){
$object = array('message'=>$cursor['messages'][$i]['message'],
'time'=>date('Y-m-d\TH:i:s\Z', $cursor['messages'][$i]['time']->sec),
'user'=>$cursor['messages'][$i]['user']);
$messages[] = $object;
}
echo json_encode($messages);
Here is what a collection looks like.
"_id": ObjectId("4f3c19e37edae1723d000000"),
"chatbetween": "private-4f3bb96d7edae1850b0000004f3c0d2d7edae1e935010000",
"messages"▼: {
"0": {
"user": "4f3c0d2d7edae1e935010000",
"time": ISODate("2012-02-15T20: 47: 30.175Z"),
"message": "message1"
},
"1": {
"user": "4f3bb96d7edae1850b000000",
"time": ISODate("2012-02-15T20: 47: 37.79Z"),
"message": "message2"
},
"2": {
"user": "4f3c0d2d7edae1e935010000",
"time": ISODate("2012-02-15T20: 47: 43.295Z"),
"message": "message3"
}
}
Your problem is you're trying to sort sub-documents, whereas the .sort() function is for sorting documents.
So, each object in the collection is a document--in this case, each chat is a document. You can sort your chats with .sort(), but you have subdocuments called messages within your documents (sub-documents), and Mongo doesn't sort those for you.
See some answers to a similar question here:
Sort Sub Documents in MongoDB
Related
To search an object in JSON from MYSQL using Laravel you do this:
$parent = DB::table($this->table)
->where("right_children->parent", $firstParent)
->get();
The problem now is how I can go about searching objects in arrays
[
{ "parent": "fish",
"children": { "right": "nunu"}
},
{ "parent": "cat",
"children": {"right": "nonha"}
}
]
IF I want to find the name "nunu" how to I search it using laravel?
I tried this:
DB::table($this->table)
->whereJsonContains("right_children->children->right", $firstParent)
->get();
but no result, I know I am getting it wrong
I am trying to retrieve all records and display them in a JSON file.
My current function retrieves all Events that belong to a specific user.
/**
* create json files from doctrine/mongo
* #Route("/createjson", name="createjson")
*/
public function createJson()
{
// check user authentication
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$dm = $this->get('doctrine_mongodb')->getManager();
$repository = $dm->getRepository('AppBundle:Event');
$events = $repository->findBy(array('user' => $this->getUser()));
$serializer = SerializerBuilder::create()->build();
$result = $serializer->deserialize($events, 'AppBundle\Document\Event', 'json');
var_dump($result);
exit;
}
This is not working because some of the elements passed into the serializer are of an array format. Error I am getting.
Warning: json_decode() expects parameter 1 to be string, array given
500 Internal Server Error - ContextErrorException
However if I use the inbuilt Symfony Serializer it works fine:
$serializer = $this->container->get('serializer');
$reports = $serializer->serialize($events, 'json');
return new Response($reports);
However the JSON to be produced will be different to my Document/Entity hence why I want/need to use the JMSSerializerBundle.
For example, a record looks like this:
[{
"id": "572041b3288b560e5e00451c",
"name": "Test",
"date": "2016-04-27T05:25:00+1000",
"enddate": "2016-04-30T11:55:00+1000",
"location": {
"name": "Sydney, NSW"
},
"key": {
"id": "1g43g34g34g23f32g32G32gGSDF"
},
"user": {
"id": "57203174288b560e5e0044da"
}, ...
}]
But I only want to display (output) to JSON
[{
"id": "572041b3288b560e5e00451c",
"name": "Test",
"date": "2016-04-27T05:25:00+1000",
"location": "Sydney, NSW"
}]
How would I go about doing this? There is not much documentation on JMSSerializerBundle online.
Edit: I should mention that the database collection I am querying has a relation to the User collection which is managed by FOSUserBundle. I'm not sure if this has any relation to my problem however
You should look at the documentation of the bundle, may be you will find more information
http://jmsyst.com/bundles/JMSSerializerBundle
http://jmsyst.com/libs/serializer/master/usage
You seems to use the wrong function of the serializer. In your case, you seems to need to get a json from your user object, so you need to use
$serializer = SerializerBuilder::create()->build();
$result = $serializer->serialize($events, 'AppBundle\Document\Event', 'json');
serialize($object):string : get a string from an object
deserialize($string):object : get an object from a representation of an object (json, xml...).
I'm using Lumen to set up a microservice for polling a database frequently and distribute the dataset through a wamp router to multiple clients. The database query is stored in a stored procedure, that's why i do the following in my controller:
$result = DB::select($query);
return $result;
The return gives the following dataset:
[
{
"0": "012345",
"1": "Moby Dick",
"2": "Herman Melville",
"3": "Hardcover",
"isbn": "012345",
"title": "Moby Dick",
"author": "Herman Melville",
"type": "Hardcover"
},
{
"0": "123456",
"1": "Laravel: Code Bright",
"2": "Dayle Rees",
"3": "Ebook",
"isbn": "123456",
"title": "Laravel: Code Bright",
"author": "Dayle Rees",
"type": "Ebook"
},
{
"0": "234567",
"1": "Easy Laravel 5",
"2": "W.J. Gilmore",
"3": "Ebook",
"isbn": "234567",
"title": "Easy Laravel 5",
"author": "W.J. Gilmore",
"type": "Ebook"
}
]
I want to remove the numeric key-value pairs prepending the associative key-value pairs. How can i do that?
Thanks in advance!
Edit: things I tried:
$result = DB::select($query)->get(); // Gives: Call to a member function get() on array. For obvious reasons
A dirty hack like Matei stated: Looping through the array and removing the KVP where the key is numeric. Which works, but I think the Laravel/Lumen framework offers cleaner solutions, which I am not able to find.
In config/database.php you can change 'fetch' => PDO::FETCH_CLASS, to 'fetch' => PDO::FETCH_ASSOC,
or
You can use array_reduce and array_filter like:
$result = json_decode(DB::select($query), true);
$result = array_reduce($result, function ($result, $item) {
$result[] = array_filter($result, function ($key) {
return !is_numeric($key);
}, ARRAY_FILTER_USE_KEY)
return $result;
}, array());
return json_encode($result);
NOTE: If the DB stmt is returning an array, rather than a json encoded string, then you must remove the json_decode and json_encode functions calls.
I am trying to create a "nested" array within an object that I am returning from a database.
I can have more than one footnote per "thing".
This is what I am currently getting back:
JSON
{
"data": [{
"id": "123",
"type": "foo",
"color": "bar",
"footnote_id": "1",
"footnote_text": " Footnote one"
}]
}
Here is the result I'm trying to generate:
JSON
{
"data": [{
"id": "123",
"type": "foo",
"color": "bar",
"footnotes": [{
"footnote_id": "1",
"footnote_text": " Footnote one"
},
{
"footnote_id": "2",
"footnote_text": "Footnote two"
}]
}]
}
I have a footnotes table that has all kinds of footnotes (footnote_id and such).
I have a type table that has all kinds of things in it (type_id and such).
I also have a type_footnotes table that only has two columns: type_id and footnote_id
I'm not sure how to create the footnotes property of the response object - then display the results within that array.
Thank you for your time!
EDIT
Here is the query - I thought I had posted this as well. My apologies.
PHP
public function get_thing($type_id) {
$this->db->select('type.type_id, type.type, type.type_color');
$this->db->join('footnotes', 'footnotes.footnote_id, footnotes.footnote_text');
$this->db->join('type_footnotes, type_footnotes.type_id = type.type_id');
$query = $this->db->get_where('type', array('type.type_id' => $type_id), 1);
if ($query->num_rows() > 0) {
return $query->result();
}
}
Remove the limit, and post here what do you get as result :
$query = $this->db->get_where('type', array('type.type_id' => $type_id));
I have the following json data:
{
"data": [
{
"name": "The Frugalicious Chef",
"category": "Chef",
"id": "186397894735983",
"created_time": "2011-03-07T16:10:35+0000"
},
{
"name": "Siuslaw Broadband",
"category": "Telecommunication",
"id": "190373850988171",
"created_time": "2011-03-06T20:21:42+0000"
},
{
"name": "Paul",
"category": "Movie",
"id": "129989595478",
"created_time": "2011-03-04T19:55:18+0000"
},
{
"name": "Mark Zuckerberg",
"category": "Public figure",
"id": "68310606562",
"created_time": "2011-02-16T09:50:35+0000"
},
The idea here is that I want to take this data and use parts of it. I want to create a list of the "category's" that are in the data. The problem is that there is and will be multiple items with the same category. So my list will have duplicates that I do not want. The following is how I am getting the data and converting it for use:
$jsonurl = "https://xxxxxxxxxx.com/".$fd_ID. "/info?access_token=".$session['access_token'];
$likesjson = file_get_contents($jsonurl,0,null,null);
$likesArray=json_decode($likesjson);
I then use a foreach to access the data.
foreach($friendLikesArray->data as $l)
{
etc......
}
So I guess muy question is I want to take the $likesArray and pull out all the unique Data->Category->names. Also will want to do sorting, and other things but I will get to that when the time comes.
Thanks for the help in advance.
Neil
The data structure you would want to use is a set, that only allows unique entries.
A simple implementation using PHP arrays is to use the keys.
e.g.
$categories = array();
foreach($friendLikesArray->data as $l)
{
$categories[$l->category] = true;
}
$categories = array_keys($categories);
This way if the category has already been added, then you are not adding anything new to the array.
If the keys are not important to you then you can use the line:
$categories[$l->category] = $l->category
But this means your array won't have 0,1,2...n for keys.