Search code examples
symfonyneo4jtwigcypherneoxygen

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.

I have been able to set up Neo4j to run with Symfony and am able to right user data to the graph. 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 

Solution

  • 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. 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 %}