I have a very simple plugin to update a user's name. All I'm doing is:
$userId = craft()->userSession->id;
$user = craft()->users->getUserById($userId);
$post = craft()->request->getPost();
$user->firstName = $post["firstName"];
craft()->users->saveUser($user);
The problem I'm having is after the page reloads from the POST submit the output still shows the old name, but then if I refresh the page without resubmitting I see the new name. Point is the name is changing but it seems like maybe this data is cached somewhere? How do I fix this?
UPDATE
To be more clear, this does not happen on all field types. First name and last name do it. Custom plain text fields do not. Category fields do it.
UPDATE
Here is the code I'm using in one of my controllers. This will not be the final code but I don't feel like that matters. Just these few lines will produce the problem I'm talking about:
public function actionEditContactInfo()
{
$userId = craft()->userSession->id;
$user = craft()->users->getUserById($userId);
$post = craft()->request->getPost();
$user->firstName = $post["firstName"];
craft()->users->saveUser($user);
}
For AJAX calls I'm doing a little bit more:
public function actionEditContactInfo()
{
$userId = craft()->userSession->id;
$user = craft()->users->getUserById($userId);
$post = craft()->request->getPost();
$user->firstName = $post["firstName"];
craft()->users->saveUser($user);
$editUser = $user->getContent();
$template = craft()->templates->render("users/profile-form/_contact-info-form", array("editUser" => $editUser));
$this->returnJson(array('template' => $template ));
}
In both cases not all fields display the updated content after a form submit is made. Custom plain text fields seem to be ok. The default user model first name and last name are problematic as are category fields. Not sure about other field types.
UPDATE
It has been brought to my attention that I should probably be more clear in my explanation of this problem. Here it goes from top to bottom:
I have a form with 2 fields:
- A category field
- A plain text field
The form submits data to the server by making an AJAX call to a controller. The controller updates the info as needed then renders a template representing the updated form. The template is then encapsulated in a JSON array and passed to a JavaScript function which updates the user's browser.
The problem I'm experiencing is not that the data is not saving, but that when the new form is re-rendered not all of the fields reflect the changes. But if I refresh the page after saving everything is fine.
Of the two fields mentioned above, plain text works as expected but the category field is problematic. Even if I dump() the user data in the re-rendered template I am given the old value for the category field even though it was updated. Here is my controller code:
public function actionEditNetworks()
{
$userId = craft()->userSession->id;
$user = craft()->users->getUserById($userId);
$user->setContentFromPost('fields');
// Save the user
$result = craft()->users->saveUser($user);
$editUser = $user->getContent();
$template = craft()->templates->render("users/profile-form/_network-links-form", array("editUser" => $editUser));
$this->returnJson(array('template' => $template));
}