Hi @all,
i develop a register form with several tables, i took the "invo" project as my base.
- Table "account" includes accout_id, password, email and username -> here i can insert data with no problem.
- Table "profile" includes profile_id, account_id(foreign key), gender, birthday -> can't insert
I created 1 Controller RegisterController.php and 2 Model Account.php and Profile.php.
How can i insert data into the second table and set the account_Id as foreign_Key into its.
RegisterController:
class RegisterController extends ControllerBase{
$form = new RegisterForm;
if ($this->request->isPost()) {
$username = $this->request->getPost('username', 'alphanum');
$email = $this->request->getPost('email', 'email');
$password = $this->request->getPost('password');
$repeatPassword = $this->request->getPost('confirmPassword');
$gender = $this -> request -> getPost('gender');
if ($password != $repeatPassword) {
$this->flash->error('Passwords are different');
return false;
}
$user = new Account();
$user->username = $username;
$user->password = sha1($password);
$user->email = $email;
$user->date = new Phalcon\Db\RawValue('now()');
$user->active = 'Y';
if ($user->save() == false) {
foreach ($user->getMessages() as $message) {
$this->flash->error((string) $message);
}
} else {
//$this->tag->setDefault('email', '');
//$this->tag->setDefault('password', '');
$this->flash->success('Thanks for sign-up, please log-in to start generating invoices');
return $this->forward('session/index');
}
}
$profile = new Profile();
$profile -> gender = $gender;
$profile -> save();
$this->view->form = $form;
}
}
Account.php:
use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Validator\Email as EmailValidator; use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
class Account extends Model { public function validation() { $this->validate(new EmailValidator(array( 'field' => 'email' ))); $this->validate(new UniquenessValidator(array( 'field' => 'email', 'message' => 'Sorry, The email was registered by another user' ))); $this->validate(new UniquenessValidator(array( 'field' => 'username', 'message' => 'Sorry, That username is already taken' ))); if ($this->validationHasFailed() == true) { return false; } }
}
Profile.php:
use Phalcon\Mvc\Model;
class Profile extends Model { public $gender;
public $birthday;
}