Hello, I'm trying to create a copy of a model, a user profile I have in session. But I cannot get my head around it. The reason is that the user can use a form where he can edit the email address, but I check first if the new email is already registered by someone else before saving the profile. If it is already registered, the user is brought back to the edit profile form with the unsaved new email in the form and an error message (but the old email address is the one in session, not displayed). This beacase if a user refreshes the page, the correct old email will be shown in the form and the error message will not be displayed anymore. This is the model:
<?php
namespace MyApp\Model;
use MyApp\Model\BaseModel;
use Phalcon\Mvc\Model\Validator\Uniqueness;
use Phalcon\Mvc\Model\Validator\PresenceOf;
class Users extends BaseModel
{
protected $id;
protected $email;
/**
* Validate that emails are unique across users
*/
public function validation()
{
$this->validate(
new Uniqueness(
array(
"field" => "email",
"message" => "Email already registered"
)
)
);
return $this->validationHasFailed() != true;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($e)
{
$this->email = $e;
}
}
This is the controller:
<?php
public function someAction()
{
$profile = $this->session->profile;
$oldEmail = $this->session->profile->getEmail(); //email adress from session
$newEmail = $this->request->getPost('email', 'email'); //email address from user form
$this->session->profile->setEmail($newEmail); //assign new email address to model
if(!$this->session->profile->save()){ //trying to save
$form->setEntity($profile); //email already registered! filling the form with old info
$this->session->profile->setEmail($oldEmail); //re-assigning old email address
} else {
$form->setEntity($this->session->profile); //everyting ok, filling form with new info
}
}
I expected to see the old email andress if the validation failed, instead I see the new one. I tried to simplify my example:
<?php
public function someAction()
{
//profile from session
$this->logger->info($this->session->profile->getEmail()); //logs OLD email address
$profile = $this->session->profile;
$form->setEntity($profile); //filling the form with old info (old email address)
$this->logger->info($profile->getEmail()); //logs OLD email address
$this->session->profile->setEmail($this->request->getPost('email', 'email'));
$this->logger->info($profile->getEmail()); //logs NEW email <--- here's my problem
}
How can it be possible? Is the variable passed as reference?
Thanks for your help.