简体   繁体   English

使用原始Cypher在Symfony中查询Neo4j

[英]Using raw Cypher to query Neo4j in Symfony

I am trying to go throught this tutorial: http://www.sitepoint.com/adding-social-network-features-php-app-neo4j/ But using the Symfony Framework instead of Silex. 我正在尝试阅读本教程: http : //www.sitepoint.com/adding-social-network-features-php-app-neo4j/但要使用Symfony框架而不是Silex。

I have been able to set up Neo4j to run with Symfony and am able to right user data to the graph. 我已经能够将Neo4j设置为与Symfony一起运行,并且能够将用户数据修改为图表。 Now I would like to display all user email addresses in a list. 现在,我想在列表中显示所有用户的电子邮件地址。 I have taken this script: 我采用了以下脚本:

 public function home(Application $application, Request $request)
    {
        $neo = $application['neo'];
        $q = 'MATCH (user:User) RETURN user';
        $result = $neo->sendCypherQuery($q)->getResult();

        $users = $result->get('user');

        return $application['twig']->render('index.html.twig', array(
            'users' => $users
        ));
    }

And adapted it to read: 并将其修改为:

   public function showUsersAction()
    {
        $em = $this->container->get('neo4j.manager');
        $query = 'MATCH (n:`User`) RETURN n';
        $users = $em->cypherQuery($query);

        //print_r($users);
        return $this->render('UserBundle:Account:showUsers.html.twig', array('users' =>$users));
    }

And The twig looks as follows: 树枝看起来如下:

{% extends '::base.html.twig' %}
{% block content %}
    <h1>get all users:</h1>
    <ul>
    {% for user in users %}
        <li>{{ user.property('email') }}</li>
    {% endfor %}
    </ul>
{% endblock %}

But something in the twig is wrong, im getting the error: 但是树枝中的某些东西是错误的,我得到了错误:

Method "property" for object "Everyman\Neo4j\Query\Row" does not exist in UserBundle:Account:showUsers.html.twig at line 6 

The problem was found in the syntax of the twig file. 在树枝文件的语法中发现了问题。 After consulting this page: https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries it became clear, that I had to include user['n'] in my twig template. 在查阅此页面后: https : //github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries变得很清楚,我必须在user['n']树枝模板中包含user['n'] The twig template now looks as such: 现在,树枝模板如下所示:

{% extends '::base.html.twig' %}
{% block content %}
    <h1>get all users:</h1>
    <ul>
    {% for user in users %}
        <li>{{ user['n'].getProperty('email') }}</li>
    {% endfor %}
    </ul>
{% endblock %}

I'm the author of the article you mentioned. 我是您提到的文章的作者。 The thing is that you use a different neo4j library than the one used in the article, hence neoclient , so the methods used in the article are different than the methods provided with neo4jphp. 事实是,您使用的文章与文章中使用的Neo4j库不同,因此使用的是neoclient ,因此本文中使用的方法与neo4jphp所提供的方法不同。

As NeoClient uses heavily the Symfony components, integrating it in Symfony is really easy, you just need to override the DI. 由于NeoClient大量使用Symfony组件,因此将其集成到Symfony中非常容易,您只需要覆盖DI。 Example here : https://github.com/graphaware/GithubNeo4j/tree/master/src/GraphAware/Neo4jBundle 这里的例子: https : //github.com/graphaware/GithubNeo4j/tree/master/src/GraphAware/Neo4jBundle

You'll then be able to use the methods illustrated in the 3 articles I wrote on Sitepoint. 然后,您将能够使用我在Sitepoint上撰写的3篇文章中说明的方法。

So your problem with the twig template is that he doesn't find the getProperty method of the node object class, which is normal as neo4jphp returns Row object classes. 因此,使用树枝模板的问题在于,他没有找到节点对象类的getProperty方法,这很正常,因为neo4jphp返回Row对象类。

If you switch back to neoclient, as in the article, in the Twig template you can just write : 如果您切换回neoclient(如本文所述),则可以在Twig模板中编写:

{% for user in users %}
   <li>{{ user.getProperty('email') }}</li>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM