Accuracy of results for spring.io guide accessing-data-neo4j? - spring-boot

I ran a spring.io guide for neo4j, and it did not return expected results.
For a sanity check, I was not sure where else to go for this. Running the spring guide with this documentation does not return results as expected. The docs state results will be similar to:
Before linking up with Neo4j...
Greg's teammates => []
Roy's teammates => []
Craig's teammates => []
Lookup each person by name...
Greg's teammates => [Roy, Craig]
Roy's teammates => [Greg, Craig]
Craig's teammates => [Roy, Greg]
However, in two instances, each on a different system, I got back the following:
Before linking up with Neo4j...
Greg's teammates => []
Roy's teammates => []
Craig's teammates => []
Lookup each person by name...
Greg's teammates => [Roy, Craig]
Roy's teammates => [Craig]
Craig's teammates => []
Yes, they are similar but not the same. Can someone confirm their own results for the same guide?

I checked the guide and it seems that the expected output is wrong.
My assumption is that it is a left-over of the old Spring Data Neo4j / Neo4j-OGM behaviour when there was a bi-directional relationship definition (also judging from the comment above the relationship in the Person class).
For completeness I add the relationship creation code here:
greg = personRepository.findByName(greg.getName());
greg.worksWith(roy);
greg.worksWith(craig);
personRepository.save(greg);
roy = personRepository.findByName(roy.getName());
roy.worksWith(craig);
// We already know that roy works with greg
personRepository.save(roy);
// We already know craig works with roy and greg
Also looking at the comments there, I would definitely say that the assumptions are just plain wrong.
The worksWith function only adds the relationship on the calling object and not the opposite.
So either the guide has to change the behaviour of the worksWith function or the expected output and the comments needs to get changed.
tl;dr;
Your output is correct for me compared with the application's code.

Related

How do I log properly a Laravel Job?

Reading the official documentation I understand that it's necessary to use Illuminate\Support\Facades\Log, but the configuration in logging.php is a bit confusing to me. I basically don't understand how the channel drivers work, it seems to me a bit overcomplicated.
Logging commands, however, is pretty easy in my opinion: you just add ->appendOutputTo('command.log') in the schedule method and the job is pretty much done. Is there a similar method to log jobs? Or is the way in the documentation the only one? If so, could anyone be kind enough to simplify it to me?
It is simple to use the Log facade,
Go to loggin.php and add a new channel, here is a signle log file example (without rotation)
'job' => [
'driver' => 'single',
'path' => storage_path('logs/job.log'),
'level' => 'info',
],
Then use it anywhere with
Log::channel('job')->info($content);
//or
Log::channel('job')->error($content);

Laravels syncWithoutDetaching and additional data

I have Googled my fingers sore, and I can't see anyone discussing this, but I have a suspicion that Laravels syncWithoutDetaching() method doesn't take any parameters for extra data like save(), sync() and attach() does?
Does anybody know this? In the API documentation the method has the following parameters:
array syncWithoutDetaching(Collection|Model|array $ids)
I have trouble adding existing data to a relationship between a Guest and an Event. I need to add status for the guests and what event they are attending, maybe attending or declined.
sync() and syncWithoutDetaching() both don't have a parameter for additional values, you have to pass the additional values as an array with the ids.
According to the docs:
You may also pass additional intermediate table values with the IDs:
$user->roles()->sync([
1 => ['expires' => true],
2,
3
]);
If you look here you can see that syncWithoutDetaching() just calls sync() but passes false as the second argument.
In your case it would be something like this:
$event->guests()->syncWithoutDetaching([
1 => ['attending' => true],
2 => ['attending' => false]
])
I think #remul answer is the best, but it requires additions for people like me who get to this page.
syncWithoutDetaching() - is just an abbreviation for sync() - here. This corresponds to sync($data, false)
The documentation talks about another great method:
If you would like to insert the same intermediate table values with
each of the synced model IDs, you may use the syncWithPivotValues
method
But the documentation does not say that the method accepts the third argument, which just corresponds to the logic of syncWithoutDetaching().
Look here.
If you pass false, the IDs not passed will not be detaching.
I think this is what the question was about.

Is there a concept of a middleware in CodeIgniter?

I want to do some pre and post processing of requests like handling authentication, loading contextual data, performance timings and things like that. Coming from Django there's a concept of MIDDLEWARE_CLASSES that lets me handle the request in various stages: https://docs.djangoproject.com/en/dev/topics/http/middleware/
Currently it seems like each Controller has to do the same setup and load, in the constructor which isn't ideal because if the constructor fails, the class doesn't get initialized which has subtle but important consequences. I want to move this global handling to a global place.
Any suggestions?
There is not.
This might be helpful.
Codeigniter forum
You need to use hooks for this, Edit your application/config/hooks.php
$hook['post_controller_constructor'][] = array(
'class' => 'Autologin',
'function' => 'cookie_check',
'filename' => 'autologin.php',
'filepath' => 'hooks'
);
I recently stumbled upon this question so even tho it has been 6 years since this question was asked, I am answering in case it helps anybody else like me.
I have found CodeIgniters has "filters" which can be used for that same purpose:
http://codeigniter.com/user_guide/incoming/filters.html?highlight=filters
Actually there is not middleware routing structure in any codeigniter framework until now. But some guys has written a bunch of code to implement that.
I have found it usable for some purposes

Querying embedded documents on a document with MongoMapper

What is a good pattern for querying embedded documents on a document? For instance, my User document has an embedded Alerts document. If I want to see if a given User has an alert with name I can do it two ways as far as I can tell -- in memory a la
alert = current_user.alerts.select{|a| a.name == params[:name]}.first
or via the actual document interface a la (note that I'm not 100% sure this is semantically valid but you get the point):
User.where('alerts.name' => params[:name], :id => current_user.id).first
There MUST be a better way, something like
current_user.alerts.where(:name => params[:name])
perhaps? Or maybe I'm just not thinking about the problem right?
Nope. And I think this is the motivation:
In MongoMapper, queries on the database always return a root object. Allowing queries to return an embedded doc without its parent would be a break with that and make a lot of things more complicated (what if I call .parent inside that embedded doc?) so MongoMappers errs on the side of simplicity and doesn't pretend that things are something they aren't. Embedded docs are stored in an array inside the root doc in MongoDB, so MongoMapper gives you an array in Ruby.
So your two ways of doing it are the intended ways of doing it.
If you need some syntactic suger, it shouldn't be too hard to code up. You could extend Array or you could code a plugin to expand upon MongoMapper's proxy for embedded docs.
I think Mongoid supports this, see "Finding" in the manual for embedded docs.
You can do either:
User.where('alerts.name' => params[:name], :id => current_user.id).fields(:alerts).first.alerts.select{|u| u.name == params[:name]}
or
User.where('alerts.name' => params[:name], :id => current_user.id).fields(:alerts).alerts.select{|u| u.name == params[:name]}.first

Datamapper (ruby) - (Using the blog example) Getting the posts that don't have comments

First SO question after using this place for reference on a lot of other things.. I'm nervous.
DataMapper.. Using Blog model for example (posts, comments etc - http://datamapper.org/docs/associations.html) I'm trying to workout how to get the blog posts that don't have any comments..
So something like Post.all(:comments => { :comment => nil })
Lot having much luck :(
I read on here that for complex reporting queries it's best to drop down to SQL, but surely there's a way of doing this isn't there SO?
Thanks in advance.
If you don't have a counter cache in your Post model (called eg "comments_count"), this is going to be slow. But here it is anyway:
Post.find(:all, :include => :comments).select { |post| post.comments.empty? }
I would suggest going with the counter cache solution (but that involves some schema refactoring and updating the counters during migration one time):
Post.all(:conditions => { :comments_count => 0 })
The latter being much more efficient, especially in Rails 3 (cause of lazy querying).

Resources