how to build a comment tree using neo4j and PHP - php

I want to build a comment tree using neo4j and PHP for example:
A
|_B
...|_C
...|_D
I am using https://github.com/jadell/neo4jphp component. It doesnt heve this method. Anyway I dont know how to get all tree using Cypher.

You just add comments onto other comments, you can execute this cypher with neo4jphp or neoclient.
MATCH (c:Comment {id:{comment_id}})
MATCH (a:Author {id:{author_id}})
CREATE (n:Comment {id:{new_comment_id}, text:{new_comment_text})
CREATE (a)-[:WROTE]->(n)

Related

Add Edges collection to a graph

Using Arangodb 3.2, have a set of collections (arangoimp + CSV):
user (documents)
profile (documents)
user_profile (edges)
I'd like create a graph from listed above. Was unable to find in the documentation about composing graph from already existent collections of vertices and edges, or didn't get how to.
In [1] there is an example how to add relation (e.g. create edges collection, linking vertices), but what if I already have one?
It would be nice to understand how to compose a graph from existent collections via (AND/OR):
PHP (triagens/arangodb)
HTTP API
Bash
Links:
https://docs.arangodb.com/3.2/Manual/Graphs/GeneralGraphs/
Have you tried to create the graph via the web interface (https://docs.arangodb.com/devel/Manual/Administration/WebInterface/Graphs.html)?
If you want to create the graph just once this is an easy solution.
Finally I found a PHP solution myself:
$edgeDefinition = new \triagens\ArangoDb\EdgeDefinition(
'user_profile',
'user',
'profile'
);
$graphName = 'testGraph';
$graph = new \triagens\ArangoDb\Graph($graphName);
$graph->addEdgeDefinition($edgeDefinition);
$graphHandler = new \triagens\ArangoDb\GraphHandler($connection);
if (!$graphHandler->getGraph($graphName)) {
$graphHandler->createGraph($graph);
}
I'd propose update official docs (see [1]) with more explicit explanation of graph_module._relation parameters.
It's a pity, but there is no ArangoDb HTTP API solution yet.

FOSElasticaBundle: Is it possible to change "query_builder_method" in controller?

According to FOSElasticaBundle documentation it is possible to configure application to use custom query builder method like this:
user:
persistence:
elastica_to_model_transformer:
query_builder_method: createSearchQueryBuilder
But is it possible to choose QB method live, e.g. in controller action?
I'd like to be able to control what's being fetched from DB while transforming Elastica results to Doctrine entities. E.g. sometimes I'll want to do eager fetch on some relations, but can't do that by default.
Since FOSElasticaBundle documentation is not very precise, I went through its code and found it impossible to control what query builder is used on controller level.
It is possible to change whole elastica_to_model_transformer to a custom service, but still it's statically defined in configuration. Maybe with some dirty solution it would be possible going this way, but I don't think it's worth it.
I decided to just not using this feature of FOSElasticaBundle. The main problem I had was that when you use fos_elastica.index instead of fos_elastica.finder or elastica repository (in order to get plain not transformed results Elastica\Resultset), there's no findPaginated method with returns Pagerfanta paginator object, which is very helpful in my case.
Fortunately although it's not mentioned in documentation it's possible to create the Pagerfanta this way too, but a little bit more manually.
Here's a code snippet:
//generate ElaticaQuery somehow.
$browseQuery = $browseData->getBrowseQuery();
$search = $this->container->get('fos_elastica.index.indexName.typName');
//create pagerfanta's adapter manually
$adapter = new \Pagerfanta\Adapter\ElasticaAdapterElasticaAdapter($search, $browseQuery);
// now you can create the paginator too.
$pager = new Pagerfanta($adapter);
//do some paging work on it...
$pager->setMaxPerPage($browseData->getPerPage());
try {
$pager->setCurrentPage($browseData->getPage());
} catch(OutOfRangeCurrentPageException $e) {
$pager->setCurrentPage(1);
}
//and get current page results.
/** #var Result[] $elasticaResults */
$elasticaResults = $pager->getCurrentPageResults();
// we have to grab ids manyally, but it's done the same way inside FOSElasticaBundle with previous approach
$ids = array();
foreach($elasticaResults as $elasticaResult) {
$ids[] = $elasticaResult->getId();
}
//use regular Doctrine's repository to fetch Entities any way you want.
$entities = $this->getDoctrine()->getRepository(MyEntity::class)->findByIdentifiers($ids);
This actually has a few advantages. In general it gives you back control over your data and doesn't tie ElasticSearch with Doctrine. Therefore you can resign on fetching data from Doctrine if you have all needed data in ElasticSearch (if they are read only data of course). This lets you optimize your application performance but reducing amount of SQL queries.
The code above may be wrapped with some kind of service in order to prevent making mess in controllers.

Use nested transformers in Laravel 5

In my Laravel 5 app, I'm implementing Transformers and Fractal.
I've got in my example two different models: User and UserLogin. Every User can have multiple UserLogins (I've already added a one-to-many relationship between them). Now I want to "clean" my response, which returns an User with his UserLogins. So I've created two transformers, and I thought that I should call a transformer inside the other one inside his return, like here:
"UserLogins"=> Fractal::collection($user->userLogins, new UserLoginTransformer).......
Unfortunately it doesn't work and the error is that it doesn't find fractal library (which is correctly imported).
What could be the problem?
Finally found the solution.
Fractal class does not exists, I cannot make simplier than that.
And you weren't correctly using the library.
So, the solution :
use \League\Fractal\Manager;
use \League\Fractal\Resource\Collection as FractalCollection;
$fractal = new Manager();
$resource = new FractalCollection($user->userLogins, new UserLoginTransformer);
return $fractal->createData($resource)->toArray();

How to auto-instantiate a library in CodeIgniter

I'm using a really basic library in Codeigniter. In order to use it I need to pass in a number of config parameters using a config function. My library currently requires me to instantiate it before I can call the config, i.e., I have to use it as below:
$this->load->library('Library');
$instance = new Library();
$instance->config($configparams);
I'd like to use it like standard CodeIgniter libraries:
$this->load->library('Library');
$this->library->config($configparams);
What do I need to add to the library in order to have it auto-instantiate? The code is as below:
class Library {
function config($configparams){
...
}
}
This is working now. I swear it wasn't working before I posted on SO! Thanks for posts.
Once you load a class
$this->load->library('someclass');
Then when use it, need to use lower case, like this:
$this->someclass->some_function();
Object instances will always be lower case
According to the docs, you should just call it. So:
$this->load->library('Library');
$this->library->config($configparams);
But why not just pass $configparams to the constructor:
$this->load->library('Library', $configparams);
Check out the guide for CodeIgniter -- it's a great resource to learn more about the framework. IMHO, there aren't any good books available on the current version; this is it.
You basically call it like anything else.
$this->load->library('Name of Library')
Read more here: http://www.google.com/url?sa=t&source=web&cd=2&ved=0CCIQFjAB&url=http%3A%2F%2Fcodeigniter.com%2Fuser_guide%2Fgeneral%2Fcreating_libraries.html&ei=tLFUTbz3HI3SsAOYgP2aBg&usg=AFQjCNFo751PYFp5SbqzuZMxGhXwMI8SJA

GWT RequestBuilder - Changin URLs

I'm using GWT to dynamically load html snippets from php script. I define the snippet i want the php script to return in the url (test.php?snippet=1). Now in GWT i have a function "getSnippet(int snippet id)" that uses a RequestBuilder to retrieve the snippet. It works perfectly fine, but it bothers me that i have to create a new RequestBuilder everytime getSnippet gets called. I'd rather have one ReqestBuilder and just change the url when getSnippet is called...
Is there a way to do this ?
Thank you !
In looking at the source code, I can't see a good reason why they are doing this. I would like to think that the GWT developers decided to leave out the setUrl method for a reason and included it in the constructor instead.
If you really want to do it, one way around this would be to extend the class and add a setUrl(String url) method. Modify all your current uses of RequestBuilder to use your newly extended class and see if anything breaks.

Categories