Database Reference
In-Depth Information
Connecting Users
A common feature in social media applications is to allow users to connect to each other through an explicit
relationship. In the sample application, we use the directed relationship type called
FOLLOWS
. By going to the “Friends”
page within the social graph section, you can see the list of the users the current user is following, search for new
friends to follow, add them and remove friends the current user is following. The user management section of the
App
class contains each of the routes to control the flow for these features, specifically the routes that cover
friends
,
search_by_user_name
,
follow
and
unfollow
.
To display the list of the users the current user is following, the /
friends
route, showing in Listing 8-26, calls the
following
method in
User
class. The
following
method in
User
, also shown in Listing 8-25, creates a list of users by
matching the current user's
username
with directed relationship
FOLLOWS
on the variable
user
.
Listing 8-25.
The
/friends
Route and the
following
Method
// friends route that shows connected users via FOLLOW relationship
$app->get('/friends', $isLoggedIn, function() use ($app){
$user = User::getByUsername($_SESSION['username']);
$following = User::following($_SESSION['username']);
$app->view()->setData(array('user' => $user,
'following' => $following,
'title'=>'User Settings'));
$app->render('graphs/social/friends.mustache');
});
// the following method in the User class
public static function following($username)
{
$queryString = "MATCH (user { username:{u}})-[:FOLLOWS]->(users) ".
" RETURN users ORDER BY users.username";
$query = new Everyman\Neo4j\Cypher\Query(Neo4Client::client(), $queryString,
array('u' => $username));
$result = $query->getResultSet();
return self::returnAsUsers($result);
}
If the list contains users, it will be returned to the controller and displayed in the right-hand part of the page, as
shown in Figure
8-9
. The display code for showing the list of users can be found in
{PROJECTROOT}/ app/templates/
graphs/social/friends.mustache
and is shown in the code snippet in Listing 8-26.