Database Reference
In-Depth Information
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="updateUser" class="btn btn-default">Update User</button>
</div>
</div>
</form>
User Edit Route
The application contains a route with the path
/user/edit
, which takes the
JSON
object argument. The
User
object
is converted from a
JSON
string and returns a
User
object as
JSON
. The response could be used to update the form
elements, but because the values are already set within the form there is no need to update the values. In this case, the
application uses the
JSON
response to let the user know if the update succeeded or not via a standard
JavaScript
alert
message (Listing 8-23).
Listing 8-23.
User
edit
Route
// edit a user
$app->put('/user/edit', function() use ($app){
$params = json_decode($app->request()->getBody());
$user=User::updateUser($_SESSION['username'],$params->firstname,$params->lastname);
echo json_encode($user);
});
User Update Method
To complete the update, the controller layer calls the
updateUser
method in
User
class. Because the object being
passed into the update method did nothing more than modify the first and last name of an existing entity, you can use
the
SET
clause via Cypher to update the properties in the graph, as shown in Listing 8-24. This Cypher statement also
makes use of the
MATCH
clause to retrieve the User node.
Listing 8-24.
The
updateUser
Method in the User Class
public static function updateUser($username,$firstname,$lastname){
$queryString="MATCH (user:User {username:{u}} ) " .
"SET user.firstname = {fn}, user.lastname = {ln}".
"RETURN user";
$query = new Everyman\Neo4j\Cypher\Query(Neo4Client::client(), $queryString, array(
'u' => $username,
'fn' => $firstname,
'ln' => $lastname));
$result = $query->getResultSet();
return self::returnAsUsers($result);
}