Database Reference
In-Depth Information
// search by user returns users in the network that aren't already being followed
public static function searchByUsername($username, $currentusername)
{
// wild card search on $username - which is just a string passed
// in from the request, e.g. the letter 'a'
$username=$username.'.*';
$queryString = "MATCH (n:User), (user { username:{c}}) " .
"WHERE (n.username =~ {u} AND n <> user) AND (NOT (user)-[:FOLLOWS]->(n)) ".
" RETURN n";
$query = new Everyman\Neo4j\Cypher\Query(Neo4Client::client(), $queryString, array(
'u' => $username,
'c' => $currentusername));
$result = $query->getResultSet();
return self::returnAsUsers($result);
}
The searchByUsername in {PROJECTROOT}/app/public/js/graphstory.js uses an AJAX request and formats the
response in render SearchByUsername . If the list contains users, it will be displayed in the center of the page under
the search form, as shown in Figure 8-9 . Otherwise, the response will display “No Users Found.”
Once the search returns results, the next action would be to click on the “Add as Friend” link, which will call the
addfriend method in graphstory.js . This will perform an AJAX request to the follow route, which will then call
the follow method in the User class. The follow method in User , shown in Listing 8-28, will create the relationship
between the two users by first finding each entity via the MATCH clause and then use the CreateUnique clause to create
the directed FOLLOWS relationship. Once the operation is completed, the next part of the query then runs a MATCH on
the users being followed to return the full list of followers ordered by the username.
Listing 8-28. The follow Route and follow Service Method
// takes current user session and will follow :username, e.g. one way follow
$app->get('/follow/:username', function ($username) use ($app) {
$following = User::follow($_SESSION['username'], $username);
echo '{"following": ' . json_encode($following) . '}';
});
// the follow method in the User class
public static function follow($username, $userTofollow)
{
$queryString = " MATCH (user1:User {username:{cu}} ), ".
" (user2:User {username:{u}} ) " .
" CREATE UNIQUE user1-[:FOLLOWS]->user2 " .
" WITH user1" .
" MATCH (user1)-[f:FOLLOWS]->(users)" .
" RETURN users " .
" ORDER BY users.username";
$query = new Everyman\Neo4j\Cypher\Query(Neo4Client::client(), $queryString, array(
'cu' => $username,
'u' => $userTofollow
));
$result = $query->getResultSet();
return self::returnAsUsers($result);
}
Search WWH ::




Custom Search