So I have a gremlin query like this:
g.V().hasLabel('Person').valueMap(true, 'name')
Now this creates a valuemap with the fields/columns 'Label', 'ID' and 'name', but how do I exclude 'ID' from this?
I only want 'Label' and 'name' to be included as the results.
Any help is very much appreciated, thank you! :)
You can use WithOptions to control this.
Here is an example using the air-routes data set
gremlin> g.V('3').valueMap('city').with(WithOptions.tokens,WithOptions.ids)
==>[id:3,city:[Austin]]
gremlin> g.V('3').valueMap('city').with(WithOptions.tokens,WithOptions.labels)
==>[label:airport,city:[Austin]]
The other answer using WithOptions probably works, but I got an error message when I tried it (I'm sending gremlin queries as a string using an API, so it might be an error on the server-side of the API).
However, I figured out another way to do this.
With labels:
g.V().hasLabel('Person').project('label', 'name').by(label).by(values('name').fold())
With IDs:
g.V().hasLabel('Person').project('id', 'name').by(id).by(values('name').fold())
Related
I'm working on reactJs with laravel CHAT. I want to display juste the last message from the sender. But I usually get all his messages. I tried lot of attempts But I didn't get the solution yet. this is my last attempt:
$types= chat::select('id','senderId')->where('userId', $id)->get();
foreach ($types as $chats) {
$chats->chat = chat::select('id','unseenMsgs', 'senderId')->where('senderId', $chats->senderId)
->get();
foreach ($chats->chat as $child){
$child->lastMessage = chat::select('userId','message', 'time', 'senderId')
->where('id', $child->id)
->orderBy('id', 'asc')->skip(0)->take(1)
->get();
}
}
return ['chatsContacts' => $types];
The userId is the receiver one and senderId is the one who send the message. At first query I tried to get all the messages that are sent to the reciever. The question is how to ignore the duplication and get juste the last message from the sender one. and Thanks in advance for your help
Ps: I'm using An MVP and the reponse should be like this:
this picture is for the response in the MVP
That's why in my server side I should create an api which return exactly this response
It is hard to recreate the issue with the amount of information provided.
From what I understand about the problem description, following seems like a possible solution:
$latestChats = chat::select('id', 'userId', 'message', 'time', 'senderId')
->where('userId', $id)
->orderBy('id', 'desc')
->get()
->unique('senderId');
return ['chatsContacts' => $latestChats];
This should give you a collection containing the last messages sent by every senderId to the specified userId.
If this does not resolve your issue please provide further information.
What does the table look like?
What is the exact format you're looking for in chatContacts ?
[edit] solved, see: https://stackoverflow.com/a/32638610/221650
Some associated data is not coming through, although it's in the contain clause.
I can get $project->Participants if I set a belongsToMany relationship in the Projects table, relating to Participants through ProjectParticipants, but that way I still can't reach other tables associated with ProjectParticipants even with $project->participants->_joinData->other_related_table
How would I do to get ProjectParticipants and Participants with the same query?
Code:
// Database: Projects <--1:N-- ProjectParticipants --M:1--> Participants
// ProjectController:
$project = $this->Projects->get($id, ['contain'=>[
'ProjectParticipants.Participants']);
// ProjectsTable:
$this->hasMany('ProjectParticipants', [
'foreignKey' => 'project_id']);
// ProjectParticipantsTable:
$this->belongsTo('Participants', [
'foreignKey' => 'participant_id']);
It's a very complicated many to many relationship.
//projectTable
$this->belongsToMany('Participants');
//praticipantsTable
$this->belongsToMany('Projects');
It's well explained in the Bookmark Tutorial.
Sorry, it's working fine, I hadn't noticed there was a custom Entity\Project::_getParticipants() that was returning a Collection.
A simple debug on that object showed everything alright, but then when running it through a foreach, the associations disappeared.
I've read that, to be able to rank search results you may query MySQL like this:
SELECT * ,
MATCH (title, body) AGAINST ('$search') AS rating
FROM posts
WHERE MATCH (title, body) AGAINST ('$search')
ORDER BY rating DESC
Is there a way to do this in CakePHP 2.X?
Also, I need to do this while paginating at the same time. So I think I would need to write condition for the paginator, not a direct 'query'.
Thanks for your help!
Use like this it will prevent mysql injection too
array("MATCH(User.current_position) AGAINST(? IN BOOLEAN MODE)" => $srch_arr['text'])
Ok, it took me some time... Since, the key issue was to get a rating on the resulting matches, the complicated part in this query was the specific field:
MATCH (title, body) AGAINST ('$search') AS rating
I figured that I should just write that field in the "field" option, in the pagination array.
The resulting code was the following:
$this->paginate = array(
'limit' => 15,
'fields' => array('*', "MATCH (data) AGAINST ('$q') AS rating"),
'conditions' => "MATCH(SearchIndex.data) AGAINST('$q' IN BOOLEAN MODE)",
'order' => array(
'rating' => 'desc',
),
);
$paginatedResults = $this->paginate('SearchIndex');
And that worked seamlessly!
I think this is the best way to achieve real search results using Cake. Unless someone has a better alternative :)
Searching phrases in between double quotes will give you the results you should expect!
I have used the above database call by Thomas (thank you) and it does work seamlessly.
However the code:
'conditions' => "MATCH(SearchIndex.data) AGAINST('$q' IN BOOLEAN MODE)",
removes the Data Abstraction Layer and opens up your site to SQL injection.
It's probably not quite as good (haven't fully tested it) but try:
'SearchIndex.data LIKE'=>'%'.$search.'%'
I hope this is helpful in someway.
So I finally got "ACL" to work, and now im trying to get the "alias" value from the "AROS" table.
I want to make a simple message like logged in as .... (admin, moderator or user).
After some tinkering i got the "alias" value from the code below but I can't shed the feeling ther's an easier way to do this. Any help or advice will be greatly appreciated.
Currently im using:
$logindata = $this->Acl->Aro->findByForeignKey($user['User']['id']);
$parent_id = $logindata['Aro']['parent_id'];
$rankdata = $this->Acl->Aro->find('first', array('recursive' => -1, 'conditions' => array('Aro.id' => $parent_id)));
$rank = $rankdata['Aro']['alias'];
There are two option for you which is simpler than yours..i tried that in my project.
1.Save your aros with the same alias as its parent.And then you can use it simply with one line code.
2.Or you can save its parent alias into users table and then you get this all again almost every part of your site with $auth .
In CakePHP, I have two tables, Countries & Networks. They have a HABTM relationship and are joined by countries_networks.
I'm trying to get all countries from the countries table where the 'name' field in Networks = 'o2'
I've realised I can't do this using a basic find(), so I've been experimenting with the containable behaviour. I have managed to restrict the returned data, but it looks as though 'containable' doesn't exactly work as I want. Heres my code:
$countries = $this->Country->find('all', array('contain' => array(
'Network' => array(
'conditions' => array('Network.name =' => "o2"),
)
)));
This query however returns ALL countries, and the Network.name if its 'o2'. What I really need to do is return ONLY the countries that have a Network.name of 'o2', and no others.
Can anyone help? Thanks.
"=' =>"
What is it? there is no needed to use "=" symbol after "Network.name"
Your query returns you exactly what your ask.
Try to select from Network.
$countries = $this->Country->Network->find('first', array(
'conditions' => array('Network.name' => "o2"),
'contain' => array('Country')
));
$countries = $countries['Country'];
You should be able to do something like this:
$this->Country->hasAndBelongsToMany['Network']['conditions'] = array('Network.name'=>'o2');
$myCountries = $this->Country->find('all');
I haven't tested this, but it should get you pretty close to where you need to be.
Also, bear in mind that changing the hasAndBelongsToMany array like this will affect all subsequent queries against that model during the current page load (but it will be reset the next time the model is instantiated).
I'm on my way out the door, so sorry for the brief explanation.
I think what you want is a HABTM relationship, but to be able to filter based on the associated model's data. To do this, check out the "Containable" behavior in Cake's manual. Pretty sure that's what you're after.